Jump to content

Recommended Posts


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

Link to comment
https://forums.phpfreaks.com/topic/268758-easier-way-to-go-about-this/
Share on other sites

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.

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>';

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.