Jump to content

return result counting


lucan

Recommended Posts

I am using this code to search my database plus it has a paginate which all works fine. The problem is i have a row count which for some reason counts all records in the database and not just the ones returned from the search query. How to i fix this.

 


$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 WHERE productname like '%$term%' and category like '%$cat%'"));

//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(*) FROM table");


list($number) = mysql_fetch_row($sql);

if (!mysql_num_rows($result)) {  
die("NO RECORD FOUND") ;
exit;
} else {
echo "Your Search for: <b>$term</b> in <b>$cat</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='ttr.php?start=$x'>$i</a> ";
	   else
	   echo "<a href='ttr.php?start=$x'><b>$i</b></a> ";
	   $i++;
	}

 //show next button 

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

?>

Link to comment
https://forums.phpfreaks.com/topic/248769-return-result-counting/
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.