Jump to content
Larry Ullman's Book Forums

margaux

Members
  • Posts

    453
  • Joined

  • Last visited

  • Days Won

    52

Everything posted by margaux

  1. My response may sound a little harsh but if you post this sort of topic on other forums you will get a much harsher response. 1. the book clearly states that it is not for beginners - if you are not able to resolve this problem, you will have difficulties with the rest of the book; 2. your problem is due to not understanding file structure; 3. to debug errors, you need to provide more information. What home page? Are you referring to the first or the second example in this book? Without more information about files, coding etc, there is not enough information to give you any kind of useful help. Read the forum guidelines and you may want to start off with this book which is very good.
  2. Run the query in something like phpmyadmin substituting values in the values clause of your query to see if you get an error. Its difficult to help without knowing what error you're receiving.
  3. When b is equal to zero the conditional if ($ interprets that as boolean false. You want to use the function is_int(). Or better still look into the filter_var functions which gives you the option of specifying a range of valid values.
  4. $_POST['amount] is a string. The empty() function will return true if the variable is an empty string, false, array(), NULL, 0, or an unset variable. Instead of if (!empty($_POST['amount'])) { try if (isset($_POST['amount'])) {
  5. I dont have this book so I'm guessing at what you're trying to do. By submitting the form without making any changes, the script is probably using the previous values that had been entered. The error is thrown because an update expects change and will return false if you try to do an update without actually changing any values. While in principle no error is made, technically there is an error. After the form has passed validation you might want to clear the $_POST array to prevent exactly this situation, which can be confusing to the user.
  6. No - but this sounds like an interesting option. Could you provide some pointers on how to implement that approach? Thanks.
  7. I believe it is possible to query a database via a remote host using ssh2_tunnel but you need to ensure you have the correct permissions. My opinion on the larger question you ask is that the company would want to change its web hosting arrangements to ensure it has full control of its assets (files, databases etc) and is able to change the arrangements as its hosting requirements change without too much hassle. It may be that your client has to go through some short term pain to get to a position that will be more suitable for the longer term.
  8. Looks like you haven't closed the final elseif clause. Try putting a closing curly bracket on line 87. You probably want to use a text editor which folds your code to make it easier to find these kind of errors.
  9. Try declaring $phone = ''; before the first if statement. You may want to double check your regex because I did not get the results you described. Also check that you are using null and empty correctly as they do not always equate. Finally - please use code tags for your code.
  10. Thanks for the reply Edward. I think you may have misunderstood my question or I'm not completely understanding your response. The User and Page classes were originally set up with Id and creatorId respectively. In order to display the creatorName which already exists as username in the user class I have added the creatorName attribute (and getCreatorName method) to the Page class. creatorName is not a field in the pages table. Is it redundant to have name in both classes (username and creatorName in User class and Page class)? Its just 2 attributes currently, but if I decided the Page class wanted to manipulate a lot more data about the creator there would be alot of overlap between the 2 classes? Does that matter in classes?
  11. I'd like to display the page author's name on the single post page. My approach: add a creatorName attribute and a corresponding getCreatorName method to the Page class in page.php, change the the query to $q = "SELECT id, creatorId, title, content, DATE_FORMAT(dateAdded, '%e %M %Y') AS dateAdded, u.username AS creatorName FROM pages AS p INNER JOIN users AS u ON u.userId=p.creatorId WHERE p.id= $id"; in page.html - use $page->getCreatorName(); to display the name This works nicely and I like the way the code structure enables me to slot in a new attribute easily. I guess this is the advantage of using OOP. My question - I have the page creator's name as an attribute in both classes, is this approach the best way? I was considering changing the FETCH_MODE to be able to access the username without having to add it as an attribute to class. Is one way better than the other or is it a matter of preference. I like the first way because I have instantiated the page object and have access to all its methods and attributes if I decide I want to do more processing with it.
  12. There are a number of approaches you can take. $date is a string - so you could use substr $date = '15-09-2010'; $year = substr($date,-4); or since the date is formatted with dashes - you could use explode $date = '15-09-2010'; $date_parts = explode('-', $date); $year = $date_parts[2];
  13. There is a chapter in the book which details how to use cookies and sessions, though maybe not with that specific example. Once you know how to use them, you would be able to adapt them easily for your requirements. Another option would be to use a hidden field to pass the variable.
  14. I'm not sure there is an easy way as the query is first sent and the variables are sent later. What exactly is the error you're getting? I often copy and paste the query into something like phpmyadmin and substitute real values for the variables. If that does not produce an error, echo out the variables before binding them to ensure they are valid. Its difficult to be more helpful without more information. Show us the code for the prepared statement and what error you're getting.
  15. If I understand correctly, one of the first things the pages do is to require the utilities file which autoloads the classes, starts the session and sets the $user variable depending on whether or not $_SESSION['user'] is set. If it doesnt exist, $user is set to null. When a user logs in the method setFetchMode with PDO::FETCH_CLASS parameter is used to create the $user object and $_SESSION['user'] is created and set to the $user object which PHP automatically serialises so that the object can be stored in the session variable. There is an awful lot of processing going on in a few lines of code because some of the php functions do it all for you. Its good to understand what php is doing so you can use it productively. Good question - made me revisit some of these functions and hopefully my understanding of them has deepened.
  16. If you do decide to go with another host, think twice about switching to Go Daddy. The support is not great and I found updating my site very slow (also the ads they ran during the super bowl were so appalling that it is reason enough to boycott them ). I have since switched to csn-uk who are brilliant on support and not expensive. But if you're outside the uk they may not be a good option. Totally agree. Its worth learning prepared statements even if you get the stored procedures working. They're better for security and performance, particularly if you're running the same query multiple times. Prepared statements basically involve 3 steps prepare the statement by defining the placeholders to be used in your query. Placeholders indicate the places where variables will be provided at run time. A simple SELECT statement "SELECT * FROM users WHERE username='$un' && password='$pw' becomes "SELECT * FROM users WHERE username = ? && password =?). Question marks are the placeholders for the variables you will bind in the next step. So first you prepare the statement with $statement = mysqli_prepare($dbconnection, $query ); bind the parameters to the statement mysqli_stmt_bind_param($statement, 'ss', $un, $pw); The second paramenter lists in order the variable types of the parameters to be bound - in this instance I am binding 2 strings. For a more lengthy query you might get something like ssiisss - that would indicate you are binding 7 variables 2 strings then 2 integers then 3 strings. (Other variable types are supported.) The last parameters, again in order, specify the variables to be bound. execute the statement - mysqli_stmt_execute($statement);
  17. That is why you use arrays - to get rid of all those difficult to maintain (not to mention ugly) if elseif elseif statements. May I suggest you try using some arrays for the age conditional and for the gender conditional. You will make a leap as a programmer when you recognise opportunities to streamline your code.
  18. Yes - worth the effort to learn how to use the PDO class. It takes an object oriented approach to connecting to most database systems - mysql, oracle, postgreSQL, SQLite. So if your project needs to change databases, you only need to change one line of code. I'm currently learning it myself - Larry covers it in the 3rd edition of his book PHP Advanced and you can check out this tutorial.
  19. if (isset($_REQUEST['age'])) { echo '<p><b>You are ' . $_REQUEST['age'] . '!</b></p>'; } works nicely. I was thinking that he could do a bit more $ages=array('0-29' => 'young', '30-60'=> 'middle aged', '60+'=>'old'); if (isset($_REQUEST['age'])) { $age = $_REQUEST['age']; echo '<p><b>You are ' . $age .'!</b></p>'; echo '<p><b>You are ' . $ages[$age] .'!</b></p>'; } and in the html <form action="#" method="post"> <p>Age: <select name="age"> <?php foreach ($ages as $key=> $value) { echo " <option>{$key}</option>";} ?> </select>
  20. There's nothing wrong with your code but you might want to consider incorporating an associative array which would reduce your if elseif statement down to one line of code. Also if you are using a select field in your html form it would allow you to code all your options with just one line. It's a good way to test your understanding of arrays. Let us know if you have any questions
  21. To clarify my understanding, it would be good to get some feedback re some of the comments in this thread. In my parent class(databaseObject) is a public static function find_by_all() which I'm accessing via child classes (Comment and Photograph) I've not seen this before and my classes have both static and non static functions. I understood the point of inheritance was that the child class could access the parent method through an instance of itself (the child) without the need for overriding.
  22. @CCroxton - thanks for posting your experiences with different shopping cart s/w packages. This sort of specific feedback is helpful.
  23. That error message usually means the password is not correct for the user. You'll need to double check that the password you've entered in the DEFINE statement matches the one set up for the user when the database was created.
  24. Found the error. This line in the instantiate function $object->attribute = $value; should be $object->$attribute = $value; This part of the project working nicely now - I can extend databaseObject class to User class and Photograph class and all the CRUD is taken care of. Now to try to create a logfile using sessions so I will probably be back
  25. Getting closer - I knew that something was wrong with how the object was being instantiated.. Have changed the method to use get_called_class in the instantiate method. protected static function instantiate($record) { $class_name = get_called_class(); $object = new $class_name; foreach ($record as $attribute=>$value) { if ($object->has_attribute($attribute)){ $object->attribute = $value; } } return $object; } now the object looks like this array(4) { [0]=> object(Photograph)#5 (11) { ["db_fields"]=> array(5) { [0]=> string(2) "id" [1]=> string(8) "filename" [2]=> string(4) "type" [3]=> string(4) "size" [4]=> string(7) "caption" } ["id"]=> NULL ["filename"]=> NULL ["type"]=> NULL ["size"]=> NULL ["caption"]=> NULL ["temp_path":"Photograph":private]=> NULL ["upload_dir":protected]=> string(6) "images" ["errors"]=> array(0) { } ["upload_errors":protected]=> array(8) { [0]=> string(26) "File uploaded successfully" [1]=> string(41) "File size larger than upload_max_filesize" [2]=> string(40) "File size larger than form MAX_FILE_SIZE" [3]=> string(28) "File only partially uploaded" [4]=> string(19) "File does not exist" [6]=> string(34) "Temporary directory does not exist" [7]=> string(19) "Can't write to disk" [8]=> string(34) "File upload prevented by extension" } ["attribute"]=> string(10) "sunflowers" } Need to figure out why the values from the d/b aren't getting into the object.
×
×
  • Create New...