jay7981 Posted February 24, 2013 Share Posted February 24, 2013 Hey all i am trying to select checkboxes using MySQL data that is in an array i have 3 checkboxes Open Close Break in the database i have a field "action" in the field is the data "Open,Close,Break" i know i can use <?= ($action=='Open')? 'checked="checked"' : '' ?> but i need to have all boxes that match the data in the field selected ... how do i go about doing this ? here is the form code for the specific field i am working with. $action= $res['action']; $actdata= implode(",", $action); <tr> <td>Action</td> <td><input type="checkbox" name="action[]" value="Open" /> Open <input type="checkbox" name="action[]" value="Close" /> Close <input type="checkbox" name="action[]" value="Break" /> Break</td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/274876-selecting-checkboxes-based-off-mysql-field-that-is-an-array/ Share on other sites More sharing options...
denno020 Posted February 24, 2013 Share Posted February 24, 2013 I assume that all the data is already in the same field? So the field could contain "Open, Close", or "Open", or all three "Open, Close, Break"? I'm not sure why you're using implode(), as that will join things together, and I don't think $action is an array. Anyway, what you could do is explode $action into another variable, using comma as the delimiter. Then do a for each that loops through the exploded array. Something like this: $action = $res['action']; $actionArr = explode(",", $action); echo "<td>Action</td>"; ecoh "<td>"; foreach($actionArr as $actionItem){ $actionItem = trim($actionItem); //Remove any leading/trailing white space echo "<input type="checkbox" name ="action[]" value="$actionItem" checked />"; echo $actionItem; } echo "</td>"; That will print the checked items as you want, however it won't print the others that aren't checked, but it's not hard to figure out how to add that in. Hope that helps Denno Quote Link to comment https://forums.phpfreaks.com/topic/274876-selecting-checkboxes-based-off-mysql-field-that-is-an-array/#findComment-1414552 Share on other sites More sharing options...
jay7981 Posted February 24, 2013 Author Share Posted February 24, 2013 (edited) your assumtion is correct, you can have any variation of the 3 in the same field, as for the implode i am joining the checkbox data together and then setting $actdata to that data as a comma seperated list and that is what is stored in the table. this is going to be an edit page, so i will need to have all 3 boxes statically in place and i will have a query pull the data from table and check the boxes accordingly so that the user could either add a selection or remove then re submit the form Edited February 24, 2013 by jay7981 Quote Link to comment https://forums.phpfreaks.com/topic/274876-selecting-checkboxes-based-off-mysql-field-that-is-an-array/#findComment-1414553 Share on other sites More sharing options...
denno020 Posted February 24, 2013 Share Posted February 24, 2013 (edited) Oh so the implode happens before the data goes into the database? That makes sense then, I thought this was the beginnings of your script to extract the data from the database. Anyway, another thing you could do then which could be simpler is to use strpos as such: $actionItems = //query to get the results from the database - this will be a string, comma separated, as you've already said $open = (strpos($actionItems, "Open") !== false) ? "checked" : ""; //this is a one line if statement, if the string "Open" is in $actionItems, this will be it's to be checked, so set $open to checked //do the same for the other two actions //Print out the checkboxes echo "<td>Action</td>"; echo "<td>"; echo "<input type='checkbox' name='action[]' value='open' $open/>"; //This will place either 'checked' or nothing to the end of the input tag echo "Open"; //again, do the above for the other two options. echo "</td>"; I'm pretty sure that will get you want you want.. Denno Edited February 24, 2013 by denno020 Quote Link to comment https://forums.phpfreaks.com/topic/274876-selecting-checkboxes-based-off-mysql-field-that-is-an-array/#findComment-1414554 Share on other sites More sharing options...
jay7981 Posted February 24, 2013 Author Share Posted February 24, 2013 Dude You Rock!! Thanks so much that has been driving me crazy for hours ... Worked like a charm! Quote Link to comment https://forums.phpfreaks.com/topic/274876-selecting-checkboxes-based-off-mysql-field-that-is-an-array/#findComment-1414555 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.