Jump to content
Larry Ullman's Book Forums

margaux

Members
  • Posts

    453
  • Joined

  • Last visited

  • Days Won

    52

Everything posted by margaux

  1. I'm not using / haven't looked at a framework. I was under the impression that static methods and properties could be inherited by child classes as long as the visibility was set correctly i.e. not private. Maybe I should move the static methods find_by_id(), find_all() and find_by_query() into the child classes but seems a shame to repeat all that code in several classes. I was hoping with the use of late static binding to be able to make the databaseObject class more abstract. Because the objects aren't being instantiated properly, I wonder if the problem is how the array $db_fields is set up.
  2. Edward - thanks for your responses and your questions as they are raising fundamentals that I haven't quite got to grips with yet. find_all() is a static method defined in the databaseObject class which is inherited by other classes including Photograph. The find_all() method gets all the records in the database, instantiates each record as a new instance of the calling class and returns an array of objects. so $photos in the original code should be an array of Photograph objects which I can loop through and display. I've dumped out the returned array being returned and its full of objects not fully formed e.g. Here is my databaseObject class array(3) { [0]=> object(DatabaseObject)#5 (1) { ["db_fields":protected]=> array(0) { } } [1]=> object(DatabaseObject)#6 (1) { ["db_fields":protected]=> array(0) { } } [2]=> object(DatabaseObject)#7 (1) { ["db_fields":protected]=> array(0) { } } } class DatabaseObject { protected static $table_name; protected $db_fields=array(); //common database methods public static function find_all() { return static::find_by_query("SELECT * FROM " . self::$table_name); } public static function find_by_id($id) { global $database; $result_array = static::find_by_query("SELECT * FROM " . self::$table_name . " WHERE id = {$id} LIMIT 1"); return !empty($result_array) ? array_shift($result_array) : false; } public static function find_by_query($q="") { global $database; $r = $database->query($q); $object_array = array(); while ($row = $database->fetch_array($r)) { $object_array[] = static::instantiate($row); } return $object_array; } protected static function instantiate($record) { $object = new self; foreach ($record as $attribute=>$value) { if ($object->has_attribute($attribute)){ $object->attribute = $value; } } return $object; } protected function has_attribute($attribute) { // returns an assoc array with all attributes as the // keys and their current values as the values $object_vars = $this->attributes(); //use array_key_exists to determine if the passed attribute exists as an // attribute of the current object return array_key_exists($attribute, $object_vars); } protected function attributes() { //return an array of attribute key value pairs for db fields only $attributes=array(); foreach ($this->db_fields as $field) { if (property_exists($this, $field)) { $attributes[$field]=$this->$field; } } return $attributes; } and here is the Photograph class class Photograph extends DatabaseObject { protected static $table_name="photographs"; public $db_fields=array('id', 'filename', 'type', 'size', 'caption'); //attributes correponding to d/b table columns public $id; public $filename; public $type; public $size; public $caption; I imagine I've made a mess of the visibility and I'm not sure that the array $db_fields from the Photograph class has been properly coded. Hope this makes sense and you can give me some guidance. If you don't want to look through all this, I understand.
  3. By using code tags, the code is formatted differently to the rest of the comment. In the top right corner of the edit box is a little arrow icon, click on it to open the formatting icons available to you when posting a comment. The code tags icon looks like <> . Click on it and insert your code into the dialog box.
  4. I'm struggling to understand how class inheritance works. I have 2 classes, one inherited by the other. class DatabaseObject { public static function find_all() { return static::find_by_query("SELECT * FROM " . self::$table_name); } } class Photograph extends DatabaseObject { public function image_path() { return $this->upload_dir .DS. $this->filename; } } In another file I try to access the method $photos = Photograph::find_all(); foreach ($photos as $photo) { echo '<figure><img src=../"'. $photo->image_path() . '" width="100"><figcaption>' . $photo->caption . ' ' . $photo->size_as_text() . '</figcaption></figure>'; } but get the error message Fatal error: Call to undefined method DatabaseObject::image_path() in /Applications/MAMP/htdocs/photo_gallery/public/admin/view_photos.php on line 22 I don't understand why its trying to access the method through the DatabaseObject class when I have specified the Photograph class. Would anyone be able to explain what is going on here? Hopefully I've provided enough information. Thanks
  5. It's difficult to debug when the code is presented in this way. Please would you use code tags. At first look, no error immediately jumps out. Which line is line 46? Look at that line and the few lines before it. Probably not causing this error but it looks like you may have a typo on the tablename in the SELECT statement - users instead of usera?
  6. Thanks HartleySan. I've experienced his scenario myself but as you said we need more info to be able to debug.
  7. I've also experienced similar scenarios. If I recall properly its when I'm posting a long response with several blocks of code.
  8. Big apologies, I read this far too quickly (and too early) on the train and was thinking this was two submit buttons. Code originally posted looks okay.
  9. The second form action is overwriting the first. You need to give the forms different names.
  10. My two cents for what its worth... Play to your strengths. What do you most like to program and what do you think you are best at? Currently Responsive Web Design is understandably the hot topic and I would certainly recommend understanding what that means and how to achieve it. It surprises me how many Web Design and Digital Marketing Agencies have not updated their own sites to be responsive. Other recommendations - solid HTML5 and CSS3 skills as they will eventually supercede HTML and CSS; javascript as I think there will always be a demand for good js programmers and a good understanding of Accessiblity as I think that will grow in importance and I think search engines will start rewarding more accessible pages. On the back end I see more job requirements for php than asp and if that's your strength learn OOP and at least one framework.
  11. That's great. Arrays and loops are worth getting to know well as they do a lot of work for you. In this instance if you want to add or change the car types, its just a quick change to the contents of the array. One suggestion that I learned here and will pass on - when debugging, print_r your variables. For example if you had inserted print_r($_POST); initially you would have seen that there was nothing in the select field and then you'd know where to amend your code.
  12. Firstly you need to give the select field a name otherwise you will not be able to access it via $_POST. Secondly, the value of all the options are always going to have the same value so it won't matter which option is selected. Thirdly, you need to use isset to validate select fields. Try this <select name="car_type">'; $car_types = array('VW Van','Chevy Camaro', 'Toyota Corolla' ); //create an array for car_types foreach ($car_types as $value) {loop through the array echo ' <option value="'. $value . '" '; //set the option value equal to the array value if ($row[0] == $value) echo 'selected '; //make sticky echo ">$value</option>"; //end option field } echo '</select><br /> <br />'; //end select input field Change line 29 to if (!isset($_POST['car_type'])) {
  13. This does not make sense - unless you use a loop, the option value is always going to be the same so how will you know what type of car is being selected? The error you're getting is resulting from your form validation. How are you validating the car_type field? You can't use empty, you need to use isset. In order to help, we need to see your code and it would help to see the d/b query you are using to populate the form. I am happy to try to help but I don't always have access to the book you refer to so please post the relevant code.
  14. If I understand the question correctly, you want to output a select field in your form, something like <option value="1">VW Van</option> <option value="2">Toyota Corolla</option> <option value="3">Chevy Camarro</option> You will need a loop for this, because at the moment the option value will always be the same value. Here's a suggestion on the assumption that you are querying a table which has the fields car_type_id and car_type. You will need to alter the code based on how you are getting the info but hopefully this gives you some idea. <select name="car_type">; while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){ echo '<option value="' . $row['car_type_id'] . '">' . $row['car_type'] . '</option>'; } echo '</select>'; Also you have a missing double quote in your first example on rows 7, 8 and 9. Should be <option value="'. $row[0] . '">VW Van</option>
  15. Pro Jquery Mobile is pretty good. I like to get off the computer and read some books occasionally but I agree with HartleySan. Check out some of the tutorials at Lynda.com - you can get a free week's trial which will give you an indication if its something you want to sign up to. Also check out css-tricks, Chris Coyier has some great tutorials and snippets. His site and forum is a little more focused on front end then back end development but there are alot of smart people there contributing and there is a fair amount of information related to WordPress. I'm afraid I can't help with ASP.NET.
  16. It depends on the project - if no or little cms functionality is required I handcode otherwise I use WordPress. I'm trying out the Roots theme which looks good, it does all the urls and has a responsive design but I haven't gotten very far with it yet. I'm hearing alot about Kirby - anyone used it?
  17. The more you practice the more you'll understand how to debug. In this case, the error message pointed you to a particular line - a large percentage of syntax errors are caused by an error in the preceding line, so be sure to scrutinise the error line and the previous one. As you code more, you'll probably realise that you make some errors consistently - I used to always forget the parentheses around an if clause, so I knew to look for that straight away. A good text editor will help by colour coding different languages and highlighting where there's an error - I use TextWrangler on mac, its free and am currently trialling Coda2 and will also try Sublime Text as I've heard both are very good. Do a search and you'll find lots of options - I think they're may be a thread somewhere on this forum.
  18. I would also recommend using HTML5 with something like modernizr for non-supporting browsers. HTML5 lets you specify an input field as required and more input types are catered for such as num, email, tel and url. These additions automatically do some validation on the client side and whilst you need to provide back-up for earlier browsers, it speeds things up for users on modern browsers.
  19. To get the total number of checkboxes checked, you want to use count($array). I think a for loop would work better. You'll need to amend the formatting of the output but this should give you the general idea. <?php if($_SERVER["REQUEST_METHOD"] == "POST"){ $array=array(); if (isset($_POST['interests'])) { $array=$_POST['interests']; } $count = count($array); echo 'You have checked ' . $count . ' boxes, the checked boxes were '; for ($i=0; $i < $count; $i++) { if ($i==($count-1)) { echo "and $array[$i]."; } else { echo ' ' . $array[$i] . ' '; } } } ?> <form method="POST" action="#"> <input type="checkbox" name="interests[]" value="music" /> Music <input type="checkbox" name="interests[]" value="movies" /> Movies <input type="checkbox" name="interests[]" value="books" /> Books <input type="checkbox" name="interests[]" value="games" /> Games <input type="submit" name="submit" value="submit" /> </form>
  20. What encryption function are you using to hash the password? If its SHA1, that's your problem as SHA1 returns a 40 character code and you're trying to store it in a 32 character field. Change it to MD5 and you should be okay.
  21. The results you are getting are correct. There are 2 syntaxes that can be used to insert a record: provide the column names and column values provide the column values only. Using the latter syntax requires that you provide a value for every column. If a value is not provided, then the default will be used. If no default has been defined, NULL will be used. If NULL is not allowed as in the case of last_name, you will get an error. I agree that it is misleading that the mysql SHOW COLUMNS command displays the default value as NULL. Look at it using phpmyadmin browse and you'll see that the default value is displayed as none.
  22. Good questions which highlight the decisions a web designer, developer and indeed the owner are required to make.They also highlight how increasingly complex web development is becoming. What are we going to do when google glass comes online? I always start with the users (e.g.what device they will be using to access the site and where from ) and what the objective of the page is e.g. selling, dissemination of information etc. Obviously, cost and time constraints have to be factored in. These considerations determine what devices need to be targeted with separate sites, responsive design or not at all.
  23. Thanks Larry for pointing out another useful resource. Abigail and I are looking for detection of mobile phones only - tablets are served the same site as desktops. Unfortunately the code you've referred to would mean tablet users would be served the mobile site. What do you think of the Mobile_Detect class which allows you to target tablets specifically thereby excluding them from the redirect? I agree that there are big downsides to creating different sites for different devices, but sometimes there are strong arguments to do so.
  24. I use this code on some of my sites. It serves up a different page for phones. You would need to amend it for tablets. function mobilePhone() { $type = $_SERVER['HTTP_USER_AGENT']; if ((strpos((string)$type, "Windows Phone") != false) || (strpos((string)$type, "iPhone") != false) || (strpos((string)$type, "Android") != false) ) { return true; } else { return false; } } if (mobilePhone() == true) { header('Location: http://www.example.com/mobile.html'); } ?>
×
×
  • Create New...