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.&nbsp;&nbsp;\n";
   
   echo "<a href=\"index.php?content=newcomment&id=$recipeid\">Add a comment</a>\n";
   echo "&nbsp;&nbsp;&nbsp;<a href=\"print.php?id=$recipeid\" target=\"_blank\">Print recipe</a>\n";
   echo "<hr>\n";
} else 
{
   $totrecords = $row[0];
   echo $row[0] . "\n";
   echo "&nbsp;comments posted.&nbsp;&nbsp;\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 "&nbsp;&nbsp;&nbsp;<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;

apparently the way the code is sequenced, the variable did not get defined before it was used. Check thru your code where $bar is is defined and consider how that happened.

This is just a Notice. It's just saying that $bar is not defined anywhere, which isn't a very bad problem unless maybe you have register_globals on.

Soln..

1. Turn off notices in php.ini

OR

2. Include this code at the header of page.

<?php
    
	// Turn off all error reporting
	error_reporting(0);
	
	// Report simple running errors
	error_reporting(E_ERROR | E_WARNING | E_PARSE);
	
    ?>

Manish thank you very much that does the trick. But I found my problem. I am telling php that I want 5 record per page and since it is a test site it has 3 comments/record on a page giving me that undefined variable. When I put the record to 2 per page it work and look perfectly.

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.