Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Having Trouble Building a Site from Scratch

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 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>

3 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I would guess that $authors is probably not an array at this point. Do you have any authors yet?

I've input 2 into the database but every time I try to load the page I get that message. I'm using two include files my index.php which: // Display author list include $_SERVER['DOCUMENT_ROOT'] . '/ijdb/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';
and a helpers.inc.php which:
function html($text)
{
 return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}

function htmlout($text)
{
  echo html($text);
}

function markdown2html($text)
 {
  $text = html($text);

  // strong emphasis
  $text = preg_replace('/__(.+?)__/s', '<strong>$1</strong>', $text);
  $text = preg_replace('/\*\*(.+?)\*\*/s', '<strong>$1</strong>',  $text);

 // emphasis
  $text = preg_replace('/_([^_]+)_/', '<em>$1</em>', $text);
  $text = preg_replace('/\*([^\*]+)\*/', '<em>$1</em>', $text);

  // Convert Windows (\r\n) to Unix (\n)
  $text = str_replace("\r\n", "\n", $text);
  // Convert Macintosh (\r) to Unix (\n)
  $text = str_replace("\r", "\n", $text);

  // Paragraphs
  $text = '<p>' . str_replace("\n\n", '</p><p>', $text) . '</p>';
    // Line breaks
   $text = str_replace("\n", '<br>', $text);

  // [linked text](link URL)
  $text = preg_replace(
   '/\[([^\]]+)]\(([-a-z0-9._~:\/?#@!$&\'()*+,;=%]+)\)/i',
   '<a href="$2">$1</a>', $text);

return $text;
}

function markdownout($text) { echo markdown2html($text); }

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Right before you call the template file, do this:

var_dump($authors);

Does it have what you expect it to have? If not, then the problem is earlier in the code and you can work your way up.