Jump to content

why is my code adding extra charicters


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>
Link to comment
https://forums.phpfreaks.com/topic/277610-why-is-my-code-adding-extra-charicters/
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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.