Jump to content

Archived

This topic is now archived and is closed to further replies.

PattyB

Help with some code not working

Recommended Posts

This code takes an array from some user input and matches it to an array stored in a field in a database.  The first foreach should loop through each value from the user input.  The second foreach should take the value from the first foreach and match it to each value in the database. 

I cannot get the outside foreach to loop through.  Ex:  User selects 1,2, and 3.  The code will only process 1 and not 2 or 3.

Here is what the ouput looks like:

No Match
Groups from database:2

User-selected:1

Match
Groups from database:1

User-selected:1

No Match
Groups from database:3

User-selected:1


Here is what the code looks like:
[code]
<?php
$sql = "SELECT * FROM members";  //Set parameter for query
$results = mysql_query($sql);    //Get data from database
$groups_input_parsed = unserialize($groups_input); //Parse Mail Form and store


foreach($groups_input_parsed as $list_group)  //Roll through each selected checkbox
{

while ($row = mysql_fetch_array($results))    //Roll through each row in member database
{
        $id = $row["id"];                      //Store ID
        $email = $row["email"];                //Store Email
$groups=unserialize($row["groups"]);    //Parse Group array

foreach($groups as $groups1)            //Roll through group database
        {
        if ($groups1 == $list_group)            // If member group is equal to selected group
{
            printf("<p><b>Match</b> <br>"); //Do Stuff Here
printf("Groups from database:");
echo $groups1;
printf("<p> User-selected:");
echo $list_group;
        } //end if
else //Otherwise there are no matches
{
            printf("<p><b>No Match</b> <br>");  //Do Stuff Here
printf("Groups from database:");
echo $groups1; 
printf("<p> User-selected:");     
echo $list_group;
        } //End else
}  //End Inside foreach
}                                              //End While



} //end outside foreach
?>
[/code]

This probably isn't the best way to it but I am new to php.

Share this post


Link to post
Share on other sites
Try putting the [code=php:0]$results = mysql_query($sql); [/code] Inside the foreach loop.

Regards
Huggie

Share this post


Link to post
Share on other sites

×

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.