Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySQL populating checkboxes problem

Status
Not open for further replies.

frainbreeze

Technical User
Jul 31, 2003
123
GB
hello

i have a mysql db that has 1 column for descriptions of images (i.e. people, signs, cat, dog). to input them into the db, i've used a form where all the checkboxes are imploded into an array and then stored as \n separated values.

when the user selects a record/image to edit, i would like the description checkboxes to automatically be checked, that way the user can uncheck (if the description no longer applies) or check more description boxes to update the record. at present, the user sees a textfield which displays the descriptions for that record, and has to manually re-check all the description boxes that already apply, as well as any additional ones they wish to add.

in so far i've managed to explode the words in the description column into an array, and loop through the array and have it return a value of checked or not.


Code:
$checked_values = explode ("\n", $combodesc);

global $checkboxlist;
$checkboxlist = array ("Dog", "Cat", "Signs", "People");

global $checkval;
#Cycle through the db-generated-array
if (isset($checked_values))
{

  for($i=0; $i < count($checked_values); $i++) 
  {
    $temp = $checked_values[$i];

    if (in_array ($temp, $checkboxlist))
    {
       $checkval = " checked ";
    }
    else 
    {
     echo " ";
    }
  }
}

and the form checkbox looks like this:

Code:
<input type="checkbox" name="desc[]" value="People" <? echo $checkval; ?> >People
<br>
<input type="checkbox" name="desc[]" value="Cats" <? echo $checkval; ?> > Cats

but i cant seem to have $checkval reset for the next checkbox...i.e. if $checkval is true for the 1st box, then it stays true for the entire form.. is it something to do with my loop structure?

thanks for any help

annie
 
Hi, why dont you try this:

if (isset($checked_values))
{

for($i=0; $i < count($checked_values); $i++)
{
$temp = $checked_values[$i];

if (in_array ($temp, $checkboxlist))
{
$checkval = " checked ";
}
else
{
echo " ";
$checkval = "";
}
}
}

I didn't see in your code where you reset the value of $checkval if it is false - thus it always remains true upon the first instance of it becoming true.


[cheers]
Cheers!
Laura
 
LTeeple

hello,

thanks for the suggestion, that was one of many solutions i've tried, and all it results in is all of the checkboxes being checked... im thinking its the placement of my end braces } to end the for/if loops, should they maybe encompass the actual form?

'preciate any other ideas,

annie
 
Well, one more idea -

how about placing the code for your input type into the if statement:

if (isset($checked_values)){
for($i=0; $i < count($checked_values); $i++){
$temp = $checked_values[$i];
if (in_array ($temp, $checkboxlist)){
$input[$i] = "<input type=\"checkbox\" name=\"desc[]\" value=\"$checked_values[$i]\" checked>$checked_values[$i]";
}
else{
$input[$i] = "<input type=\"checkbox\" name=\"desc[]\" value=\"$checked_values[$i]\">$checked_values[$i]";
}
}
}

Then for your form, depending on how it's structured, you can spit out the $input array using a for or while loop.


[cheers]
Cheers!
Laura
 
hello again,

i tried that, and while it works for the values held in the $input[$i] array, it displays nothing for the rest of the descriptions that are not held in $checked_values

for example:

if $checked_values (whatever matches the image) holds: people, cat, dog

and $checkboxlist (the complete list of possible descriptions) holds: people, cat, dog, shoes, hat

using the code above, only 3 checkboxes are displayed:
people (checked box)
cat (checked box)
dog (checked box)

but i would like:
people (checked box)
cat (checked box)
dog (checked box)
shoes (unchecked box)
hat (unchecked box)

to show up as well...

im thinking so far it should go:

Code:
while (not end of $checkboxlist array)
 go through and compare the $checkboxlist and $checked_values arrays

if (a match is found)
 output <input with "checked" tag>
else
 output regular <input tag>

but im not sure how to make it still print all values in $checkboxlist regardless of whether it matches or not?


thanks,

annie
 
hello,

i finally figured it out after i got over my sucky-loop-syndrome, heres the working code if anyone else is affected by this illness!

this will display all the checkboxes in $checkboxlist array, and display checked for each of $checkboxlist items that match $checked_values items

Code:
#$checkboxlist is an array of all possible checkbox values
#$checked_values is an array of the checked values associated with a record


foreach ($checkboxlist as $listitem) {
#for each entry in $checked_values place into $listitem temporarily
echo "<span class='desc'>";
	if (in_array ($listitem, $checked_values)) {
		echo "<br><input type=\"checkbox\" name=\"".$listitem."\" value=\"".$listitem."\" checked >".$listitem;
	}			
	else {
	echo "<br><input type=\"checkbox\" name=\"".$listitem."\" value=\"".$listitem."\" >".$listitem;
	} 
echo "</span>";
}

after it spits out all the checkboxes (checked and unchecked) the user can easily uncheck or check more boxes and then on submit just use SQL UPDATE for the entire record.

huzzah!


annie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top