MySQL_Narb Posted September 24, 2012 Share Posted September 24, 2012 <tr> <td>Type</td> <td> <select name="type" class="button"> <option value="1"'; if($type == 1) { $content .= 'selected="selected"'; } $content .= '>Normal</option> <option value="2"'; if($type == 2) { $content .= 'selected="selected"'; } $content .= '>Staff-only threads/topics</option> <option value="3"'; if($type == 3) { $content .= 'selected="selected"'; } $content .= '>New threads hidden</option> <option value="4"'; if($type == 4) { $content .= 'selected="selected"'; } $content .= '>Mod Forum</option> <option value="5"'; if($type == 5) { $content .= 'selected="selected"'; } $content .= '>Administrator Forum</option> </select> </td> </tr> Any idea on a more simplified method? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/268758-easier-way-to-go-about-this/ Share on other sites More sharing options...
Christian F. Posted September 24, 2012 Share Posted September 24, 2012 First of all, either you've cut away some necessary code when pasting this here, or that code is not working at all. Considering the syntax, I suspect it's the former. In any case, there is no point in dynamically creating what's basically static HTML. Move that HTML code out of your PHP code, and put it in the bottom where it belong. Alternatively set up an include script, if you don't want it shown at every load (of that page). As for a better way of doing it: <?php $selected = array (); $selected[intval ($type)] = ' selected="selected"'; ?> <select name="type" class="button"> <option value="1"<?php echo isset($selected[1]) ? $selected[1] : ''; ?>>Normal</option> <option value="2"<?php echo isset($selected[2]) ? $selected[2] : ''; ?>>Staff-only threads/topics</option> <option value="3"<?php echo isset($selected[3]) ? $selected[3] : ''; ?>>New threads hidden</option> <option value="4"<?php echo isset($selected[4]) ? $selected[4] : ''; ?>>Mod Forum</option> <option value="5"<?php echo isset($selected[5]) ? $selected[5] : ''; ?>>Administrator Forum</option>' </select> Using this method you can remove the ternary operator and the isset () check as well, but that will produce a notice about undefined indices. Should you have notices turned on in your error reporting. Alternatively to using the ternary operator there, you can loop through the available options and create one index per option. Setting only the one matching the type to selected, and the rest to empty strings. Quote Link to comment https://forums.phpfreaks.com/topic/268758-easier-way-to-go-about-this/#findComment-1380697 Share on other sites More sharing options...
MySQL_Narb Posted September 24, 2012 Author Share Posted September 24, 2012 (edited) Thanks Christian, definitely helpful. I remember using ternary operators, but I need to do this HTML inside the PHP. Any ideas? Edited September 24, 2012 by MySQL_Narb Quote Link to comment https://forums.phpfreaks.com/topic/268758-easier-way-to-go-about-this/#findComment-1380699 Share on other sites More sharing options...
DavidAM Posted September 25, 2012 Share Posted September 25, 2012 Use an array ... $optTypes = array( 1=> 'Normal', 2=> 'Staff-only threads/topics', 3=> 'New threads hidden', 4=> 'Mod Forum', 5=> 'Administrator Forum' ); echo '<tr><td>Type</td> <td><select name="type" class="button">'; foreach ($optTypes as $index => $optType) { echo sprintf('<option value="%d" %s>%s</option>', $index, ($index == $type ? 'selected="selected"' : ''), $optType); } echo '</select> </td> </tr>'; Quote Link to comment https://forums.phpfreaks.com/topic/268758-easier-way-to-go-about-this/#findComment-1380902 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.