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;
}
?>