Can someone please tell me what I am doing wrong. I continue getting an undefined variable notice when "bar" is define in my page. Also the Previous and Next (page) is not working because bar is undefined. Previous and Next is only shown as text and nothing else
Notice: Undefined variable: bar in C:\wamp\www\recipe\showrecipe.inc.php on line 111
GoTo: PreviousNext
<?php
$con = mysql_connect("localhost", "test", "test") or die('Could not connect to server');
mysql_select_db("recipe", $con) or die('Could not connect to database');
$recipeid = $_GET['id'];
$query = "SELECT title,poster,shortdesc,ingredients,directions from recipes where recipeid = $recipeid";
$result = mysql_query($query) or die('Could not find recipe');
$row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved');
$title = $row['title'];
$poster = $row['poster'];
$shortdesc = $row['shortdesc'];
$ingredients = $row['ingredients'];
$directions = $row['directions'];
$ingredients = nl2br($ingredients);
$directions = nl2br($directions);
echo "<h2>$title</h2>\n";
echo "by $poster <br><br>\n";
echo $shortdesc . "<br><br>\n";
echo "<h3>Ingredients:</h3>\n";
echo $ingredients . "<br><br>\n";
echo "<h3>Directions:</h3>\n";
echo $directions . "\n";
echo "<br><br>\n";
$query = "SELECT count(commentid) from comments where recipeid = $recipeid";
$result = mysql_query($query);
$row=mysql_fetch_array($result);
if ($row[0] == 0)
{
echo "No comments posted yet. \n";
echo "<a href=\"index.php?content=newcomment&id=$recipeid\">Add a comment</a>\n";
echo " <a href=\"print.php?id=$recipeid\" target=\"_blank\">Print recipe</a>\n";
echo "<hr>\n";
} else
{
$totrecords = $row[0];
echo $row[0] . "\n";
echo " comments posted. \n";
echo "<img src=\"images/img12.gif\">";
echo "<a href=\"index.php?content=newcomment&id=$recipeid\">Add a comment</a>\n";
echo "<img src=\"images/img11.gif\">";
echo " <a href=\"print.php?id=$recipeid\" target=\"_blank\">Print recipe</a>\n";
echo "<hr>\n";
echo "<h2>Comments:</h2>\n";
if (!isset($_GET['page']))
$thispage = 1;
else
$thispage = $_GET['page'];
$recordsperpage = 5;
$offset = ($thispage - 1) * $recordsperpage;
$totpages = ceil($totrecords / $recordsperpage);
$query = "SELECT date,poster,comment from comments where recipeid = $recipeid order by commentid desc limit $offset,$recordsperpage";
$result = mysql_query($query) or die('Could not retrieve comments');
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$date = $row['date'];
$poster = $row['poster'];
$comment = $row['comment'];
$comment = nl2br($comment);
echo $date . " - posted by " . $poster . "\n";
echo "<br>\n";
echo $comment . "\n";
echo "<br><br>\n";
}
if ($thispage > 1)
{
$page = $thispage - 1;
$prevpage = "<a href=\"index.php?content=showrecipe&id=$recipeid&page=$page\">Previous</a> ";
} else
{
$prevpage = "Previous";
}
if ($totpages > 1)
{
$bar = '';
for($page = 1; $page <= $totpages; $page++)
{
if ($page == $thispage)
{
$bar .= " $page ";
} else
{
$bar .= " <a href=\"index.php?content=showrecipe&id=$recipeid&page=$page\">$page</a> ";
}
}
}
if ($thispage < $totpages)
{
$page = $thispage + 1;
$nextpage = " <a href=\"index.php?content=showrecipe&id=$recipeid&page=$page\">Next</a>";
} else
{
$nextpage = "Next";
}
echo "GoTo: " . $prevpage . $bar . $nextpage;
}
?>
The last part is where it stay the variable bar is undefined which is line 111
echo "GoTo: " . $prevpage . $bar . $nextpage;