Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-22-2011, 06:46 PM   PM User | #1
zelig
New Coder

 
Join Date: Nov 2011
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
zelig is an unknown quantity at this point
Pulling Value from Table with a drop down

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:

Code:
<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!!
zelig is offline   Reply With Quote
Old 11-22-2011, 07:42 PM   PM User | #2
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
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.
__________________
Been a sign maker for 5 years. My business:
American Made Signs
myfayt is online now   Reply With Quote
Old 11-22-2011, 08:12 PM   PM User | #3
zelig
New Coder

 
Join Date: Nov 2011
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
zelig is an unknown quantity at this point
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?
zelig is offline   Reply With Quote
Old 11-22-2011, 08:25 PM   PM User | #4
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
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.

PHP Code:
$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>'
__________________
Been a sign maker for 5 years. My business:
American Made Signs
myfayt is online now   Reply With Quote
Old 11-22-2011, 08:29 PM   PM User | #5
zelig
New Coder

 
Join Date: Nov 2011
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
zelig is an unknown quantity at this point
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:

PHP Code:
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");
        }        
        
    } 

Last edited by zelig; 11-22-2011 at 08:32 PM..
zelig is offline   Reply With Quote
Old 11-22-2011, 08:59 PM   PM User | #6
Adee
Regular Coder

 
Join Date: Jul 2010
Location: Oregon City
Posts: 280
Thanks: 5
Thanked 50 Times in 49 Posts
Adee can only hope to improve
Quote:
Originally Posted by zelig View Post
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:

PHP Code:
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..
Adee is offline   Reply With Quote
Users who have thanked Adee for this post:
zelig (11-22-2011)
Old 11-22-2011, 09:08 PM   PM User | #7
zelig
New Coder

 
Join Date: Nov 2011
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
zelig is an unknown quantity at this point
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 is offline   Reply With Quote
Old 11-22-2011, 09:54 PM   PM User | #8
zelig
New Coder

 
Join Date: Nov 2011
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
zelig is an unknown quantity at this point
Figured it out.

Thanks!
zelig is offline   Reply With Quote
Old 11-22-2011, 10:35 PM   PM User | #9
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
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.
__________________
Been a sign maker for 5 years. My business:
American Made Signs
myfayt is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:44 PM.


Advertisement
Log in to turn off these ads.