Jump to content

Recommended Posts

Okay, so, here's my dilemma. I'm wanting to have it so someone can edit an item in the database, but it is a dropdown menu for the options to choose from.

 

How do I make it so that the option that the item currently is is what the default showing is?

 

For instance, if the item is for a "warrior", it will show warrior already in the dropdown menu. However, I could go to the dropdown menu and select a different option if I wanted. The {{class}} doesn't seem to give that default. Right now, it just defaults to "All" regardless of what the item truly goes to, such as "warrior".

 

Thanks in advance!!

 

Here's what I have for the coding:

 

<tr><td width="20%">Class:</td><td><select name="class" value="{{class}}">
<option value="1" {{class1select}}>All</option>
<option value="2" {{class2select}}>Alchemist</option>
<option value="3" {{class3select}}>Assassin</option> 
<option value="4" {{class4select}}>Dark Arts</option>
<option value="5" {{class5select}}>Dark Paladin</option>
<option value="6" {{class6select}}>Entertainer</option>
<option value="7" {{class7select}}>Hunter</option>
<option value="8" {{class8select}}>Mage</option>
<option value="9" {{class9select}}>Monk</option>
<option value="10" {{class10select}}>Paladin</option>
<option value="11" {{class11select}}>Pirate</option>
<option value="12" {{class12select}}>Priest</option>
<option value="13" {{class13select}}>Psion</option>
<option value="14" {{class14select}}>Scholar</option>
<option value="15" {{class15select}}>Thief</option>
<option value="16" {{class16select}}>Warlock</option>
<option value="17" {{class17select}}>Warrior</option>
</select></td></tr>

Link to comment
https://forums.phpfreaks.com/topic/258701-drop-down-list-default-for-editing/
Share on other sites

Here's to options, one hard coded, the other generated.

<?php
//example result
$row['class']="5";
echo '<tr><td width="20%">Class:</td><td><select name="class">';
echo "<option value=\"1\"" . ($row['class']=="1" ? ' selected="selected"' : '') . ">All</option>";
echo "<option value=\"2\"" . ($row['class']=="2" ? ' selected="selected"' : '') . ">Alchemist</option>";
echo "<option value=\"3\"" . ($row['class']=="3" ? ' selected="selected"' : '') . ">Assassin</option>";
echo "<option value=\"4\"" . ($row['class']=="4" ? ' selected="selected"' : '') . ">Dark Arts</option>";
echo "<option value=\"5\"" . ($row['class']=="5" ? ' selected="selected"' : '') . ">Dark Paladin</option>";
echo "<option value=\"6\"" . ($row['class']=="6" ? ' selected="selected"' : '') . ">Entertainer</option>";
echo "<option value=\"7\"" . ($row['class']=="7" ? ' selected="selected"' : '') . ">Hunter</option>";
echo "<option value=\"8\"" . ($row['class']=="8" ? ' selected="selected"' : '') . ">Mage</option>";
echo "<option value=\"9\"" . ($row['class']=="9" ? ' selected="selected"' : '') . ">Monk</option>";
echo "<option value=\"10\"" . ($row['class']=="10" ? ' selected="selected"' : '') . ">Paladin</option>";
echo "<option value=\"11\"" . ($row['class']=="11" ? ' selected="selected"' : '') . ">Pirate</option>";
echo "<option value=\"12\"" . ($row['class']=="12" ? ' selected="selected"' : '') . ">Priest</option>";
echo "<option value=\"13\"" . ($row['class']=="13" ? ' selected="selected"' : '') . ">Psion</option>";
echo "<option value=\"14\"" . ($row['class']=="14" ? ' selected="selected"' : '') . ">Scholar</option>";
echo "<option value=\"15\"" . ($row['class']=="15" ? ' selected="selected"' : '') . ">Thief</option>";
echo "<option value=\"16\"" . ($row['class']=="16" ? ' selected="selected"' : '') . ">Warlock</option>"; 
echo "<option value=\"17\"" . ($row['class']=="17" ? ' selected="selected"' : '') . ">Warrior</option>";
echo "</select></td></tr>";	
?>

 

Better option

<?php 
//example result
$row['class']="2";
$classes=array("Select","All","Alchemist","Assassin","Dark Arts","Dark Paladin","Entertainer","Hunter","Mage","Monk","Paladin","Pirate","Priest","Psion","Scholar","Thief","Warlock","Warrior");

echo "<tr><td width=\"20%\">Class:</td><td><select name=\"class\">";
foreach ($classes as $k => $v){
echo  "<option value=\"$k\"" . ($row['class']=="$k" ? ' selected="selected"' : '') . ">$v</option>";
}
echo "</select></td></tr>";
?>

Assuming you know what you want to default before any HTML is output to the header, you can use PHP to set a variable like $classDefault = "warrior" and then use inline if statements to print out the selected attribute in the option tag.

 

<option value="warrior" <?=($classDefault == "warrior" ? "selected=\"true\"" : "");?>>Warrior</option>
<option value="paladin" <?=($classDefault == "paladin" ? "selected=\"true\"" : "");?>>Paladin</option>
<option value="priest" <?=($classDefault == "priest" ? "selected=\"true\"" : "");?>>Priest</option>

Here's my method for such, using objects and ternary for brevity:

<select name="class">
<?php foreach($available_classes as $class):?>
	<option 	
		// Compare the user's class id with the available class id's, if there's a match, establish it selected
		(($user->class->class_id == $class->class_id) ? 'selected=selected' : '' )

		// create the option's value
		value= "<?php echo $class->class_id;?>"
		> 

	// create the option's text
	<?php echo $class->class_title; ?>
	</option>
<?php endforeach; ?>
</select>

I tried this option:

 

<?php 
//example result
$row['class']="2";
$classes=array("Select","All","Alchemist","Assassin","Dark Arts","Dark Paladin","Entertainer","Hunter","Mage","Monk","Paladin","Pirate","Priest","Psion","Scholar","Thief","Warlock","Warrior");

echo "<tr><td width=\"20%\">Class:</td><td><select name=\"class\">";
foreach ($classes as $k => $v){
echo  "<option value=\"$k\"" . ($row['class']=="$k" ? ' selected="selected"' : '') . ">$v</option>";
}
echo "</select></td></tr>";
?>

 

... But I'm getting an error on the $row['class']="2"; line.

 

Error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

 

I don't see why that is an error...

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.