limitphp Posted April 6, 2009 Share Posted April 6, 2009 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.... Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted April 6, 2009 Share Posted April 6, 2009 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.">"; Quote Link to comment Share on other sites More sharing options...
limitphp Posted April 6, 2009 Author Share Posted April 6, 2009 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! Quote Link to comment Share on other sites More sharing options...
Axeia Posted April 6, 2009 Share Posted April 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
limitphp Posted April 6, 2009 Author Share Posted April 6, 2009 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.... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.