SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    7
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Problem with foreach from book study

    Immagine.png

    I'm studying from the book of which the author is Kevin Yank,
    I'm in chapter 5, the book advised me to turn to this forum.
    I'm doing the exercise using two separate databases so as to practice both with the example from the book and be with my idea,
    For now I posted the picture with the error that I'm from, someone can tell me yours?

  2. #2
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    7
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thanks

  3. #3
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    8,220
    Mentioned
    237 Post(s)
    Tagged
    1 Thread(s)
    Sorry but images need to get approved and we do check from time to time but if you think that we're taking too long, do get in touch with a member of the staff and send him a pm asking him to do so.

    I can't really see what's wrong. Your error clearly indicates that there's something missing in your foreach() and probably is a variable (jokes) that has no value.

    Now, you say that this is a book written by Kevin Yank. He wrote several books but because you're talking about "jokes", I think that this book in particular is "Build your own database driven website with PHP and MySQL"

    Chapter 5 is dedicated to the relationships between tables. Could it be the case that the table jokes was completely empty?

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Please post the php code, so we can take a look

  5. #5
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,014
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    If I remember the code correctly $jokes is the array that the jokes returned from the database is loaded into. A foreach loop expects to be given an array to iterate (loop) through, but when there are no jokes returned by the query, all it sees is an empty variable instead.

    Just before you grab the result set, add this:

    PHP Code:
    $jokes=array(); 
    That will set up the $jokes variable as an empty array, ready to have any jokes returned by the database added to it. I'm surprised that the error has not been corrected by now as it's existed in the last two editions of the book at least. @HAWK ; Could you please raise it with whoever at hq deals with errors/errata in SitePoint books? ty
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  6. #6
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    7
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    If I remember the code correctly $jokes is the array that the jokes returned from the database is loaded into. A foreach loop expects to be given an array to iterate (loop) through, but when there are no jokes returned by the query, all it sees is an empty variable instead.

    Just before you grab the result set, add this:

    PHP Code:
    $jokes=array(); 
    That will set up the $jokes variable as an empty array, ready to have any jokes returned by the database added to it. I'm surprised that the error has not been corrected by now as it's existed in the last two editions of the book at least. @HAWK ; Could you please raise it with whoever at hq deals with errors/errata in SitePoint books? ty
    thanks for reply

    I found that did not work because I had the authors table in the database EMPTY: D
    .. so I solved the problem. But it is also true that the book does not warn: D

  7. #7
    Galactic Overlord gold trophysilver trophybronze trophy
    HAWK's Avatar
    Join Date
    Aug 2003
    Location
    New Zealand
    Posts
    12,540
    Mentioned
    956 Post(s)
    Tagged
    14 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    @HAWK ; Could you please raise it with whoever at hq deals with errors/errata in SitePoint books? ty
    Sure

  8. #8
    SitePoint Enthusiast bronze trophy \\.\'s Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    72
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well readin that "Error" the first one is telling you that $jokes is not defined and thats your problem to start with. You should check you code to see that you have infact got an array called $jokes defined then take it from there.

  9. #9
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    7
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    foreach ($result as $row)
    {
      $jokes[] = array(
        'id' => $row['id'],
        'news' => $row['testonotizia'],
        'name' => $row['name'],
        'email' => $row['email']
      );
    }
    
    include 'jokes.html.php';

  10. #10
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    8,220
    Mentioned
    237 Post(s)
    Tagged
    1 Thread(s)
    Aaaaaaah! This is so much better! Thank you @phpita ;

    The thing is that I cannot always have access to the text directly so seeing your code helps.

    Still, since you've solved the problem (well done to you )... Am I right to assume that you don't need further help? (at least, for the moment?)

  11. #11
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    7
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by molona View Post
    Aaaaaaah! This is so much better! Thank you @phpita ;

    The thing is that I cannot always have access to the text directly so seeing your code helps.

    Still, since you've solved the problem (well done to you )... Am I right to assume that you don't need further help? (at least, for the moment?)
    No, thanks, i continue study


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •