Jump to content

[SOLVED] foreach loop and array help


tekrscom

Recommended Posts

Hi everyone, I was hoping for a bit of help with this loop and array I got here...

 

    <?
    $prequery = "SELECT InterestID FROM MemberInterests WHERE MemberID = '$_SESSION[MemberID]'";
    $preresults = mysql_query($prequery);
    while ($prerow = mysql_fetch_assoc($preresults)) {
	$myInterestsArray[] = $prerow;
}
    $query = "SELECT InterestID, InterestName FROM InterestTypes";
$results = mysql_query($query);
$RowsPerColumn = (mysql_num_rows($results) / 2);
$PrintedCounter = 0;
while ($row = mysql_fetch_assoc($results)) {
	$PrintedCounter += 1;
	echo '
        <tr>
            <td>
                ' . $row['InterestName'] . '
            </td>
            <td>
                <input type="checkbox" name="' . $row['InterestID'] . '"';
			foreach ($myInterestsArray as $key => $myInterestsArrayValue) {
				if ($row['InterestID'] == $myInterestsArrayValue) {echo ' checked';} 
			}
			echo '>
            </td>
        </tr>
	';
	if ($PrintedCounter == $RowsPerColumn) {
		echo '
                </table>
            </td>
            <td>
                <table style="width: 200px;">
		';
	}
}
    ?>

 

When I check the array with this...

	
<?
echo '<pre>';
print_r ($myInterestsArray);
echo '</pre>';
?>

 

I get this...

Array
(
    [0] => Array
        (
            [interestID] => 1
        )

    [1] => Array
        (
            [interestID] => 2
        )

    [2] => Array
        (
            [interestID] => 3
        )
etc, etc,...

Link to comment
https://forums.phpfreaks.com/topic/174230-solved-foreach-loop-and-array-help/
Share on other sites

Looks like you are looping through an array of arrays... so the value result of your for each is going to also be an array.    Try this..

 

if ($row['InterestID'] == $myInterestsArrayValue['InterestID']) {echo ' checked';} 

 

Yes, that worked quite nicely.. I knew I had an array in an array, I just didn't know how to access it... Thank  you very much!

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.