PHP Paging

Hi All

I’ve got a problem I hope you could help you… I’m using paging to create a directory website. I can get it to show the category listings and the first 5 results on page one.

However if I click page 2 I get an error message saying, what I want to be able to do is when I click page 2 it shows me the next 5 results from the MYSQL database for that specific category.


Notice: Undefined variable: category_id in C:\\wamp\\www\\planningparties.co.uk\\directory\\pages.php on line 32
Error, query failed

I’ve shown the script I’m using below it’s taken me a few hours to get this far and I’m still struggling any help would be great.

Code is here:

<?php
  require_once('../admin/config.php');
  mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); 
  mysql_select_db(DB_DATABASE); 
  
  $result = mysql_query("SELECT * FROM directory_categories ORDER BY category_id");
	
while($row = mysql_fetch_array($result))
  {
  echo '<ul>';
  echo '<li><a href=pages.php?category_id='. $row['category_id']. '>' . $row['category_title'] .  "</a></li>";
  echo '</ul>';
  }
  
// Define some variables 
$resultsperpage = 5;

if (isset($_GET['category_id'])) {
	$category_id = $_GET['category_id'];
}

// Show the first page
$page_number = 1;

// if page is define show it as a page 
if (isset($_GET['page'])) {
	$page_number = $_GET['page'];
}

// Count the offset
$offset = ($page_number- 1) * $resultsperpage;
$query = " SELECT * FROM directory_adverts WHERE category_id =".$category_id."" .
         " LIMIT $offset, $resultsperpage";
$result = mysql_query($query) or die('Error, query failed');

// print the random numbers
while($row = mysql_fetch_array($result))
{
  echo '<div id="layout">
  <table width="600" border="0">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><a href=advert.php?advert_id='. $row['advert_id']. '/>' . $row['advert_title'] . '</a></td>
    <td>&nbsp;</td>
    <td>'.$row['advert_content'].'</td>
  </tr>
</table>
</div>';
}

// how many rows we have in database
$query   = "SELECT COUNT(advert_id) AS numrows FROM directory_adverts";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$resultsperpage);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav  = '';

for($page = 1; $page <= $maxPage; $page++)
{
   if ($page == $page_number)
   {
      $nav .= " $page "; // no need to create a link to current page
   }
   else
   {

	$nav .= " <a href=\\"$self?page=$page\\">$page</a> ";
   } 
}

echo $nav;



?>

Okay…
You need a default behavior for $category_id if no category_id is specified on the url line. Right now you’ve got a line to set it IF it’s a get variable, but if there isnt one, you’ve got nothing.


if (isset($_GET['category_id'])) { 
    $category_id = $_GET['category_id']; 
} else {
    $category_id = 1;
}

something like that.

Secondly: You need to add said category to the URL you’re outputting in the pagination links.

    $nav .= " <a href=\\"$self?page=$page&category_id=$category_id\\">$page</a> ";