Jump to content


Photo

checking check boxes with variables from an array


  • Please log in to reply
3 replies to this topic

#1 goatboy

goatboy
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 20 June 2006 - 05:55 PM

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

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 June 2006 - 09:53 PM

When creating your checkboxes, you could do something like this:
<?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 "/>";
?>


#3 goatboy

goatboy
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 29 June 2006 - 09:25 PM

[!--quoteo(post=386158:date=Jun 20 2006, 04:53 PM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ Jun 20 2006, 04:53 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
When creating your checkboxes, you could do something like this:
<?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 "/>";
?>
[/quote]


ah that looks like it will work, and i can just replace the numeric values with the id variable from the database


#4 goatboy

goatboy
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 29 June 2006 - 09:37 PM

[!--quoteo(post=389463:date=Jun 29 2006, 04:25 PM:name=goatboy)--][div class=\'quotetop\']QUOTE(goatboy @ Jun 29 2006, 04:25 PM) View Post[/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!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users