0

Hi all,

I have just made a search bar for a site but I am having trouble trying to return the number of results that are found.

E.g. I want it to print "Your search terms returned 'xx' results" after each search.

This is my code:

<?php
include ('db.php');

if (isset($_POST['search'])) {

	$searchterm = $_POST['searchterm'];
	$searchcat = $_POST['searchcat'];
	$searchterm = trim($searchterm);

	if (!$searchterm && !$searchcat) {
		echo 'no search terms entered';
		exit;
	}

        $getresults = mysql_query("SELECT * from auctions WHERE a_title LIKE '%".$searchterm."%' OR category_id = '$catid'");	

	$i=0;
	while ($i < 10 and $matches = mysql_fetch_assoc($getresults)) {
               //return number of matches &
               //display results here...
        }
?>

Thanks, Nonshatter

Edited by nonshatter: n/a

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Virtualbase
0

you may add following code between line no 16 and 18

$countofrecs=mysql_query("SELECT count(*) from auctions WHERE a_title LIKE '%".$searchterm."%' OR category_id = '$catid'");

$cnt=mysql_fetch_assoc($countofrecs) ;

echo "Your search terms returned '{$cnt[0]}' results";

Edited by urtrivedi: n/a

0

Hey, thanks for the rapid reply. I'm still not getting the correct result though...
It only echoes "Your search terms returned '' results". It doesn't want to print out whatever is in {$cnt[0]} Any thoughts?
Thanks again

0

User mysql_fetch_array() instead of mysql_fetch_assoc() in line no 4 of my code

Edited by urtrivedi: n/a

0

$countofrecs=mysql_query("SELECT count(*) from auctions WHERE a_title LIKE '%".$searchterm."%' OR category_id = '$catid'");
$cnt=mysql_fetch_array($countofrecs) ;
echo "Your search terms returned ".mysql_num_rows($cnt)." results";

I hope that'll help.

Thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.