Jump to content


Photo

Help with some code not working


  • Please log in to reply
2 replies to this topic

#1 PattyB

PattyB
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 September 2006 - 06:55 PM

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

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

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 14 September 2006 - 07:32 PM

Try putting the
$results = mysql_query($sql);
Inside the foreach loop.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 PattyB

PattyB
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 September 2006 - 08:11 PM

Yup that was it.  Thanks so much!  :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users