SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach not working....help pls

    Just working my way through the sitepoint 'php & mysql' ebook but have been stumped by an error i'm getting on a foreach statement.

    Relating directly to the book it's Chapter 4......pages 130, 131 (for those who have the 'You Too Can Create Impressive Database Driven Web Sites Using PHP & MySQL!'

    Section from my jokes.html.php page, which is being stated as the error:

    Code:
    <?php foreach ($jokes as $joke): ?>
    Error is:
    Warning: Invalid argument supplied for foreach() in G:\mowes_portable\www\db\jokes.html.php on line 14

    I've triple checked it's the same as the book and can't see anything else amiss. Everything else was working prior to this section (connect to DB, encoding to utf-8, etc all as stated I should do).

    Any help much appreciated please.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what does var_dump($jokes); output?

  3. #3
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hash View Post
    what does var_dump($jokes); output?
    Have to excuse my complete ignorance here (completely new to php) but where would I place that please?

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    $jokes should contain an array. The error indicates it doesn't. To be sure, you'd have to post the entire code here (let's hope it's not too long ). Do use the appropriate php tags around the code please.

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Brussells View Post
    Have to excuse my complete ignorance here (completely new to php) but where would I place that please?
    You would put that before the line where the error occurs. That way you can see what $jokes contains (it should contain an array).

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Brussells View Post
    Have to excuse my complete ignorance here (completely new to php) but where would I place that please?
    var_dump dumps the contents of a variable so you can view it, your error is that the argument in the for loop (the variable $jokes) is incorrect, so it is helpful to see what is in that variable. Put it before the for()

  7. #7
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hash View Post
    what does var_dump($jokes); output?
    Output is:

    string(60) "Why did the chicken cross the road? To get to the other side" Warning: Invalid argument supplied for foreach() in G:\mowes_portable\www\db\jokes.html.php on line 15

    Which I presume is showing the array information - which is correct = the joketext

    Full code
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>List of Jokes</title>
    </head>
    <body>
    <p>
    Here's a list of all the jokes:
    </p>
    		
    <?php
    var_dump($jokes);
    foreach ($jokes as $joke):
    ?>
    		
    <blockquote>
    <p>
    <?php echo htmlspecialchars($joke, ENT_QUOTES, 'UTF-8'); ?>
    </p>
    </blockquote>
    			
    <?php endforeach; ?>
    </body>

  8. #8
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As a quick thought - is 'foreach' used on all versions of php or not?

  9. #9
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Yes, it is. But it wants an array, and $jokes is a string.
    Where does $jokes come from? In your script, it isn't defined anywhere. Where does it take that text?

  10. #10
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Brussells View Post
    Output is:

    string(60) "Why did the chicken cross the road? To get to the other side" Warning: Invalid argument supplied for foreach() in G:\mowes_portable\www\db\jokes.html.php on line 15

    Which I presume is showing the array information - which is correct = the joketext
    foreach requires an array, the clue as to what is wrong above is "string(60)", a string is not an array, you can read more about types in php here, but essentially:

    PHP Code:
    $jokes = new array(); // create an empty array named jokes
    $jokes[0] = 'why did the chicken ...'// add this string to the array
    $jokes[1] = 'I just flew in from ...'// add another string, jokes is now an array containing two strings 
    if you var_dump($jokes) now you will see the difference

  11. #11
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Yes, it is. But it wants an array, and $jokes is a string.
    Where does $jokes come from? In your script, it isn't defined anywhere. Where does it take that text?
    Part of my index.php file

    Code:
    	// SELECT joketext from table
    	$result = mysqli_query($link, 'SELECT joketext FROM joke');
    	if(!$result)
    	{
    		$error = 'Error fetching jokes: ' .mysqli_error($link);
    		include 'error.html.php';
    		exit();
    	}
    	
    	// Create array from result set
    	while ($row = mysqli_fetch_array($result))
    	{
    		$jokes = $row['joketext'];	
    	}
    	
    	// display results
    	include 'jokes.html.php';

  12. #12
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Here's the error:
    PHP Code:
        // Create array from result set
        
    while ($row mysqli_fetch_array($result))
        {
            
    $jokes $row['joketext'];    
        } 
    This way, at the end of the while loop, $jokes will contain only the text of the last joke. To create an array, you must add [] to $jokes:
    PHP Code:
        // Create array from result set
        
    while ($row mysqli_fetch_array($result))
        {
            
    $jokes[] = $row['joketext'];    
        } 

  13. #13
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Here's the error:
    PHP Code:
        // Create array from result set
        
    while ($row mysqli_fetch_array($result))
        {
            
    $jokes $row['joketext'];    
        } 
    This way, at the end of the while loop, $jokes will contain only the text of the last joke. To create an array, you must add [] to $jokes:
    PHP Code:
        // Create array from result set
        
    while ($row mysqli_fetch_array($result))
        {
            
    $jokes[] = $row['joketext'];    
        } 
    That nailed it!! I triple checked the jokes.html.php page but didnt think it was within my index.php where the fault lies.

    thanks ever so much to everyone for your prompt and thorough responses - I've definitely learnt something very good today (other than learning to read and type correctly )

    Onwards I can now go!

  14. #14
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Errors are easily made. That's why it's important to learn how to find them too
    Good luck!

  15. #15
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Agreed - Lesson learnt.

    Thx again.


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
  •