aviatorisu Posted April 29, 2006 Share Posted April 29, 2006 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 Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted April 29, 2006 Share Posted April 29, 2006 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 Quote Link to comment Share on other sites More sharing options...
aviatorisu Posted April 29, 2006 Author Share Posted April 29, 2006 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~ Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted April 29, 2006 Share Posted April 29, 2006 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] Quote Link to comment Share on other sites More sharing options...
aviatorisu Posted April 29, 2006 Author Share Posted April 29, 2006 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 $priceAND 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~ Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted April 30, 2006 Share Posted April 30, 2006 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 ... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.