Jump to content

Help with some code not working


PattyB

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.
Link to comment
https://forums.phpfreaks.com/topic/20761-help-with-some-code-not-working/
Share on other sites

Archived

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

×
×
  • 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.