phpretard Posted August 23, 2009 Share Posted August 23, 2009 My objective is to get the comma seperated values from an individual row and display them as checkboxes. $all_cats[] = value1, value2, value3, value4, value5, value6 <- all should be displayed as checkboxes $type[]= value2, value4, value6 <-- these should be "checked" <?php connect(); $get_current_categories = mysql_query("select * from bugs2 where id='".$_GET['id']."' ") or die(mysql_error()); while ($cat_check = mysql_fetch_assoc($get_current_categories)) { $type[]=$cat_check['type']; } $imp=implode(", ", $type); $exp=explode(", ", $imp); $get_all_categories = mysql_query("select * from types order by name ") or die(mysql_error()); while ($all = mysql_fetch_assoc($get_all_categories)) { $all_cats[] = $all['name']; } foreach($all_cats as $category) { if(in_array($exp, $all_cats)){ // PROBLEM IS HERE I BELIEVE $selected = "<input type=\"checkbox\" name=\"categories[]\" value=\"$category\" checked />$category"; }else{ $selected = "<input type=\"checkbox\" name=\"categories[]\" value=\"$category\" />$category"; } echo $selected; } free($get_current_categories); free($get_all_categories); ?> The problem is all the chackboxes show up but none are checked. I think I have pinpointed where the problem is but am unable to fix it. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/ Share on other sites More sharing options...
xangelo Posted August 23, 2009 Share Posted August 23, 2009 You've flipped the in_array arguments. if(in_array($exp, $all_cats) Should actually be if(in_array($all_cats, $exp) The first value in in_array is what you're looking for. The second is the array you're looking for it in. http://us2.php.net/manual/en/function.in-array.php Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/#findComment-904497 Share on other sites More sharing options...
phpretard Posted August 23, 2009 Author Share Posted August 23, 2009 It still didn't work for some reason... Can you see any other issues? Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/#findComment-904517 Share on other sites More sharing options...
Psycho Posted August 23, 2009 Share Posted August 23, 2009 You are making this way more difficult than it needs to be. Simply do ONE query to get all the optins and add a parameter using JOIN to determine which ones should be checked. Give me a moment and I'll post some code based upon what you currently have. However, based on what you have the 'type' field in the 'bugs2' table would need to match up with the 'name' field in the 'types' table? Is that correct? Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/#findComment-904520 Share on other sites More sharing options...
kickstart Posted August 23, 2009 Share Posted August 23, 2009 Hi I agree with the above. Just been playing:- <?php connect(); $get_all_categories = mysql_query("select a.name, b.type from types a LEFT OUTER JOIN bugs2 b ON a.name = b.type AND id='".$_GET['id']."' order by a.name ") or die(mysql_error()); while ($all = mysql_fetch_assoc($get_all_categories)) { echo "<input type=\"checkbox\" name=\"categories[]\" value='".$all['name']."' ".((is_null($all['type'])) ? '' : 'checked' )." />".$all['name']; } ?> However you should check that $_GET['id'] is legit before using it in SQL. All the best Keith All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/#findComment-904522 Share on other sites More sharing options...
phpretard Posted August 23, 2009 Author Share Posted August 23, 2009 I "hardcoded" the id to 20(known to have the right info) and I get: Column 'id' in on clause is ambiguous What does that mean? I am researching as you (will hopefully) respond. THANK YOU!!! Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/#findComment-904529 Share on other sites More sharing options...
kickstart Posted August 23, 2009 Share Posted August 23, 2009 Hi Means that more than one of the tables has a column called id. So it doesn't know to check types.id or bugs2.id Qualify the id field for this:- <?php connect(); $get_all_categories = mysql_query("select a.name, b.type from types a LEFT OUTER JOIN bugs2 b ON a.name = b.type AND b.id='".$_GET['id']."' order by a.name ") or die(mysql_error()); while ($all = mysql_fetch_assoc($get_all_categories)) { echo "<input type=\"checkbox\" name=\"categories[]\" value='".$all['name']."' ".((is_null($all['type'])) ? '' : 'checked' )." />".$all['name']; } ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/#findComment-904538 Share on other sites More sharing options...
Psycho Posted August 23, 2009 Share Posted August 23, 2009 This should be all that you need. May need to check that field names are correct for your environment. It basically does a select all names on the 'types' table and then appends a new field to each record ('checked' with a value of 1 or 0) based upon whether there is a match in the 'bugs2' table with the type value matching the name. $query = "SELECT name, IF (name IN (SELECT type FROM bugs2 WHERE id='{$_GET['id']}'),1,0) as checked FROM types ORDER BY name" $result = mysql_query($query) or die(mysql_error()); $checkboxes = ''; while ($checkData = mysql_fetch_assoc($result)) { $checked = ($checkData['checked']==1) ? ' checked="checked"' : ''; $checkboxes .= "<input type="checkbox" name="categories[]" value="{$checkData['name']}"{$checked} />$checkData['name']<br /> "; } echo $checkboxes; EDIT: kickstart's method should work too. Just different ways to accomplish the same thing. I suspect his may be more efficient, but don't know for sure. Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/#findComment-904545 Share on other sites More sharing options...
phpretard Posted August 23, 2009 Author Share Posted August 23, 2009 Is this code to replace part or all of the existing? I ask because it still gives me all blank checkboxes...although it does display all the checkboxes The (comma seperated) value of b.type='value2, value4, value6' Should I not comma seperate? That is some great code by the way! It all makes sense to me except for: --- ".((is_null($all['type'])) ? '' : 'checked' )." --- I don't quite understand why the question mark and the colon are there. Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/#findComment-904547 Share on other sites More sharing options...
kickstart Posted August 23, 2009 Share Posted August 23, 2009 Hi The method is very similar to mine, just using a subselect while I have used a join. It was to replace you whole fragment of code. The bit ".((is_null($all['type'])) ? '' : 'checked' )." is doing an IF within a statement. It checks if the returned type feield is null with is_null($all['type']) , if that is true it returns the bit after the ? (ie, nothing in this case), if false it returns the bit after the : ('checked'). Where is he comma seperated list of b.type coming from? All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/#findComment-904554 Share on other sites More sharing options...
phpretard Posted August 23, 2009 Author Share Posted August 23, 2009 Comma seperation comes from: if (isset($_POST['submit'])){ $categories=implode(", ", $_POST['categories']); connect(); $insert=mysql_unbuffered_query("update bugs2 set type = '$categories' where id='".intval($_POST['id'])."' ") or die(mysql_error()); } So when I update it changes the values in the DB (with the simplified code Hooray!) it's the display that's killing me...it still won't show "checked" boxes Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/#findComment-904556 Share on other sites More sharing options...
kickstart Posted August 23, 2009 Share Posted August 23, 2009 Hi Arrggh. That wasn't in the original post. What is it that you want from that table? What are categories? Why are they stored in one field? My guess is that you have bugs which have various catagories. If so what you should really have is a table of bugs and another table list all the categories for each bug. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/#findComment-904560 Share on other sites More sharing options...
phpretard Posted August 23, 2009 Author Share Posted August 23, 2009 I apologize for that... I am trying to fix someone elses mess and it seemed the easiest way to resolve was to comma seperate in the (field "type") in the table "bugs"), then reference the table I added called (categories) that has six entries (which are the categories). [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/#findComment-904566 Share on other sites More sharing options...
kickstart Posted August 23, 2009 Share Posted August 23, 2009 Hi If you can move the types off to a seperate table then that would be far easier, as you can then do most of the work in one SQL call. While there are ways to extract the comma seperated field, they are not nice to read, would be a nightmare to maintain and I would perform badly. As well as giving you major problems if someone introduces a type with a comma in it. To continue with what you already have:- <?php connect(); $TypeArray = array(); $get_current_categories = mysql_query("select type from bugs2 where id='".$_GET['id']."' ") or die(mysql_error()); while ($cat_check = mysql_fetch_assoc($get_current_categories)) { $types = explode(',',$cat_check['type']); foreach($types as $type) { $type = trim($type); $TypeArray[$type] = $type; } } $get_all_categories = mysql_query("select name from types order by name ") or die(mysql_error()); while ($all = mysql_fetch_assoc($get_all_categories)) { echo "<input type='checkbox' name='categories[]' value='".$all['name']."' ".((isset($TypeArray[$all['name']])) ? "checked='checked'" : '' )." />".$all['name']; } free($get_current_categories); free($get_all_categories); ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/#findComment-904594 Share on other sites More sharing options...
phpretard Posted August 23, 2009 Author Share Posted August 23, 2009 You are right... I was trying to save a little time but look how much I've spent (yours and mine) with band-aids Thank you for your help!!! I will just rebuild it the right way. Quote Link to comment https://forums.phpfreaks.com/topic/171523-solved-difficult-arrays-and-check-boxes/#findComment-904600 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.