SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    May 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php mysql novice to ninja 5th edition

    php mysql novice to ninja 5th edition

    chapter 7

    After following the steps I keep getting the error:
    Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/ijdb/admin/authors/authors.html.php on line 13

    Does anyone know where I went wrong or where the book went wrong, here's the code:

    PHP Code:
    <?php include_once $_SERVER['DOCUMENT_ROOT'] .
        
    '/ijdb/includes/helpers.inc.php'?>
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Manage Authors</title>
      </head>
      <body>
        <h1>Manage Authors</h1>
        <p><a href="?add">Add new author</a></p>
        <ul>
          <?php foreach ($authors as $author): ?>
            <li>
              <form action="" method="post">
                <div>
                  <?php htmlout($author['name']); ?>
                  <input type="hidden" name="id" value="<?php
                      
    echo $author['id']; ?>">
                  <input type="submit" name="action" value="Edit">
                  <input type="submit" name="action" value="Delete">
                </div>
              </form>
            </li>
          <?php endforeach; ?>
        </ul>
        <p><a href="..">Return to JMS home</a></p>
      </body>
    </html>
    Last edited by SpacePhoenix; May 16, 2013 at 22:49. Reason: placed php tags around php code

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,000
    Mentioned
    101 Post(s)
    Tagged
    0 Thread(s)
    That error means that the foreach loop was expecting to be given an array to loop through but was given something else. Whenever you're going to build an array of items that a foreach is going to work with always do:

    PHP Code:
    $the_array=array(); 
    That ensures that the variable is set up as an array, if nothing is in it the foreach will see an empty array
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Member
    Join Date
    May 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a complete newbie at this, and I'm working off the code in the book. Would it be possible for you to rewrite the code so it'll work. If you could I'd really appreciate it and hopefully I'll be able to understand.

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,070
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Okay, I need you to confirm this but does your '/ijdb/includes/helpers.inc.php' file have something like the following:
    PHP Code:
    // Display author list
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';

    try
    {
    $result $pdo->query('SELECT id, name FROM author');
    }
    catch (
    PDOException $e)
    {
    $error 'Error fetching authors from the database!';
    include 
    'error.html.php';
    exit();
    }

    foreach (
    $result as $row)
    {
    $authors[] = array('id' => $row['id'], 'name' => $row['name']);
    }

    include 
    'authors.html.php'
    If so, you need to add the following immediately before the foreach
    PHP Code:
    $authors = array(); 
    I'm a bit surprised that the book has this typo in it. Granted you would only see this Warning if you have PHP setup to show warnings (which is good! I prefer new users have PHP setup to show warnings and notices when learning, you'll learn better practices that way).
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Member
    Join Date
    May 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol, not even close all it has is:

    <?php
    function html($text)
    {
    return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
    }
    function htmlout($text)
    {
    echo html($text);
    }

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,070
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Okay, what file has the foreach statement that builds $authors? You will want to add $authors = array(); immediately above the foreach to get rid of the warning.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Does the book expect you to have already put data into the authors table? Have you skipped a step?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,070
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Barry81 View Post
    lol, not even close all it has is:

    <?php
    function html($text)
    {
    return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
    }
    function htmlout($text)
    {
    echo html($text);
    }
    Ah, sorry, I found it, the file name would be: chapter7/admin/authors/index.php

    Adding $authors = array(); before the foreach will remove the warning.

    Quote Originally Posted by StarLion View Post
    Does the book expect you to have already put data into the authors table? Have you skipped a step?
    I think either way, he would run into this as throughout the process you create INSERT, UPDATE, and DELETE actions. So after you delete all authors you will see the warning.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  9. #9
    SitePoint Member
    Join Date
    Feb 2014
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help guys, you helped me big time with my own similar problem in a roundabout way

    Alan


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
  •