Page 1 of 1

[solved] wildcard use maybe?

Posted: Wed Sep 14, 2005 1:56 pm
by tom.cull
Hi,

I have a search form for my db - I have various option values for my drop down list. I can get the search to work on teh db when i select from the options however I want at the top of the list a value "All" which will list all.

Currently I am trying to use a wildcard but it doesnt work! Any ideas? Thanks

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p>Car Test - User Quick Search</p>
<form enctype="multipart/form-data" method="post"  action="<?php echo $PHP_SELF ?>">
  <p>Location 
    <select name="location_subarea" size="1">
      <option value="*">ALL</option>
      <option value="South East - London">South East - London</option>
      <option value="North West - Manchester">North West - Manchester</option>
      <option value="South East - Kent">South East - Kent</option>
    </select>
  </p>
  <p>Price Range
    <select name="car_pbracket" size="1">
      <option value="*">ALL</option>
      <option value="0-500">0-500</option>
      <option value="500-1000">500-1000</option>
      <option value="1000-1500">1000-1500</option>
    </select>
	<br>
	<input type="submit" name="submit" value="Search">
  </p>
</form>
<p>&nbsp;</p>

Code: Select all

<?php

if ($submit) {

$username="xxxx_xxxx";
$password="xxxx";
$database="xxxx_xxxx";

$location_subarea=$_POST['location_subarea'];
$car_pbracket=$_POST['car_pbracket'];

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");


$result = mysql_query("SELECT * FROM newsstuf_usedcars
WHERE location_subarea='$location_subarea' AND car_pbracket='$car_pbracket'") or die(mysql_error());

$row = mysql_fetch_array( $result ); 

echo $query;

echo( "<a href=\"editdetails.php?id=".$row['id']."\">".$row['id']." . ".$row['contact_nfirst']." ".$row['contact_nlast']." - ".$row['car_make']."</a><br />" ); 




}

mysql_close();
?>


</body>
</html>
(also as a side - I need it to list more than one if there is more than one result !!)

Posted: Wed Sep 14, 2005 2:31 pm
by s.dot
To search all of them, just leave out a WHERE clause in your query.

To display more than one result

Code: Select all

while($row = mysql_fetch_array($result))
{
  echo $row['something'];
}

Posted: Wed Sep 14, 2005 2:33 pm
by tom.cull
that wont work tho as i need the WHERE in there for when the user selects something from the list other than ALL.

Posted: Wed Sep 14, 2005 2:58 pm
by ryanlwh
you can do this

Code: Select all

if ($location_subarea == '*') $location_subarea = "location_subarea";
else $location_subarea = "'$location_subarea'"; // notice the single quotes inside the double quotes

// ** Do the same for car_pbracket

$result = mysql_query("SELECT * FROM newsstuf_usedcars
WHERE location_subarea=$location_subarea AND car_pbracket=$car_pbracket") or die(mysql_error());  // here just remove the single quotes around the variables (as we've already added them in the if statement)

Posted: Wed Sep 14, 2005 3:08 pm
by tom.cull
thanks - works like a treat!