Jump to content

Drop Down Menu help...


zelig

Recommended Posts

Okay, so, here's the scenario.

 

I have a form that is editing an item that is already in the database. The text fields fill in just fine with that info. However, the drop down menus don't retrieve that info, rather resorting to the defaults, which can be a problem if you don't remember what you originally had.

 

Is there anyway to make the dropdown menus pull the info from the table and use that rather than resorting back to the default?

 

I tried using this:

 

<tr><td width="20%">Bonus:</td><td><select name="bonus" value="{{bonus}}">
<option value="Attack" {{bonus1select}}>Add to the attack power of weapon</option>
<option value="Defense" {{bonus2select}}>Add to the defensive power of armor</option>
<option value="None" {{bonus3select}}>No effect</option>
</select><br /></td></tr>

 

So, it's obvious the "value" portion not working. Any help would be great!!

Link to comment
Share on other sites

You mean setting the default selected value to match the database?

 

Your {$bonus1select} and {$bonus2select} look like they are setup to do this. However, I don't know why you are doing {{$variableName}}. Please post a greater portion of your code.

 

I'm pretty sure that <select doesn't have a value attribute. Whatever the default is has to be selected by either (1) Displaying it first in the options list or (2) giving it the "selected" attribute in the <option tag.

Link to comment
Share on other sites

Here's my coding so far:

 

function edititem($id) {
    
    if (isset($_POST["submit"])) {
        
        extract($_POST);
        $errors = 0;
        $errorlist = "";
        if ($name == "") { $errors++; $errorlist .= "Name is required.<br />"; }
        if ($price == "") { $errors++; $errorlist .= "Price is required.<br />"; }
        if (!is_numeric($price)) { $errors++; $errorlist .= "Price must be a number.<br />"; }
        if ($type == "") { $errors++; $errorlist .= "Type is required.<br />"; }
        if (!is_numeric($value)) { $errors++; $errorlist .= "Value must be a number.<br />"; }
        
        if ($errors == 0) { 
		$query = doquery("UPDATE `items` SET `name`='$name', `descript`='$descript', `price`='$price', `type`='$type', `bonus`='$bonus', `slot`='$slot', `target`='$target', `attr`='$attr', `value`='$value' WHERE `id`='$id'", "items");
		admindisplay("Item updated.","Edit Items");
        } else {
            admindisplay("<b>Errors:</b><br /><div style=\"color:red;\">$errorlist</div><br />Please go back and try again.", "Edit Items");
        }        
        
    }   
        
    
    $query = doquery("SELECT * FROM items WHERE id='$id'", "items");
    $row = mysql_fetch_array($query);

$page = <<<END
<b><u>Edit Items</u></b><br /><br />
<form action="admin_panel.php?do=edititem:$id" method="post">
<table width="90%">

<tr><td width="20%">Type:</td><td><select name="type" value="{{type}}">
<option value="Armor" {{type1select}}>Armor</option>
<option value="Item" {{type2select}}>Item</option>
<option value="Pet" {{type3select}}>Pet</option>
<option value="Potion" {{type4select}}>Potion</option>
<option value="Weapon" {{type5select}}>Weapon</option>
</select><br /><span class="small">Choose what type the item is: Items = RP only items that will not be "used".</span></td></tr>
</table>
<input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" />
</form>
END;
    
    if ($row["type"] == 1) { $row["type1select"] = "selected=\"selected\" "; } else { $row["type1select"] = ""; }
    if ($row["type"] == 2) { $row["type2select"] = "selected=\"selected\" "; } else { $row["type2select"] = ""; }
    if ($row["type"] == 3) { $row["type3select"] = "selected=\"selected\" "; } else { $row["type3select"] = ""; }
if ($row["type"] == 4) { $row["type4select"] = "selected=\"selected\" "; } else { $row["type4select"] = ""; }
if ($row["type"] == 5) { $row["type5select"] = "selected=\"selected\" "; } else { $row["type5select"] = ""; }
$page = parsetemplate($page, $row);
    admindisplay($page, "Edit Items");
    
}

Link to comment
Share on other sites

A better way might be:

 

$options = array(1 => "Armor", 2 => "Item", 3 => "Pet, 4 => "Potion", 5 => "Weapon");
$selectCode = "<select name=\"type\">";
foreach ($options as $key => $value) {
   $selected = "";
   if ($row['type'] == $key) $selected = " selected";

   $selectCode .= "<option value=\"{$value}\"{$selected}>{$value}</option>";
}
$selectCode = "</select>";

$page = <<<END
<b><u>Edit Items</u></b><br /><br />
<form action="admin_panel.php?do=edititem:$id" method="post">
<table width="90%">

<tr><td width="20%">Type:</td><td>
{$selectCode}
<br /><span class="small">Choose what type the item is: Items = RP only items that will not be "used".</span></td></tr>
</table>
<input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" />
</form>
END;

 

There are a lot of ways you can do this, though. I don't really think you need to be running a function to replace those variables. You're just making more work for yourself.

Link to comment
Share on other sites

yes it would. You could even make a function to do it so you don't have to post the code multiple times.

 

Note: A little rusty on functions so recode if needed.

 

function getSelectCode($options) {
   $selectCode = "<select name=\"type\">";
   foreach ($options as $key => $value) {
      $selected = "";
      if ($row['type'] == $key) $selected = " selected";

      $selectCode .= "<option value=\"{$value}\"{$selected}>{$value}</option>";
   }
   $selectCode = "</select>";

   return $selectCode;
}

 

I think that is correct. The return statement may be wrong. Just because I have been doing java coding a lot lately lol.

 

@teynon thank you.

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.