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

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

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.