...

View Full Version : Pulling Value from Table with a drop down



zelig
11-22-2011, 07:46 PM
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!!

myfayt
11-22-2011, 08:42 PM
Looks like the DK script, correct? If it is add a while loop with a select box in admin.php or if in the game, lib.php

If it's not that script, then we can talk about what to do. :thumbsup:

zelig
11-22-2011, 09:12 PM
It used to be, yeah. I've bastardized it to create a much more involved game, but there are parts of DK that have been swallowed and regurgitated into something different.

And add a while loop where abouts in the coding?

myfayt
11-22-2011, 09:25 PM
I've used DK before so I know the structure. Sounds like your editing a user in the admin cpanel.

Setting up a while loop drop down box would look like this.


$query = doquery("SELECT * FROM {{table}} WHERE id>'0'", "users");
$page .= '<select name="charname">';
while ($row = mysql_fetch_array($query)) {
$page .= "<option value=\"".$row['charname']."\">".$row['charname']."</option> ";
}
$page .= '</select>';

zelig
11-22-2011, 09:29 PM
Okay. That's awesome then!

Actually, I'm editing items, and there are multiple drop down menus.

In the form section, (like what I have pasted), do I need to change anything to make this work?

I pasted what you sent over but changed charname to type (since that is one of the drop down menus) and am trying it out now. How do I, though, make that populate the drop down menu?

Here is the php on it, so far, before the form:



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");
$page .= '<select name="type">';
while ($row = mysql_fetch_array($query)) {
$page .= "<option value=\"".$row['type']."\">".$row['type']."</option> ";
}
$page .= '</select>';
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");
}

}

Adee
11-22-2011, 09:59 PM
Okay. That's awesome then!

Actually, I'm editing items, and there are multiple drop down menus.

In the form section, (like what I have pasted), do I need to change anything to make this work?

I pasted what you sent over but changed charname to type (since that is one of the drop down menus) and am trying it out now. How do I, though, make that populate the drop down menu?

Here is the php on it, so far, before the form:



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");
$page .= '<select name="type">';
while ($row = mysql_fetch_array($query)) {
$page .= "<option value=\"".$row['type']."\">".$row['type']."</option> ";
}
$page .= '</select>';
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");
}

}


why do you have that select box in a while loop with a query that is updating the database..

zelig
11-22-2011, 10:08 PM
Ok, I guess I'm not entirely sure what is going on then. (long day at work)

What you sent over was the actual select box? Would I put that into the form portion, and if so, would it then recognize what other options are available so if it needed changed/updated, that could happen?

zelig
11-22-2011, 10:54 PM
Figured it out.

Thanks!

myfayt
11-22-2011, 11:35 PM
By the way the DK structure for items is using explode in an array. It's horrible, normalize the database and have every item inserted according to the player.

Such as ID 1, Player ID: 93, ItemID 12, Amount: 3, etc.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum