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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.