Jump to content

Search Form Variable Help


aviatorisu

Recommended Posts

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:

[code]<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>[/code]
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
Link to comment
Share on other sites

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:

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

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
Link to comment
Share on other sites

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

[code]$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);[/code]
Link to comment
Share on other sites

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:


[code]<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>
        [/code]

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

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

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

~Z~
Link to comment
Share on other sites

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 ...

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.