Jump to content

refine search results


joshgarrod

Recommended Posts

hello all. i have a poblem, i have a bar down the side of my website and it shows all of the different makes from my classified adverts and how many of them there are. the user then clicks on link and it should then only show the results matching that description. it knows how many there are when it loads the search page, however, it doesn't show any results. here is my code:

 

<?php

  // Get the search variable from URL
  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=100; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database 
mysql_connect("ipaddr","usr","pass"); //(host, username, password)

//specify database
mysql_select_db("db") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query = "select * from classifieds where make like \"%$trimmed%\"  
  order by make"; 

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

if ($numrows == 0)
  {
  echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";

  }

// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: "" . $var . "", there are $numrows caravans matching this criteria</p>";

while ($row= mysql_fetch_array($result))
		{ 
		$ref = $row["ref"];
		$make = $row["make"];
		$model = $row["model"];
		$year = $row["year"];
		$berth = $row["berth"];
		$weightun = $row["weightun"];
		$weightla = $row["weightla"];
		$lengthin = $row["lengthin"];
		$lengthex = $row["lengthex"];
		$image1 = $row["image1"];
		$price = number_format($row["price"]); //formatting the price field to use a thousand separator
		echo '$berth';
			echo "<div id=\"caravan_row\">";
				echo "<div id=\"logo\"><a href='view_used_caravan.php?ref=$ref'><img src=\"images/$make.gif\" border=\"0\"  alt=\"$make caravan\" title=\"$make caravan\" width=\"66\" height=\"83\"/></a></div>";
				if ($image1 == "uploads/") {
				echo "<div id=\"image\"><a href=\"view_used_caravan.php?ref=$ref\"><img src=\"images/icon_no_photo.gif\" alt=\"$make $model $year\" width=\"100\" height=\"75\" border=\"0\" /></a></div>";
				}
				else {
				echo "<div id=\"image\"><a href='view_used_caravan.php?ref=$ref'><img src=\"$image1\" border=\"0\" alt=\"$make $model $year\" width=\"100\" height=\"75\" title=\"$make $model $year\" /></a></div>";
				}
				echo "<div id=\"specifications\">";
					echo "<div id=\"make\"><h1></strong>$make $model</h1></div>";
					echo "<div id=\"price\"><span class=\"price\">£$price</span></div>";
					echo "<div id=\"more\"><a href='view_used_caravan.php?ref=$ref' title=\"More info on the $make $model $year \">More info>></a></div>";
					echo "<div id=\"year\"><strong>Year: </strong>$year</div>";
					echo "<div id=\"weightun\"><strong>MIRO: </strong>$weightun kg</div>";
					echo "<div id=\"berth\"><img src=\"images/$berth.gif\" class=\"berth\" />$berth berth</div>";
					echo "<div id=\"weightla\"><strong>MTPLM: </strong>$weightla kg</div>";
					echo "<div id=\"lengthex\"><strong>Shipping length: </strong>$lengthex ft</div>";	
					echo "<div id=\"lengthex\"><strong>Location: </strong>$county</div>";						
				echo "</div>";
			echo "</div>";
		}

?>

 

many thanks in advance

Link to comment
Share on other sites

You can just run the SQL once -

SELECT * FROM classified WHERE make LIKE '%{$trimmed}%' ORDER BY make LIMIT {$s}, {$limit};

 

That will do the same thing except you don't have to run a SQL twice. Check to make sure the SQL returned results before the while loop. Same as you did in the first case. If you use my query up there, check to make sure it returns at least 1 row.

 

 

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.