AviNahum Posted June 23, 2009 Share Posted June 23, 2009 ummm i made an article application that allows to admin to put one article in more than one category... i use checkboxs to choose the categories and i just implode it and insert into DB... but when i try to edit a article i want the checkbox of category already be checked (only categories that choosen)... $DB->query("SELECT * FROM cms_articles WHERE id='$article_id'"); $article = $DB->fetch_row(); //+--------------------------- // Select all out categories //+--------------------------- $DB->query("SELECT * FROM cms_categories order by id"); $cats_number = $DB->get_num_rows(); $c .= '<table border="0" cellpadding="0" cellspacing="0" width="45%" bgcolor="#FFFFFF"><tr>'; // Set up cats counter $count = 1; // If we dont have any categories so show an error if ($cats_number == 0) $c .= '<td><b>לא קיימים קטגוריות<b></td>'; $a = explode(',', $article['cats']); $b = count($a); while ($cat = $DB->fetch_row()) { for ($i=0; $i<= $b; $i++) { if ($a[$i] == $cat['id']) { $checked .= 'checked="checked"'; } } $c .= '<td width="25%"><input type="checkbox" name="cats[]" value="'.$cat['id'].'" '.$checked.'> '.$cat['name'].'</td>'; $count++; // Every 4 cats we want to break a line if ($count == 4) { $c .= '</tr><tr>'; $count = 1; } } $c .= '</tr></table>'; there are some words in hebrew, sorry... as you can see i just select the cats of the article and explode it.... and i using the FOR loop into the WHILE loop to make the boxes checked... it's wont work... its make all categories checked... i want it's checked only the categories that i choosed when i made the article... ***sorry for very poor english*** Quote Link to comment https://forums.phpfreaks.com/topic/163343-problems-with-checkbox/ Share on other sites More sharing options...
Dathremar Posted June 23, 2009 Share Posted June 23, 2009 I think You should change for ($i=0; $i<= $b; $i++) { if ($a[$i] == $cat['id']) { $checked .= 'checked="checked"'; } } To: $checked = (in_array($cat['id'] ), $a) ? 'checked="checked"' : ""; The statement $checked .= 'something' concats the string to the already existing $checked variable. Probably that is why You got all the check boxes checked. Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/163343-problems-with-checkbox/#findComment-861810 Share on other sites More sharing options...
AviNahum Posted June 23, 2009 Author Share Posted June 23, 2009 thanks a lot! its works great! Quote Link to comment https://forums.phpfreaks.com/topic/163343-problems-with-checkbox/#findComment-861814 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.