agge Posted March 28, 2007 Share Posted March 28, 2007 I have two table T1 and T2 And I have a form where user fill with data what he is searching for. With one and two querys it is no problem, then I can use a simple IF like this if (!empty (value1) || (value2) { code .... } if (empty(value2) { code for value1.. } else { code for value2.. } but then I have 3 more value I can search for, value3, value4, value5 wich could by depending on some of the other values, and there is my problem I don't have a formula so I can set this up to work. example: Imagine that user search for country for <input name="country"> then the select show everything for country=country. user search for name and country, then the select show everything where country = country and name = name user search for country, name and state, user search for country, name, state, city user search for city it get many IF:s to counting in all possibilities, I can get all values from the selects but I can not write this code formula in php, so it works and not selecting wrong IF... I use php and mysql, and I send the querys through url like http://localhost/country=USA&state=Texas etc. How is the right way of doing this "search engine" any good hints for this should make me happy. Link to comment https://forums.phpfreaks.com/topic/44707-using-a-form-for-different-searching-in-my-db/ Share on other sites More sharing options...
pocobueno1388 Posted March 28, 2007 Share Posted March 28, 2007 Here is a search script I use on my site: <?php if ($_POST['search']){ //Start out by selecting all the fields from that table $query = "SELECT acreID, ownedby, indistrict, whatisbuilt, price FROM land WHERE 1"; //Now this is how you will set your IF statements...you willl use GET obviosly if ($_POST['acreID']) $query .= " AND acreID='{$_POST['acreID']}'"; if ($_POST['ownerID']) $query .= " AND ownedby='{$_POST['ownerID']}'"; if ($_POST['built']) $query .= " AND whatisbuilt='{$_POST['built']}'"; if ($_POST['max']) $query .= " AND price <= '{$_POST['max']}'"; $result = mysql_query($query)or die (mysql_error()); //Now display all the data echo " <table width=600 class=tstyle6><tr> <td class=tstyle2><center> S E A R C H </td></tr><tr> <td><center> <table id=menue class=tstyle5 width=\"100%\"><tr> <td><center><font color=2A305A>$num results found<p></font></table id=topic></td></tr><tr><td><center><br> <table class=tstyle1 width=500><tr> <tr class=tstyle3 align='center'> <td class=tstyle3>Acre ID</td> <td class=tstyle3>Owner ID</td> <td class=tstyle3>District</td> <td class=tstyle3>Built</td> <td class=tstyle3>Price</td> <tr align='center'>"; while (list($acreID, $owner, $district, $built, $price)=mysql_fetch_row($result)){ echo "<td class=tstyle5><a href='acre.php?acreID=$acreID'>#$acreID</a></td>"; echo "<td class=tstyle5><a href='userprofiles.php?id=$owner'>#$owner</a></td>"; echo "<td class=tstyle5>$district</td>"; echo "<td class=tstyle5>$built</td>"; echo "<td class=tstyle5>$$price</td>"; echo "<tr class=tstyle5 align='center'>"; } print "</tr></table></table>"; include 'footer.php'; exit; } ?> Link to comment https://forums.phpfreaks.com/topic/44707-using-a-form-for-different-searching-in-my-db/#findComment-217074 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.