Jump to content

checking check boxes with variables from an array


goatboy

Recommended Posts

this is for a screen where i'm editing a user.

I'm able to assign the user access to certain sections. to do this in the user setup i use checkboxes.
i put the selections into an array and then serialize them before entering it into a database.

when i go to edit the user, getting the array out isn't the issue. i unserialize it. but i want the checkboxes checked according to whats in the array.
so lets say i have check boxes for
news, downloads, articles, submissions with i'd of 1-4
in my array i have 1,2,4

the problem i'm having is how do i compare the checkbox id to the i'ds in the array so it will be checked if one of them matches. btw the options for check boxes will get longer in the future.

I hope i explained this well enough and didn't make it too confusing
Link to comment
Share on other sites

When creating your checkboxes, you could do something like this:
[code]<?php
$boxes = array(1,2,4); //Populated from your SQL query
echo "<input type='checkbox' name='1' ";
if(in_array(1,$boxes)) echo "checked";
echo "/>";
?>[/code]
Link to comment
Share on other sites

  • 2 weeks later...
[!--quoteo(post=386158:date=Jun 20 2006, 04:53 PM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ Jun 20 2006, 04:53 PM) [snapback]386158[/snapback][/div][div class=\'quotemain\'][!--quotec--]
When creating your checkboxes, you could do something like this:
[code]<?php
$boxes = array(1,2,4); //Populated from your SQL query
echo "<input type='checkbox' name='1' ";
if(in_array(1,$boxes)) echo "checked";
echo "/>";
?>[/code]
[/quote]


ah that looks like it will work, and i can just replace the numeric values with the id variable from the database
Link to comment
Share on other sites

[!--quoteo(post=389463:date=Jun 29 2006, 04:25 PM:name=goatboy)--][div class=\'quotetop\']QUOTE(goatboy @ Jun 29 2006, 04:25 PM) [snapback]389463[/snapback][/div][div class=\'quotemain\'][!--quotec--]
ah that looks like it will work, and i can just replace the numeric values with the id variable from the database
[/quote]


works!

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.