Jump to content


Photo

Search Form Variable Help


  • Please log in to reply
5 replies to this topic

#1 aviatorisu

aviatorisu
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 29 April 2006 - 06:31 PM

I've recently just started learning how PHP works, and I've made pretty good leaps and bounds I think, but I'm stuck.

I'm developing a webpage for people to search for places to go within my city, and I've decided to start with a simple form and go from there.

Here is a portion of the code involved:

<TR><TD ALIGN=LEFT><FONT FACE="verdana" SIZE="2"><B>Price:</B></FONT></TD></TR>
<TR><TD><FONT SIZE="1"><SELECT NAME="price" width="310">
<OPTION selected value="">Search All
<OPTION VALUE="">------------------------------------</option>

<OPTION VALUE="1">$</option>
<OPTION VALUE="2">$$</option>
<OPTION VALUE="3">$$$</option>
<OPTION VALUE="4">$$$$</option>
This portion of the search form is obviously to select a place to go based on price. I can make the search work for values of 1 thru 4, but my problem is, what value do I plug in to return a "Search All" query?

Please let me know if you require more information.

Thanks in advance,

Zachary

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 29 April 2006 - 07:06 PM

If I was doing a mysql query, I'd set on of your option values to blank

<option value=''>search all</option>

then on the backend, the mysql query might look like:

$query = "SELECT * FROM `table`";
if($_POST['price']) $query.=" WHERE `price` <= '".$_POST['price']."'";
$result=mysql_query($query);

you might have to test it... the if($_POST['price']) should only fire if $_POST['price'] has a value.

hope that's what you're asking

#3 aviatorisu

aviatorisu
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 29 April 2006 - 07:30 PM

I think I understand what you are saying, but suppose I have an additional 2 or 3 other menu options...how would that change the query?

~Z~

#4 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 29 April 2006 - 07:36 PM

no worries -- just a separate line for each piece. (you could use a do while loop, if this is too 'manual')

$query = "SELECT * FROM `table`";
if($_POST) {
   $query.= " WHERE 1 "
if($_POST['price']) $query.=" AND `price` <= '".$_POST['price']."'";
if($_POST['location']) $query.=" AND `location` LIKE '%".str_replace(" ","%",$_POST['location'])."%'"
// add your other criteria here
$result=mysql_query($query);


#5 aviatorisu

aviatorisu
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 29 April 2006 - 08:09 PM

Okay, I gave that a shot and totally screwed it up. Let me post the entire form here...maybe that will change something, maybe not:


<TR><TD><FONT SIZE="1"><SELECT NAME="gowhere" width="310">
        <OPTION VALUE="">Search All
        <option value="">------------------------------------</option>

        <option value="RESTAURANT">Restaurant</option>
        <option value="CLUB">Club</option>
        <option value="SHOWS">Show</option>
        <option value="CASINO">Casino</option>
        

        <TR><TD><FONT SIZE="1"><SELECT NAME="loc" width="310">
        <OPTION selected VALUE="">Search All
        <OPTION VALUE="">------------------------------------</option>

        <OPTION VALUE="1">The Strip</option>
        <OPTION VALUE="2">North/North Las Vegas</option>
        <OPTION VALUE="3">East</option>
        <OPTION VALUE="4">West/Summerlin</option>
        <OPTION VALUE="5">South</option>
        <OPTION VALUE="6">Henderson</option>
        <OPTION VALUE="7">Boulder City</option>
        

        <TR><TD><FONT SIZE="1"><SELECT NAME="price" width="310">
        <OPTION selected value="">Search All
        <OPTION VALUE="">------------------------------------</option>

        <OPTION VALUE="1">$</option>
        <OPTION VALUE="2">$$</option>
        <OPTION VALUE="3">$$$</option>
        <OPTION VALUE="4">$$$$</option>
        

Here is the QUERY script that I'm working with as well that makes it work with a keyword option as well:

$query = "select * from places where location LIKE $loc AND price LIKE $price
AND name LIKE \"%$trimmed%\"
  order by id";

As I said, I tried the suggested code, but somehow I screwed it up...any other thoughts?

~Z~

#6 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 30 April 2006 - 01:10 AM

depends on the error.

Generally, whatever your <select> name is, will come across in a $_POST variable... sooo...

$query = "SELECT * FROM `places` WHERE `location` LIKE ".$_POST['loc']." AND `price` LIKE '".$_POST['price']."' AND `name` LIKE '%".$_POST['trimmed']."%' ORDER BY `id`";

Now... the LIKE is usually surrounded by the %'s -- and I generally replace any spaces with a % too...

... WHERE `location` LIKE '%".str_replace(" ","%",$_POST['loc'])."%' AND ...






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users