Jump to content

ckrudelux

Members
  • Posts

    565
  • Joined

  • Last visited

Everything posted by ckrudelux

  1. Php is on the server side and can only send data to the client. If you want to delay output or make anything after the page has been loaded javascript is the language to use. It runs on the client side and can manipulate the page after loading
  2. Yeah, since in your example you have a string witch both have text content and data content. You will have to seperate the two part some how. Either by having them in two different variables, remove parts of the string or to split it. There are several ways to do this. $txt = 'A long string'; $datastr = '1,2,3,4,5'; echo $txt . $datastr; explode(',',$datastr);
  3. Say what? php requires a dilimiter then exploding if you have a long.string you will need to extract the data portion before exploding the string into an array $txt = 'A long string 1,2,3,4,5'; explode(',', $txt); outputs an array like this: ['A long string 1', '2', '3', '4', '5'] You can use functions like substr to extract the string protion you need before you use explode to remove unwanted text.
  4. Hm, looked at my own files and noticed that I have this in the top. Does it solve the issue? I think I gave up trying to find what was wrong and just tried this and left it like that. <?php error_reporting(E_ALL); ini_set("display_errors", "on");
  5. Maybe it's an error reporting level. My code doesn't halt if I echo something that doesn't exist. If I rember correctly I think I had some similar issue until I change the error reporting level. This is also a neat trick in php 7 like isset but without all the extra work echo $var ?? 'default value';
  6. Nothing wrong with the code above (Just not so reusable). To think of is: that user input don't go raw in the SQL query (mysqli or PDO makes that easy) and not outputting raw user input/data to the browser by using for example htmlentities function.
  7. ckrudelux

    System shop

    We won't do the work, but we can guide you and help you with finding out where the problems are. I'm guessing cause you didn't post any code. You don't have any PHP knowledge so start to read about PHP and how that works. You should be able to figure out by your own how to build what you want after that. If you have an issue related to your code or coding practice we are here to help. Good luck
  8. In php you get the URI params by the $_GET variable. In your case you would use $_GET['model'] in php to get the value of model value.
  9. If it's something simple why not just simply make PHP draw a image that looks like the HTML output. If you want take a screenshot of the whole site, it's going to be a different story, might be web services you can use. To capture the screen you could make a bash script taking a screen shot of the server entering the site.
  10. ckrudelux

    System shop

    Try writing in English and people might be more interested in helping out with what ever problem your are facing.
  11. You should use htmlentities before displaying it on the page, not before you add it to the database. The database needs escaping so it won't interfer with your query.
  12. @ means that it suppress any error triggered by that expression. https://www.php.net/manual/en/language.operators.errorcontrol.php
  13. ckrudelux

    Php Exceptions

    To answer my own question remember the namespace. This will make things work much smoother
  14. ckrudelux

    Php Exceptions

    Should it matter from there I catch an exception? This works and catches the PDO exception from users $dice = new Dice(); try { $dice->create('Controller'); }catch(Exception $e){ } Try to catch the exception in the controller wont work class Controller { public function __construct(Users $users){ try{ $users->create('username','password'); }catch(Exception $e){ } } }
  15. How would I unset a cookie with an unknown path? What I'm trying to do is to automate the task of removing the session if nothing is stored in the session variable. But I'm trying to implement so that I could use different sessions in different folders which makes the path more or less unknown. I know this might not be a good way of doing it but still I'm curious to know.
  16. I been thinking for a long time about how to treat DB with PHP in a good way. I get stuck at if I should have a method creating the tables the class needs or I just create the table manually then I would need the class in a project. So I started to search a bit on the topic and got some articles about data mappers which uses a class to do the bidding between the program and the database. Article: http://www.slideshare.net/vlucas/building-data-mapper-php5-presentation Still I feel like I haven't found any good answers to my question on how the DB should be implement, maybe I just think about it too much. So I post this thread here in need of guidance, where my brain simply just make thing too complicated.
  17. Oh.. should have thought about that.. thank you very much
  18. This is the exact code but it should be the same as the example. private function updateChildren(Menu $menu){ if(count($menu->getItems())){ if($insert = DB::PDO()->prepare("INSERT INTO `menu` (`label`,`path`,`parent`) VALUES (:label, :path, '". intval($menu->getID()) ."')") && $update = DB::PDO()->prepare("UPDATE `menu` SET `label`=:label, `path`=:path WHERE id=:id")){ foreach($menu->getItems() as $item){ if($item->getID()){ if($update->execute(array("label"=>$item->getLabel(),"path"=>$item->getPath(),"id"=>$item->getID()))){ $this->updateChildren($item); } }else{ if($insert->execute(array("label"=>$item->getLabel(),"path"=>$item->getPath()))){ $item->__construct(intval(DB::PDO()->lastInsertID())); $this->updateChildren($item); } } } } } if(count($menu->getRemoved())){ foreach($menu->getRemoved() as $remove) $this->removeMenu($remove); } } I tested it by var_dump() the insert and update variables
  19. PHP is a server side language and can not interact with the html.You can look at it like php is writing you a html file and then sends it to the browser. What you do is you create an other php file which disblays the description of your insect <tr> <td><a href="descriptionpage.php?insect=<?php echo $row['insect_id']?>">Description</a></td></tr> You can retive the insect key with this variable on you description page: $_GET['insect']
  20. <?php $pdo = new PDO(...);$insert = $pdo->prepare(...);$update = $pdo->prepare(...); $items = array(...); foreach($items as $item) if($item->id) $update->execute(...); else $insert->execute(...); The value of $insert becomes TRUE while $update becomes a PDOStatement object.Why doesn't both of them become a PDOStatement?
  21. My late second post. Have been away visiting my grand parents.I've been looking into my collection class and came up with a method making it possible to extend my SQL query. So then extending my collection class I can setup my query from the constructor using the methods: addTables, addColumns, addConditions, setColumnID and setObjectName all of these are protected methods and are only for setting up the query. This would look something like this then extending the collection class public function __construct(){ $this->addTables("`table1`"); $this->addColumns("`table1`.*"); $this->setObjectName("test"); $this->setColumnID("`table1`.id");} Then calling the fetch method which is a default method from the collection class I would get a test object returned populated with the row data from the database. If I want to extend this class I could just add an other table and columns public function __construct(){ parent::__construct(); $this->addTables("JOIN `table2` ON `table1`.id=`table2`.id"); $this->addColumns("`table2`.*"); $this->setObjectName("test2");} This made the code less repetitive then writing collection classes. But I still have to know the table name used in the previous class. At my grand parents I used my laptop since I had a clean set of the code I had to add paths etc. So I started to add setup methods to my controllers so I didn't have to write all paths and add default values every time I had a clean start. I'm not sure about how I should call these methods since I don't want to call them twice. I also know I will be adding functionality later on so I need some sort of register on what setup methods that has been called. One idea is that in the admin panel I will have a list of all the controllers which has a setup method. I would have to keep track of which one I want run and let the setup method figure out if it should run or not. This would mean that I don't need a register. What I got now is a main install file that looks for a setup methods in all files it can find but this is more of a setup all file and would probably not be as good for a single setup call. I've also started doing layout design for my admin panel. I made a Wordpress looking layout which is what I'm currently using. I'm thinking of changing some of it's design cause of the complex design of the menu buttons then hovering items that are next to the current one. Which I solved by using CSS classes set in the template by checking pre set flags of the different items in the menu. This could been preformed by CSS alone if there where a way to select something previously to the selected element. Oddly the CSS support only for the next element and not the previous one.
  22. ckrudelux

    Project Blog

    IntroductionSo I finally decide to have a go with this idea of having a project blog I'm really bad at this sort of thing and often find my self working for hours not writing a single line of comment to my code.. I don't find it necessary cause my naming often tells what it does so I don't see the point typing out the same thing the name says. But maybe I just lake the guide lines of typing comments.. read once that if you have to think twice about what a code you written does you need a comment for it. I will be doing a MVC pattern system. I've already started with this project so the first post will be covering what I've done so far. I encourage to comment any spelling or miss use of words since English isn't my main language and it would be good to get some improvements.To be clear the questions I have in my post are meant for me and I don't expect on you answering them for me. So who am I? Well I'm Andreas, I've been a member since 2008 I know I'm not the most active user I usually ask questions to small thing I don't now how to search for or then I just got completely stuck at some code error. Lets get started.So to get things in motion I'm using a class I call a Mapper class and it's task is to register paths to a controller and method. I get the path from the global $_SERVER['PATH_INFO'] variable which takes everything after "index.php" so "index.php/blog/posts/entry" would be "/blog/posts/entry". I had no intention of having any SQL queries into this class but I soon realized that I had to have a "registerMap" method to the class so I could set paths from a controller or model classes.Only problem with using this mapper method is that I can't do pages like "blog/posts/entry/page/2" in the path so they are set as normal get keys. From the index file I define paths to various places including a web root which defines the index file location compared to the server root. This is good in case you want to put this code into a sub folder. The template engine i choose to use is Smarty, you can find it at http://smarty.net, I just find it easy to use. My abstract Controller class extends the Smarty class so I can set the data form the Controller class. I added a function to tell what template folder I want to use in my view directory.Another neat thing I did was to make a plugin for Smarty what inserts the CSS files to the head so I get less request to the server. I will change that later to handle JavaScript files too. I also have a ActiveUser class that handles the current user on the site. I like this class a lot cause of the way I use sessions with it. Since we aren't allowed to throw cookies at our visitor without any permission this class only starts the session if the session cookie is set and the cookie is only set then you login on the page. I mostly like this cause I've seen complains about just this problem then reading about that new law about cookies. I think that covers the basic introduction of that I've done without going into detail of the exact code looks like to the point there I'm now. The next step is to add an admin panel where I can add and remove users. And change settings to other component. Current issueSo this is about how I handle the users which isn't the active one like say I'm logged in and i want to change someone else user type. How should I get that object in the code.At the moment I can fetch users from the ActiveUser class but this feels wrong. Cause it's not within the task that is set for the ActiveUser class. So what I think the ActiveUser should handle is hold the current user id and type and have the responsibilities to create/update/remove users. (maybe change password and type for the current user but it feels like that should be in the User class)The User class extends the Person class and contains only one extra property which is the user type.Why I choose to not have the user name in the User class is cause I hashed the user name making it more time consuming if the database got out. I also written an abstract class for collections the idea is that the class could sett properties like limit the number of objects which would be returned or the sorting order of the query "ASC and DESC" (Maybe a random order would be a great thing to add). Problem with this Collection class is that it's not fully thought through. I've written some code that uses the concept but I find my self doing the same code over and over again mostly the only thing that is different is the query to the database unless the object has child objects to be loaded. I'm sure that I should get the users from a collection class i just don't know how to complete this collection class at the moment cause the case scenarios are too many. One example is that i can load a user from the user collection but since the user is a part of a person. The person data must be loaded to the object too. Question is should I add some sort of property in the person collection class to get the query for that specific table so I can add it to my object. Also what if the object I extends has child objects and how do get them, a public method for getting that particular property data set from that person. Well I think this will be all for this time.. hope this wasn't too confusing.
  23. ckrudelux

    OOP and MYSQL

    Might have done some over thinking on how I wanted things to look like while still wanting a new point of view. I had a look at the PDO again and yes it might suite the need I'm looking for. I just couldn't place it in my thoughts and it felt wrong. Thanks for the answers I got
  24. ckrudelux

    OOP and MYSQL

    CollectPerson is storing the Person object just look in the while loop and you will see the assigning of the object to the persons property. I haven't used PDO cause I never used it before I don't really know why I should either. I understand that I could use any database I want with the same function but that isn't really needed for me at this point. Auto escaping sure it's a good thing but I could simply write my own instead of loading the hole PDO class.
  25. ckrudelux

    OOP and MYSQL

    Yes that is kind of what I do now but I fetch the array from the object collecting the result so I know that I made the query for it. I would still need to validate the data (Just haven't thought about it yet). But at least I could be sure the data is from table a and not b or isn't made up. I know it still could be over written but then it would be on purpose and not by sending the wrong array into the mix. I just feel is something else I missed something simpler I haven't thought of.
×
×
  • Create New...