Jump to content

[SOLVED] Help Determining Whether Checkbox Should Be Checked?


limitphp

Recommended Posts

I have a value that is passed from checkboxes all with the same name "genre[]".

 

The value is stored in a variable called $genres.

 

The value is the ids of the genres that were checked.  So the value could be:

"1" or "1,2" or "1,2,3" or "1,5,10,14" or "10" or "11, 17"

 

I want to determine if a checkbox should be checked or not....

like

if ()//this genreID is in the value $genres

  <input checkbox name="genre[]" value="whatever" CHECKED>

else

  <input checkbox name="genre[]" value="whatever" >

 

What code could I use to see if the genreID is in the variable $genres?

 

I was thinking something like:

if (strstr($genres, '1'))

but that will be true if the $genres has a 10 or 11 in it....

 

something along these lines;

$genres="1,2,3"; 
$genres_array=explode(",", $string); 
if(!in_array($genreID, $genres_array)) 
{ 
$checked="checked='checked'"; 
}
else 
{
$checked=""; 
}
echo "<input type='checkbox' name='genre[]' value='$genreID' ".$checked.">"; 

something along these lines;

$genres="1,2,3"; 
$genres_array=explode(",", $string); 
if(!in_array($genreID, $genres_array)) 
{ 
$checked="checked='checked'"; 
}
else 
{
$checked=""; 
}
echo "<input type='checkbox' name='genre[]' value='$genreID' ".$checked.">"; 

 

So, if I put it back in array form, it can tell if just "1" is in there?  It won't confuse 11 for it?

 

thanks!

Depending on if I understood you correctly I'd do the following:

<?php
$arrGenre = array_flip( explode( ',', $genre ) );

if ( isset( $arrGenr[1] ) )//this genreID is in the value $genres
  echo '<input checkbox name="genre[]" value="whatever" CHECKED>';
else
   echo '<input checkbox name="genre[]" value="whatever" >';

?>

 

explode creates an array out of your values using the comma (first parameter) to split the string.

array_flip changes the keys and values around. So you can use isset() on the index instead of having to loop over the array for each checkbox.

 

[edit]Looks like it took me too long to find the array_flip function. Maybe it's easier to understand for you? Please do post another response if it's still not clear.

Actually, I really liked what you did here:

$genres_array=explode(",", $string);
if(!in_array($genreID, $genres_array))
{
$checked="checked='checked'";
}
else
{
$checked="";
}

 

I'll take that and put it in a loop:

$genres_array=explode(",", $genres);

$checked = array(); //Is this how you create an array?

 

while ($i<=17)

{

  if(!in_array($i, $genres_array))

    $checked[$i]="checked";

  else

    $checked[$i]="";

  i++;

}

 

<input checkbox name="genre[]" value="whatever" $checked[0]>

etc....

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.