clankill3r Posted October 12, 2011 Share Posted October 12, 2011 I want to make a list like this: I store the genres in a array like this: $genre_list = array( array("ACTION", "First Person Shooter"), array("ACTION", "Third Person Shooter"), array("ACTION", "Tactical Shooter"), array("ACTION", "Fighting"), array("ACTION", "Arcade"), array("ADVENTURE", "Adventure"), array("ADVENTURE", "Platformer"), array("ADVENTURE", "Point and Click") ); This is my first attempt to only show the subgenre, i only get a blank list (apart from this <option value="genre">Genre</option> ). <div class="forminput"> <select id="genres" name="genres"> <option value="genre">Genre</option> <?php for($i=0; $i<count($genre_list[0]); $i++) { ?> <option value="<?=$i?>"<?=$genre_list[0][$i]?></option> <?php } ?> </select> </div> Quote Link to comment Share on other sites More sharing options...
jnvnsn Posted October 12, 2011 Share Posted October 12, 2011 <div class="forminput"> <select id="genres" name="genres"> <option value="genre">Genre</option> <?php for($i=0; $i<count($genre_list[0]); $i++) { ?> <option value="<?=$i?>"<?=$genre_list[0][$i]?></option> <?php } ?> </select> </div> The loop is only intended for the first array: array("ACTION", "First Person Shooter"). The rest are not being traversed. Try this in your code: $count = count($genre_list); for($i=0; $i<$count; $i++) { echo $genre_list[$i][0]; echo $genre_list[$i][1]; } Quote Link to comment Share on other sites More sharing options...
WebStyles Posted October 12, 2011 Share Posted October 12, 2011 try this to create groups inside the select box: <?php $genre_list = array( array("ACTION", "First Person Shooter"), array("ACTION", "Third Person Shooter"), array("ACTION", "Tactical Shooter"), array("ACTION", "Fighting"), array("ACTION", "Arcade"), array("ADVENTURE", "Adventure"), array("ADVENTURE", "Platformer"), array("ADVENTURE", "Point and Click") ); $group = ''; echo '<select id="genres" name="genres">'; foreach($genre_list as $item){ if($group == '' || $group != $item[0]) echo '<optgroup label="'.$item[0].'">'; $group = $item[0]; echo '<option value="'.$item[0].'-'.$item[1].'">'.$item[1].'</option>'; } echo '</select>'; ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 12, 2011 Share Posted October 12, 2011 Another option is to build an array in a more logical format using the category/group name as the index and the genre's as sub-arrays. This is much more logical IMHO $genre_list = array ( "ACTION" => array("First Person Shooter", "Third Person Shooter", "Tactical Shooter", "Fighting", "Arcade"), "ADVENTURE" => array("Adventure", "Platformer", "Point and Click") ) echo "<select id='genres' name='genres'>\n"; foreach($genre_list as $genre => $subgenre_list) { echo "<optgroup label='{$genre}'>\n"; foreach($subgenre_list as $subgenre) { echo "<option value='{$genre}-{$subgenre}'>{$subgenre}</option>\n"; } } echo "</select>\n"; Quote Link to comment Share on other sites More sharing options...
WebStyles Posted October 12, 2011 Share Posted October 12, 2011 Yeah, I totally agree with mjdamato. But since your question was not "how should I organize the array", I didn't mention it. Quote Link to comment Share on other sites More sharing options...
clankill3r Posted October 12, 2011 Author Share Posted October 12, 2011 Both thanks, i prefer the last method. Is it hard to make the genre selectable aswell, and not only the subgenre? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted October 12, 2011 Share Posted October 12, 2011 just put it in an <option> tag instead of an <optgroup> Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 12, 2011 Share Posted October 12, 2011 Of course, once you make the genre an option you lose the visual "grouping". But, you can use styles to simulate the same effect. But, I can't validate that the stylings will look the same from browser to browser. You'd have to test for yourself. This will give you a "similar" look without using the OPTGROUP tag (at least in FF). Define the following classes in your style sheet .genreOption { font-weight: bold; font-style: italic;} .subgenreOption { margin-left: 10px; } Then change the code above to instead apply different styles based upon the value type foreach($genre_list as $genre => $subgenre_list) { echo "<option class='genreOption' value='{$genre}'>{$genre}</option>\n"; foreach($subgenre_list as $subgenre) { echo "<option class='subgenreOption' value='{$genre}-{$subgenre}'>{$subgenre}</option>\n"; } } NOTE: The style are only applied when you view the values with the list expanded. Once a value is selected and the select list closes it is displayed 'normally' Quote Link to comment Share on other sites More sharing options...
clankill3r Posted October 13, 2011 Author Share Posted October 13, 2011 thanks! 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.