mjurmann Posted December 13, 2007 Share Posted December 13, 2007 Hello. I have a form with checkboxes that will be used to sort all of the entries in my database. Each of the checkbox inputs are as follows: <input type="checkbox" name="cats[]" value="9" />Red<br /> <input type="checkbox" name="cats[]" value="10" />Green<br /> <input type="checkbox" name="cats[]" value="11" />Blue<br /> <input type="checkbox" name="cats[]" value="12" />Violet<br /> <input type="checkbox" name="cats[]" value="13" />Orange<br /> <input type="checkbox" name="cats[]" value="14" />Brown<br /> <input type="checkbox" name="cats[]" value="15" />Pink<br /> When the form is submitted, I am trying to get all of the results from the database which have at least one of the checkbox values passed through the URL using $_GET The problem is, when I run the while loop and attempt to echo out the array, it only returns the entries that have the last checkbox value. All of the other values are ignored and thus I am not getting the correct results. Here is my code, I'm not very good with arrays so I'm sorry in advance. <?php if ($_GET['submit'] == "Search") { $cats = $_GET['cats']; foreach($cats as $categories) { echo "$categories"; echo "<br />"; $searchResult = "SELECT * FROM wp_posts JOIN wp_term_relationships on wp_posts.ID = wp_term_relationships.object_id JOIN wp_DLM_DOWNLOADS on wp_posts.ID = wp_DLM_DOWNLOADS.id WHERE wp_term_relationships.term_taxonomy_id = '$categories'"; } $searchResultRow = mysql_query($searchResult) or die(mysql_error()); echo $searchResult; while($row = mysql_fetch_array($searchResultRow)) { ?> <?php echo $row['post_title'];?> <?php } } Here is the information I get back when I echo $categories and then when I echo $searchResult: 10 28 SELECT * FROM wp_posts JOIN wp_term_relationships on wp_posts.ID = wp_term_relationships.object_id JOIN wp_DLM_DOWNLOADS on wp_posts.ID = wp_DLM_DOWNLOADS.id WHERE wp_term_relationships.term_taxonomy_id = '28' Thanks in advance if anyone can help me. Quote Link to comment Share on other sites More sharing options...
mjurmann Posted December 13, 2007 Author Share Posted December 13, 2007 Since the foreach loop is correctly echoing out the array, I'm convinced that there is something wrong with this line: $searchResult = "SELECT * FROM wp_posts JOIN wp_term_relationships on wp_posts.ID = wp_term_relationships.object_id JOIN wp_DLM_DOWNLOADS on wp_posts.ID = wp_DLM_DOWNLOADS.id WHERE wp_term_relationships.term_taxonomy_id = '$categories'"; Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted December 13, 2007 Share Posted December 13, 2007 In the following block of code: foreach($cats as $categories) { echo "$categories"; echo "<br />"; $searchResult = "SELECT * FROM wp_posts JOIN wp_term_relationships on wp_posts.ID = wp_term_relationships.object_id JOIN wp_DLM_DOWNLOADS on wp_posts.ID = wp_DLM_DOWNLOADS.id WHERE wp_term_relationships.term_taxonomy_id = '$categories'"; } All you're doing there is defining $searchResult your query, however everytime PHP loops through the array ($cats) it will overwrite $searchResult and thus only the last checkbox will return a result from the database. You will have to run your query and get the result from that query from within the foreach loop. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 13, 2007 Share Posted December 13, 2007 You're closing the foreach loop before you actually do the mysql_query so you're only obtaining the results for the last checked checkbox. Ken Quote Link to comment Share on other sites More sharing options...
mjurmann Posted December 13, 2007 Author Share Posted December 13, 2007 The only problem now is that when I'm going through the loop, some of the entries have more than one of the $cats values, so they are echoing out multiple times. How can I limit each entry to only appearing once even if it appears in more than one of search categories? Quote Link to comment Share on other sites More sharing options...
mjurmann Posted December 13, 2007 Author Share Posted December 13, 2007 Anyone? Please? Quote Link to comment Share on other sites More sharing options...
rab Posted December 13, 2007 Share Posted December 13, 2007 <?php if( isset($_GET['submit']) ) { $used = array(); foreach($_GET['cats'] as $cat) { $cat = (int)$cat; if( !in_array($cat,$used) ) { $used[] = $cat; $searchResult = "SELECT * FROM wp_posts JOIN wp_term_relationships on wp_posts.ID = wp_term_relationships.object_id JOIN wp_DLM_DOWNLOADS on wp_posts.ID = wp_DLM_DOWNLOADS.id WHERE wp_term_relationships.term_taxonomy_id = '$cat'"; $searchResultRow = mysql_query($searchResult) or die(mysql_error()); while($row = mysql_fetch_array($searchResultRow)) echo $row['post_title']; } } } ?> Try it 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.