Leatfield Posted December 8, 2010 Share Posted December 8, 2010 Hi There, I have the following code: two arrays: $user_body_group & $DB_Seconday_muscles I want to look up the "$DB_Seconday_muscles" Array and search for the elements in the "$user_body_group" array The result would be displaying a checkbox list with only the items in the "$user_body_group" array checked: foreach($DB_Seconday_muscles as $value) { echo "<input name=\"colors[]\" type=\"checkbox\" value=\"$value\""; if (in_array($value,$user_body_group)) { echo "CHECKED"; } echo "> $value "; } BUT... When i run thi script i only get the first element in the searched array ticked. Can anyone help! Many Thanks! Leatfield Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 8, 2010 Share Posted December 8, 2010 Well, the values will need to be EXACTLY the same for in_array() to return true. Other than that the logic looks correct. However, the HTML is not formatted correctly. For one, there is no space after the closing quote of the value and to correctly check a field you should use checked="checked". BUt, that doesn't seem to be the cuase of the issue you are describing. This code is a little more efficient. If you still have problems with this code post the contents of the arrays for further help foreach($DB_Seconday_muscles as $value) { $checked = (in_array($value, $user_body_group)) ? ' checked="checked"' : ''; echo "<input name=\"colors[]\" type=\"checkbox\" value=\"{$value}\"{$checked}> {$value} \n"; } Quote Link to comment Share on other sites More sharing options...
Leatfield Posted December 8, 2010 Author Share Posted December 8, 2010 Thanks For your help. Yes the HTML is not great i intend to tidy that up. one of the arrays had a space between the comma E.G Element1, Element2, Element3 The space was the cause of the problem. Thans for the prompt reply! Leatfield Quote Link to comment 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.