Jump to content


Photo

Pagination Problem


  • Please log in to reply
No replies to this topic

#1 centenial

centenial
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 21 July 2006 - 11:11 PM

Hi,

I'm having trouble with this pagination script. It's supposed to display 8 properties per page, with links to prev & next pages, however it only shows 6... and the previous/next links don't work with the DB Query.

<?php

$method = $_GET['method'];
if ($method == "list") {
	echo '	List Categories Here...';
} else {
	  include 'admin/includes/config.php';
	  $category = $_GET['category'];
	
	  if(!isset($_GET['page'])){
	      $page = 1;
	  } else {
	      $page = $_GET['page'];
	  }
	  
	  // Define the number of results per page
	  $max_results = 8;
	  
	  // Figure out the limit for the query based
	  // on the current page number.
	  $from = (($page * $max_results) - $max_results);
	  
	  // Perform MySQL query on only the current page number's results
	  $sql = "select l.id, l.l_id, l.state, l.price, l.s_desc, l.picture1, t.name As town from listings l 
	INNER JOIN towns t ON t.id = l.t_id
	WHERE l.c_id = '$category' AND status = '1'
	ORDER BY l.id DESC
	LIMIT $from, $max_results";
	  
	  $result = mysql_query($sql) or die(mysql_error());
	  

	if (mysql_num_rows($result) <= 0) {
		echo "We're sorry, there are currently no properties listed in this category. Please check back soon!";
	} else {				
		while($row = mysql_fetch_array($result)){
		      // Build your formatted results here.
		          $id=$row["id"];
		          $l_id=$row["l_id"]; 
		          $t_id=$row["town"]; 
		          $state=$row["state"]; 
		          $price=$row["price"]; 
		          $s_desc=$row["s_desc"];
		          $picture1=$row["picture1"];
		  
		  //results
		  
		echo "
		<div style='margin-top: .5cm; width: 95%;'>
			<a href='detail.php?id=$id'><img src='admin/$picture1' width='175' border='0' align='left' hspace='4'></a>
			<b>$t_id, $state</b><br />
			<b>Listing ID:</b> $l_id<br />
			<b>Price:</b> $price<br />
			<br />
			<div style='font-size: 9pt;'>
				$s_desc <a href='detail.php?id=$id'>
				More...</a></div>
		</div>";
		          }
		  
		  
		  // Figure out the total number of results in DB:
		  $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM listings"),0);
		  
		  // Figure out the total number of pages. Always round up using ceil()
		  $total_pages = ceil($total_results / $max_results);
		  
		  // Page Number Hyperlinks
		  echo "<br /><br />Page #: ";
		  
		  // Previous Link
		  if($page > 1){
		      $prev = ($page - 1);
		      echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">&lt;&lt; Previous</a>&nbsp;&nbsp;";
		  }
		  
		  for($i = 1; $i <= $total_pages; $i++){
		  if(($page) == $i){
		  echo "$i";
		  } else {
		  echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$i&category=$category&name=$name\">$i</a> ";
		  }
		  if(($i + 30) % 30 == 0)
		  {
		  echo '';
		  }
		  }
		  
		  // Next Link
		  if($page < $total_pages){
		      $next = ($page + 1);
		      echo "&nbsp;&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next &gt;&gt;</a>";
		}
	}
}
?>
Can anybody help?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users