I am creating the Simple Blog from PHP for absolute beginners book. I have worked through the book and my only error now occurs when I am logged in as the admin and try to Post a Blog. The errors I get are:
Warning: array_push() expects parameter 1 to be array, boolean given in C:\xampp\htdocs\simple_blog\inc\functions.inc.php

Warning: array_pop() expects parameter 1 to be array, boolean given in C:\xampp\htdocs\simple_blog\index.php

And this error where the entry I created should be:
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\simple_blog\index.php

here is my index.php code:

<?php

    error_reporting(E_ALL);
    ini_set('display_errors', 2);
    session_start();

    /*
     * Include the necessary files
     */
    include_once 'inc/functions.inc.php';
    include_once 'inc/db.inc.php';

    // Open a database connection
    $db = new PDO(DB_INFO, DB_USER, DB_PASS);

    // Figure out what page is being requested (default is blog)
    if(isset($_GET['page']))
    {
        $page = htmlentities(strip_tags($_GET['page']));
    }
    else
    {
        $page = 'blog';
    }

    // Determine if an entry URL was passed
    $url = (isset($_GET['url'])) ? $_GET['url'] : NULL;

    // Load the entries
    $e = retrieveEntries($db, $page, $url);

    // Get the fulldisp flag and remove it from the array
    $fulldisp = array_pop($e);

    // Sanitize the entry data
    $e = sanitizeData($e);

?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-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" />
    <link rel="stylesheet" href="/simple_blog/css/default.css" type="text/css" />
    <link rel="alternate" type="application/rss+xml" 
        title="My Simple Blog - RSS 2.0" 
        href="/simple_blog/feeds/rss.php" />
    <title> Simple Blog </title>
</head>

<body>

    <h1> Simple Blog Application </h1>
    <ul id="menu">
        <li><a href="/simple_blog/blog/">Blog</a></li>
        <li><a href="/simple_blog/about/">About the Author</a></li>
    </ul>
	
<?php if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == 1): ?>
    <p id="control_panel">
        You are logged in!
        <a href="/simple_blog/inc/update.inc.php?action=logout">Log
            out</a>.
    </p>
<?php endif; ?>

    <div id="entries">

<?php

// If the full display flag is set, show the entry
if($fulldisp==1)
{

	// Get the URL if one wasn't passed
    $url = (isset($url)) ? $url : $e['url'];

	if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == 1)
    {
        // Build the admin links
        $admin = adminLinks($page, $url);
    }
    else
    {
    	$admin = array('edit'=>NULL, 'delete'=>NULL);
    }
	
    if($page=='blog')
    {
        // Load the comment object
        include_once 'inc/comments.inc.php';
        $comments = new Comments();
        $comment_disp = $comments->showComments($e['id']);
        $comment_form = $comments->showCommentForm($e['id']);
		
    }
    else
    {
        $comment_form = NULL;
    }
?>

        <h2> <?php echo $e['title'] ?> </h2>
        <p> <?php echo $e['entry'] ?> </p>
        <p>
            <?php echo $admin['edit'] ?>
            <?php if($page=='blog') echo $admin['delete'] ?>
        </p>
        <?php if($page=='blog'): ?>
        <p class="backlink">
            <a href="./">Back to Latest Entries</a>
        </p>
        <h3> Comments for This Entry </h3>
        <?php echo $comment_disp, $comment_form; endif; ?>

<?php

} // End the if statement

// If the full display flag is 0, format linked entry titles
else
{
    // Loop through each entry
    foreach($e as $entry) {

?>

        <p>
            <a href="/simple_blog/<?php echo $entry['page'] ?>/<?php echo $entry['url'] ?>">
                <?php echo $entry['title'] ?>

            </a>
        </p>

<?php

    } // End the foreach loop
} // End the else

?>

        <p class="backlink">
<?php

if($page=='blog' 
    && isset($_SESSION['loggedin'])
    && $_SESSION['loggedin'] == 1):

?>
            <a href="/simple_blog/admin/<?php echo $page ?>">
                Post a New Entry
            </a>
<?php endif; ?>
        </p>

        <p>
			<a href="/simple_blog/feeds/rss.xml">
                Subscribe via RSS!
            </a>
        </p>

    </div>

</body>

</html>

and my functions.php code:

<?php

function retrieveEntries($db, $page, $url=NULL)
{
    /*
     * If an entry URL was supplied, load the associated entry
     */
    if(isset($url))
    {
        $sql = "SELECT id, page, title, entry, created
                FROM entries
                WHERE url=?
                LIMIT 1";
        $stmt = $db->prepare($sql);
        $stmt->execute(array($url));

        // Save the returned entry array
        $e = $stmt->fetch();

        // Set the fulldisp flag for a single entry
        $fulldisp = 1;
    }

    /*
     * If no entry ID was supplied, load all entry titles for the page
     */
    else
    {
        $sql = "SELECT id, page, title, entry, url, created
                FROM entries
                WHERE page=?
                ORDER BY created DESC";
        $stmt = $db->prepare($sql);
        $stmt->execute(array($page));
    
        $e = NULL; // Declare the variable to avoid errors

        // Loop through returned results and store as an array
        while($row = $stmt->fetch()) {
            if($page=='blog')
            {
                $e[] = $row;
                $fulldisp = 0;
            }
            else
            {
                $e = $row;
                $fulldisp = 1;
            }
        }

        /*
         * If no entries were returned, display a default
         * message and set the fulldisp flag to display a
         * single entry
         */
        if(!is_array($e))
        {
            $fulldisp = 1;
            $e = array(
                'title' => 'No Entries Yet',
                'entry' => 'This page does not have an entry yet!'
            );
        }
    }

    // Add the $fulldisp flag to the end of the array
    array_push($e, $fulldisp);

    return $e;
}

function confirmDelete($db, $url)
{
	$e = retrieveEntries($db, '', $url);

	return <<<FORM
<form action="/simple_blog/admin.php" method="post">
	<fieldset>
		<legend>Are You Sure?</legend>
		<p>Are you sure you want to delete the entry
		"$e[title]"?
		</p>
		<input type="submit" name="submit" value="Yes" />
		<input type="submit" name="submit" value="No" />
		<input type="hidden" name="action" value="delete" />
		<input type="hidden" name="url" value="$url" />
	</fieldset>
</form>
FORM;
}

function deleteEntry($db, $url)
{
    $sql = "DELETE FROM entries
            WHERE url=?
            LIMIT 1";
    $stmt = $db->prepare($sql);
    return $stmt->execute(array($url));
}

function adminLinks($page, $url)
{
    // Format the link to be followed for each option
    $editURL = "/simple_blog/admin/$page/$url";
    $deleteURL = "/simple_blog/admin/delete/$url";

    // Make a hyperlink and add it to an array
    $admin['edit'] = "<a href=\"$editURL\">edit</a>";
    $admin['delete'] = "<a href=\"$deleteURL\">delete</a>";

    return $admin;
}

function sanitizeData($data)
{
    if(!is_array($data))
    {
        return strip_tags($data, "<a>");
    }
    else
    {
        return array_map('sanitizeData', $data);
    }
}

function makeUrl($title)
{
    $patterns = array(
        '/\s+/',
        '/(?!-)\W+/'
    );
    $replacements = array('-', '');
    return preg_replace($patterns, $replacements, strtolower($title));
}

function createUserForm()
{
	return <<<FORM
<form action="/simple_blog/inc/update.inc.php" method="post">
	<fieldset>
		<legend>Create a New Administrator</legend>
		<label>Username
			<input type="text" name="username" maxlength="75" />
		</label>
		<label>Password
			<input type="password" name="password" />
		</label>
		<input type="submit" name="submit" value="Create" />
		<input type="submit" name="submit" value="Cancel" />
		<input type="hidden" name="action" value="createuser" />
	</fieldset>
</form>
FORM;
}

?>

Hi,

In functions.php -> retrieveEntries().

Before the statement

return $e;

,
use the following code to do the debug.

var_dump($e);

You are getting the error coz the $e returned from the function is not an array.
Also try to add some echo statements in the if{} else{} conditions in the function to check if you are getting the correct data at each step.

Hope this helps.

Thanks for the response, unfortunately that didn't solve the problem but actually caused the page to now load with this:

array(6) { [0]=> array(12) { ["id"]=> string(2) "14" [0]=> string(2) "14" ["page"]=> string(4) "blog" [1]=> string(4) "blog" ["title"]=> string(11) "Sixth Entry" [2]=> string(11) "Sixth Entry" ["entry"]=> string(13) "More entries!" [3]=> string(13) "More entries!" ["url"]=> string(11) "sixth-entry" [4]=> string(11) "sixth-entry" ["created"]=> string(19) "2011-11-21 17:47:28" [5]=> string(19) "2011-11-21 17:47:28" } [1]=> array(12) { ["id"]=> string(2) "13" [0]=> string(2) "13" ["page"]=> string(4) "blog" [1]=> string(4) "blog" ["title"]=> string(11) "Fifth Entry" [2]=> string(11) "Fifth Entry" ["entry"]=> string(21) "Testing another entry" [3]=> string(21) "Testing another entry" ["url"]=> string(11) "fifth-entry" [4]=> string(11) "fifth-entry" ["created"]=> string(19) "2011-11-21 17:46:01" [5]=> string(19) "2011-11-21 17:46:01" } [2]=> array(12) { ["id"]=> string(2) "12" [0]=> string(2) "12" ["page"]=> string(4) "blog" [1]=> string(4) "blog" ["title"]=> string(11) "Forth Entry" [2]=> string(11) "Forth Entry" ["entry"]=> string(82) "Another day, another entry!!! Editing today!! Editing now logged in as the admin." [3]=> string(82) "Another day, another entry!!! Editing today!! Editing now logged in as the admin." ["url"]=> string(11) "forth-entry" [4]=> string(11) "forth-entry" ["created"]=> string(19) "2011-11-15 12:20:20" [5]=> string(19) "2011-11-15 12:20:20" } [3]=> array(12) { ["id"]=> string(2) "11" [0]=> string(2) "11" ["page"]=> string(4) "blog" [1]=> string(4) "blog" ["title"]=> string(11) "Third Entry" [2]=> string(11) "Third Entry" ["entry"]=> string(28) "Third time is the charm :-)!" [3]=> string(28) "Third time is the charm :-)!" ["url"]=> string(11) "third-entry" [4]=> string(11) "third-entry" ["created"]=> string(19) "2011-11-15 12:17:09" [5]=> string(19) "2011-11-15 12:17:09" } [4]=> array(12) { ["id"]=> string(1) "3" [0]=> string(1) "3" ["page"]=> string(4) "blog" [1]=> string(4) "blog" ["title"]=> string(12) "Second Entry" [2]=> string(12) "Second Entry" ["entry"]=> string(31) "Trying again yet another entry!" [3]=> string(31) "Trying again yet another entry!" ["url"]=> string(12) "second-entry" [4]=> string(12) "second-entry" ["created"]=> string(19) "2011-11-07 18:45:48" [5]=> string(19) "2011-11-07 18:45:48" } [5]=> array(12) { ["id"]=> string(1) "2" [0]=> string(1) "2" ["page"]=> string(4) "blog" [1]=> string(4) "blog" ["title"]=> string(11) "First Entry" [2]=> string(11) "First Entry" ["entry"]=> string(37) "This is going to be a long project!!!" [3]=> string(37) "This is going to be a long project!!!" ["url"]=> string(11) "first-entry" [4]=> string(11) "first-entry" ["created"]=> string(19) "2011-11-06 11:09:58" [5]=> string(19) "2011-11-06 11:09:58" } }

Should I add if{} else{} statements around this part in functions.inc.php:

array_push($e, $fulldisp);

    return $e;

Or where do you suggest adding them?

Hmm, that is strange because the error says that the parameter1 you are passing to array_push() is not an array but boolean. Even the other errors concerning to array_pop() and foreach() statement convey the same.

I'm getting the same problem. Must be an error in the code in the book. Can anyone shed any light on this? I'm very stuck thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.