Jump to content

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!

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.

Guest
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.