Jump to content

why is my code adding extra charicters


Q695
Go to solution Solved by Q695,

Recommended Posts

I know the PHP is correct, but the HTML is adding extra information, and it's not pulling up the selected information based on the statement set.

<select name="slot" size="1">
        			<option value="" <?php
                    if ($item_slot=="0"){ echo "selected";}
					?>>Pick One</option>
					<option value="1"<?php
                    if ($item_slot=="1"){ echo "selected";}
					?>>Amulet</option>
                    <option value="2"<?php
                    if ($item_slot=="2"){ echo "selected";}
					?>>Head Gear</option>
                    <option value="3"<?php
                    if ($item_slot=="3"){ echo "selected";}
					?>>Epaulets</option>
                    <option value="4"<?php
                    if ($item_slot=="4"){ echo "selected";}
					?>>Shield</option>
                    <option value="5"<?php
                    if ($item_slot=="5"){ echo "selected";}
					?>>Chest Gear</option>
                    <option value="6"<?php
                    if ($item_slot=="6"){ echo "selected";}
					?>>Weapon</option>
                    <option value="7"<?php
                    if ($item_slot=="7"){ echo "selected";}
					?>>Ring</option>
                    <option value="8"<?php
                    if ($item_slot=="8"){ echo "selected";}
					?>>Boots</option>
                    <option value="9"<?php
                    if ($item_slot=="9"){ echo "selected";}
					?>>Gloves</option>
                    <option value="10"<?php
                    if ($item_slot=="10"){ echo "selected";}
					?> >Backpack</option>
				</select>


output code

<select size="1" name="slot">
<option value="">Pick One</option>
<option value="1">Amulet</option>
<option selected="" value="2">Head Gear</option>
<option value="3">Epaulets</option>
<option value="4">Shield</option>
<option value="5">Chest Gear</option>
<option value="6">Weapon</option>
<option value="7">Ring</option>
<option value="8">Boots</option>
<option value="9">Gloves</option>
<option value="10">Backpack</option>
</select>
Edited by Q695
Link to comment
Share on other sites

you have too much code, to write, test, reenter, change, add items to, reuse for different things...

 

by defining an array to hold your data, you can write/test generic code that you can reuse for different things simply by changing the data definition -

<?php
$options[1] = "Amulet";
$options[2] = "Head Gear";
$options[3] = "Epaulets";
$options[4] = "Shield";
$options[5] = "Chest Gear";
$options[6] = "Weapon";
$options[7] = "Ring";
$options[8] = "Boots";
$options[9] = "Gloves";
$options[10] = "Backpack";

?>
<select size="1" name="slot">
<option value="">Pick One</option>
<?php

$item_slot = 2; // test value

foreach($options as $value=>$legend){
    $selected = $item_slot == $value ? " selected='selected'": '';
    echo "<option value='$value'$selected>$legend</option>\n";
}
?>
</select>
Link to comment
Share on other sites

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.