Jump to content

paginate problem


lucan

Recommended Posts

I am using the following search code with a paginate. the code works fine but how do i hide the paginate if the search results are less than the default settings

 



mysql_connect ("localhost", "user","pword")  or die (mysql_error());
mysql_select_db ("database"); 

$term = $_GET['term']; 
$cat = $_GET['cat'];

//max display per page 
$per_page = 4;

//get start variable 
$start = $_GET['start'];

// cout records 
$record_count = mysql_num_rows(mysql_query("SELECT * FROM table"));

//count max pages 
$max_pages = $record_count / $per_page; //may count as decimal

if (!$start)
      $start =0;
  	  

$sql="SELECT * FROM table WHERE productname like '%$term%' and category like '%$cat%' LIMIT $start, $per_page"; 
$result = mysql_query($sql);

$sql = mysql_query("SELECT COUNT(*) as total FROM table");
list($number) = mysql_fetch_row($sql);

if (!mysql_num_rows($result)) {  
die("NO RECORD FOUND") ;
exit;
} else {
echo "Your matches for: <b>$term</b><br><br>"; 
}
while ($row = mysql_fetch_array($result)){ 
$num_rows = mysql_num_rows($number);
if (!$i++) echo "Total records $number";

?>

<br /> 
<br />  
<?php echo 'ID: '.$row['id'];  ?>  <br />  
<?php echo '<br/> Product Name: '.$row['productname'];?>  <br />  
<?php echo '<br/> Price: '.$row['price']; ?><br />  
<?php echo '<br/> category: '.$row['category']; ?>
  
<br />  
<?php
}
?>


<?php

//setup prev and next variables	 
$prev = $start - $per_page;
 $next = $start + $per_page;

 // show prev button 	 
 if(!($start<=0))
 	echo " <a href='ttr.php?start=$prev'>Prev </a>";

	//set var for first page
	$i=1;

	for ($x=0;$x<$record_count;$x=$x+$per_page)
	{
	 if ($start!=$x)
	   echo "<a href='page.php?start=$x'>$i</a> ";
	   else
	   echo "<a href='page.php?start=$x'><b>$i</b></a> ";
	   $i++;
	}

 //show next button 

 if (!($start>=$record_count-$per_page))
 	echo " <a href='page.php?start=$next'>Next</a>";

?>

Link to comment
https://forums.phpfreaks.com/topic/248522-paginate-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.