Jump to content

Selecting Checkboxes based off MySQL Field that is an Array


jay7981

Recommended Posts

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by jay7981
Link to comment
Share on other sites

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 by denno020
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.