Jump to content

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


Groups from database:1


No Match
Groups from database:3


Here is what the code looks like:
$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

This probably isn't the best way to it but I am new to php.
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.

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.