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

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";

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

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

Beautiful... Consider it solved!

$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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.