Jump to content

Table search issues


joshgarrod

Recommended Posts

Hi all, a have a small problem with my search script. At the moment I am searching a field in my tabel called searchtags, now if I enter a one word search term of a string of characters it will return the results but if they are not in the same order it won't.

 

Example:

 

searchtags contains - Ford Mondeo 1999 2.0L petrol car

 

If I search "Ford Mondeo" the query returns results, whereas if I search "Mondeo Ford" the query returns no results?

 

I assume I have to expand the search in some way but I don;t know how, any ideas? Thanks in advance

 

 

<?php

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

if ($field == "") { //if search from homepage start here

$query = "select * from fleet where searchtags like \"%$trimmed%\"  
  order by fleetref"; // EDIT HERE and specify your table and field names for the SQL query

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


if ($numrows == 0)
  {
  echo "<p>Sorry, your search: "" . $trimmed . "" motorhomes to hire 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 "<h1>You searched for - " . $var . "cars</h1>";

// now you can display the results returned
  while ($row= mysql_fetch_array($result))
  { 
  $fleetref = $row['fleetref'];
  $fleetmake = $row['fleetmake'];
  $fleetmodel = $row['fleetmodel'];
  $fleetyear = $row['fleetyear'];
  $fleetchassis = $row['fleetchassis'];
  $fleetberth = $row['fleetberth'];
  $fleetlayout = $row['fleetlayout'];
  $fleetlength = $row['fleetlength'];
  $fleettype = $row['fleettype'];
  $fleetof = $row['fleetof'];
  $fleetimage1 = $row['fleetimage1'];
  $fleetimage2 = $row['fleetimage2'];
  $fleetimage3 = $row['fleetimage3'];
  $fleetimage4 = $row['fleetimage4'];
  $fleetadded = $row['fleetadded'];
  $fleetengine = $row['fleetengine'];
  $pricelow = $row['pricelow'];
  $pricemid = $row['pricemid'];
  $pricehigh = $row['pricehigh'];
  $fleetviews = $row['fleetviews'];
  
  $query = mysql_query("SELECT * FROM mhhire WHERE `ref` = '$fleetof'");
			  
			  if(mysql_num_rows($query)==0){

				echo "<p>This company does not exsist</p>";

				  }else{
					  while($details = mysql_fetch_array($query)){
					  $company = $details ['company'];
					  $location = $details ['location'];
  
			echo "<div class=\"fleet_vehicle\">";

						if ($fleetimage1 == "fleet/") {
						echo "<img src=\"images/no_image.jpg\" alt=\"$fleetmake $fleetmodel\" width=\"100\" height=\"75\" class=\"fleet_thumb\">";
						}
						else {
						echo "<img src=\"$fleetimage1\" alt=\"$fleetmake $fleetmodel\" width=\"100\" height=\"75\" class=\"fleet_thumb\">";
						}
						echo "<div class=\"fleet_title\">$fleetmake $fleetmodel $fleetyear</div>";
						echo "<div class=\"fleet_berth\"><img src=\"images/$fleetberth.gif\" alt=\"$fleetberth berth\"></div>";
						echo "<div class=\"fleet_details_in\"><div class=\"fleet_details\">- $fleetberth berth<br />- $fleetlayout<br /></div></div>";
						echo "<div class=\"fleet_details_in\"><div class=\"fleet_details\">- $fleetchassis<br />- $fleetengine<br /></div></div>";
						echo "<div class=\"fleet_details_in\"><div class=\"fleet_details\">- $fleetlength meters<br />- $fleettype<br /></div></div>";
						echo "<div class=\"fleet_options\">From <strong>$company</strong> in <strong>$location</strong> - <a href=\"view_motorhome_for_hire.php?ref=$fleetref\">More info >></a></div>";

						echo "</div>";

		}}
  }

} ?>

Link to comment
Share on other sites

Using LIKE doesn't allow you to search by relevance. It will only return a result if the specified string exists in the field. 'Ford Mondeo' may exist, but unless the record is 'Ford Mondeo Ford', the query won't return a result for LIKE %Mondeo Ford%. You may want to look into using FULLTEXT searches.

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.