only1perky Posted January 26, 2009 Share Posted January 26, 2009 Hi guys could someone please help me out before I go insane. I'm completely new to php so you'll have to be patient with my stupidity. My database contains a table with about 50 categories that are all either true or false (obviously marked as 1 or 0). I would like to search this table based on which checkboxes are ticked and return the results accordingly. I know I need to include a foreach in my query but I just can't get my head round what I need to do. Could someone please just write my query for me so I can move on and get confused with something else. Here is my form (I've only put 3 checkboxes to keep it short) Form: <form name="advancedsearch" action="search.php" method="post" > <input name="criteria[]" type="checkbox" value="realale" /> <input name="criteria[]" type="checkbox" value="fruitwine" /> <input name="criteria[]" type="checkbox" value="champagne" /> <input type="submit" name="submit" value="Submit"> </form> I also have this script which I've been using to search from a text box. Is it possible to use this code with the checkboxes? (Obviously a few alteration will be needed. <?php if(isset($_GET['search-d'])){ // Get the search variable from URL $var = @$_GET['search-d'] ; $trimmed = trim($var); //trim whitespace from the stored variable // rows to return $limit=10; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>You failed to enter a search parameter...</p>"; ?> <a href="javascript:history.go(-1)">Click here to search again</a> <?php }else { // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } // Build SQL Query mysql_select_db($database_connuser, $connuser) or die; $query = "select * from drinking WHERE " .$trimmed. " = 1 order by venue asc"; // EDIT HERE and specify your table and field names for the SQL query $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search for pubs serving "" . $trimmed . "" returned zero results</p>"; ?><?php // google echo "<p><a href=\"http://www.google.com/search?q=" . $trimmed . "\" target=\"_blank\" title=\"Look up " . $trimmed . " on Google\">Click here</a> to try the search on google</p>"; }else{ // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for pubs serving "" . $var . ""</p>"; // begin to show results set echo "";?><br/><? $count = 1 + $s ; // now you can display the results returned ?> <table width="100%" border="0" class="tabletext"> <tr> <td width="13"></td> <td width="60"></td> <td width="92"></td> <td width="127"></td> <td width="79"></td> </tr> <tr> <td colspan="4"><p><strong>Venue Name</strong></p></td> <td> </td> </tr> <?php while ($row= mysql_fetch_array($result)) { ?> <tr> <td colspan="4" bgcolor="#FFFFFF"> </td> <td bgcolor="#FFFFFF"> </td> </tr> <tr> <td colspan="4" bgcolor="#FFFFFF"><p><STRONG><?php echo $row['venue']; ?></STRONG><br> <strong>Address:</strong> <?php echo $row['address'];?>, <?php echo $row['area'];?></p></td> <td bgcolor="#FFFFFF"><p class="subhead"><a href="venuedetails.php?id=<?php echo $row['id']; ?>">More Info</a><br> <a href="http://www.multimap.com/maps/?qs=<?php echo $row['postcode']?>&countryCode=GB">View Map</a></p></td> <?php $count++ ; } ?> </tr> </table> <?php $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"$PHP_SELF?s=$prevs&search-q=$var\"><< Prev 10</a>  "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&search-q=$var\">Next 10 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; }}} ?> Hope you can help. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/142415-multiple-checkbox-search/ Share on other sites More sharing options...
MadTechie Posted January 26, 2009 Share Posted January 26, 2009 i think you want this $find = implode("=1 AND ",$_POST['criteria'])."=1"; $query = "select * from drinking WHERE $find order by venue asc"; //RESULT = $query= "select * from drinking WHERE realale=1 AND fruitwine=1 AND champagne=1 order by venue asc"; Quote Link to comment https://forums.phpfreaks.com/topic/142415-multiple-checkbox-search/#findComment-746190 Share on other sites More sharing options...
only1perky Posted January 26, 2009 Author Share Posted January 26, 2009 Thank you mate it worked perfectly. So simple when you know how. Quote Link to comment https://forums.phpfreaks.com/topic/142415-multiple-checkbox-search/#findComment-746391 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.