Jump to content

Recommended Posts

I am trying to allow editing in an admin section. I have several checkboxes where the user can check one or more of a category which are then sent to a database using the serialize function. I would like the admin to be able to pull up the entire list of checkboxes showing the ones that were previously checked.

So here's what I have that doesn't work:

the array:

$array1 = array ('option1,'option2','option3');

then the rest

$choice = unserialize($row['choice']);
foreach ($array1 as $value)
					{
						echo '<input type="checkbox" name="option[]" value="' . $value . '"';
						if ($value == $option) {
						echo ' checked';
						}';	
				}

 

Do this make sense? As of now the checkboxes show but the previously "checked" ones are not checked.

Link to comment
https://forums.phpfreaks.com/topic/82251-need-help-with-2-arrays/
Share on other sites

Where is $option defined? Do you mean $choice?

 

 

Also make it

if ($value == $option) {
    echo ' checked';
}	

 

You had a '; at the end brace

 

 

Yes sorry, I think I'm losing it... LOL

it should be choice = unserialize($row['choice']);

$choice = unserialize($row['choice']);
foreach ($array1 as $value)
{
	echo '<input type="checkbox" name="option[]" value="' . $value . '"';
	   if ($value == $choice) {
	    echo ' checked';
	   }
          echo '>';
}

I'm going to admit that I have absolutely -no- idea what's really going on here... heh

 

I'll take a stab, tho, because this problem is fading down the list...

 

If my solution isn't a solution, it might get you headed the right way, so here we go...

 

Could you maybe be needing to compare the key against choice for the 'checked' portion?

 

$choice = unserialize($row['choice']);
foreach ($array1 as $key => $value)
{
	echo '<input type="checkbox" name="option[]" value="' . $value . '"';
	   if ($key == $choice) {
	    echo ' checked';
	   }
          echo '>';
}

 

Whether or not this works, I have no idea why this is being done... serialize wasn't really purposed for storing an array into a database in this manner. This defeats the reason behind having a normalized database with atomic values. I would much prefer to fix the logic behind the database structure, and this problems disappears forever. I can't be of much help there, as the 'bigger picture' of what you're doing isn't here, just this snippet... just some thoughts. = )

 

PhREEEk

I'm sure that this could be set up better... LOL.

Basically I have a form that people fill out (name, address, email, etc) and then they have have to check all their specialties using checkboxes. All this info gets put into a database. I'm using the serialize function to handle the checked info.

Now I'm creating a form where admin can edit this information if needed. So, I'm trying to basically re-create the form with the previous entered values showing. So the name textbox shows the value entered for name, email textbox shows the email address entered, etc.... and all the checkboxes show up with the previously checked ones checked.

Does that help at all?

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.