[solved] wildcard use maybe?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

[solved] wildcard use maybe?

Post 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 !!)
Last edited by tom.cull on Wed Sep 14, 2005 3:08 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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'];
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

Post 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.
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post 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)
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

Post by tom.cull »

thanks - works like a treat!
Post Reply