Jump to content

Array question


mjurmann

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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'"; 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

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.