tryingtolearn Posted July 29, 2007 Share Posted July 29, 2007 Hi again, I am trying to preselect all the checkboxes on an edit form if they are part of the record in the database I put this to get an array of categories that pertain to that record being edited. $res2 = mysql_query ("SELECT id FROM cat_associations WHERE tem_id=$tid"); while (list($cat) = mysql_fetch_row($res2)) { if ($cat) $cats=array($cat); } Then the checkboxes are added to the form with this $row1 = array(); $rows = array(); $res = mysql_query ("SELECT id, title, parent FROM categories ORDER BY title"); while (list($id, $title, $parent) = mysql_fetch_row($res)) { if ($parent) $rows[$parent][$id] = $title; else $row1[$id] = $title; } echo "<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\" width=\"100%\">\n"; echo '<tr><td><b>', implode('</b></td><td><b>', $row1), '</b></td></tr>'; echo "<tr valign=\"top\">\n"; foreach ($row1 as $k => $v) { echo"<td>\n"; if (isset($rows[$k])) { foreach ($rows[$k] as $id => $title){ if (in_array($id, $cats)){ echo"<input type=\"checkbox\" name=\"cat_x[]\" value=\"$id\" checked>$title<br>"; }else{ echo"<input type=\"checkbox\" name=\"cat_x[]\" value=\"$id\" >$title<br>"; } } } echo "</td>\n"; } echo "</tr>\n"; echo "</table>\n"; The part that is throwing me is this if (in_array($id, $cats)){ echo"<input type=\"checkbox\" name=\"cat_x[]\" value=\"$id\" checked>$title<br>"; }else{ echo"<input type=\"checkbox\" name=\"cat_x[]\" value=\"$id\" >$title<br>"; } } If the array from the top contains 10 11 12 51 I am only getting the last one (51) checked in the form. Is there a way to have it so it checks all of the boxes in the array? This sounds confusing - I hope I am explaining it OK. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 29, 2007 Share Posted July 29, 2007 try $cats = array(); $res2 = mysql_query ("SELECT id FROM cat_associations WHERE tem_id=$tid"); while (list($cat) = mysql_fetch_row($res2)) { if ($cat) $cats[] = $cat; // add $cat to array } Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted July 29, 2007 Author Share Posted July 29, 2007 Barrand, Worked perfect as usual! Thanks again for your help. 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.