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.... Link to comment https://forums.phpfreaks.com/topic/152833-solved-help-determining-whether-checkbox-should-be-checked/ 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.">"; Link to comment https://forums.phpfreaks.com/topic/152833-solved-help-determining-whether-checkbox-should-be-checked/#findComment-802623 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! Link to comment https://forums.phpfreaks.com/topic/152833-solved-help-determining-whether-checkbox-should-be-checked/#findComment-802625 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. Link to comment https://forums.phpfreaks.com/topic/152833-solved-help-determining-whether-checkbox-should-be-checked/#findComment-802627 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.... Link to comment https://forums.phpfreaks.com/topic/152833-solved-help-determining-whether-checkbox-should-be-checked/#findComment-802650 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.