tekrscom Posted September 14, 2009 Share Posted September 14, 2009 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,... Quote Link to comment https://forums.phpfreaks.com/topic/174230-solved-foreach-loop-and-array-help/ Share on other sites More sharing options...
wshell Posted September 14, 2009 Share Posted September 14, 2009 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';} Quote Link to comment https://forums.phpfreaks.com/topic/174230-solved-foreach-loop-and-array-help/#findComment-918476 Share on other sites More sharing options...
tekrscom Posted September 14, 2009 Author Share Posted September 14, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/174230-solved-foreach-loop-and-array-help/#findComment-918482 Share on other sites More sharing options...
wshell Posted September 14, 2009 Share Posted September 14, 2009 Glad to help... cheers. Quote Link to comment https://forums.phpfreaks.com/topic/174230-solved-foreach-loop-and-array-help/#findComment-918486 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.