get the matching id of the selected item in the listbox

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

get the matching id of the selected item in the listbox

Post by zplits »

Hi there, good day to all. Does any know how to get the matching id of the selected item in the listbox?

If you don't know what i mean, here is a screenshot below. To help you to better understand what is my prob. The screenshot below show the supplies page, it is here that i enter and save data into the database.
Image

And in the image below, the saved items in the supplies page will be use here,
Image

My problem:
I have only 1 problem and that is to get the code for the selected description in the drop-down list box. For example i choose cheese in the description field, and fill up other info, then when i press the save button, the data will be saved in the database and be displayed in the table below. Because i have chosen cheese the code that will appear in the table will be "1211". Because in the supplies page, cheese has the code of 1211.

Any help? Thanks in advance.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: get the matching id of the selected item in the listbox

Post by yacahuma »

your description should already have the value

Code: Select all

 
<select name="description">
<option value="1211">Cheese</option>
<option value="0019">Milk</option>
<option value="0007">Onion</option>
</select>
 
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: get the matching id of the selected item in the listbox

Post by zplits »

thanks for the response sir, Here is the code for the select box

Code: Select all

<?php 
$querySupplies = "SELECT supplies_PK, code, name, quantity, measurement, perUnitCost, totalCost FROM $tbl_name2";
$resultSupplies = mysql_query($querySupplies,$connection);   
$getResultSupplies = $resultSupplies;
 
echo "<select name=\"description\" size=\"1\" class=\"formFieldsDropDownCode\" id=\"code\" tabindex=\"4\">";
 
   while($row = mysql_fetch_assoc($getResultSupplies)){
        $showName=$row['name'];
        echo "<option value=".$showName.">".$showName."</option>";
    }
    //echo "<input type=\"hidden\" name=\"code\" id=\"hiddenField\" value=".$code."/>";
    echo "</select>";
                ?>
I want to match the selected item with it's corresponding code no. Please help me out.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: get the matching id of the selected item in the listbox

Post by yacahuma »

Code: Select all

 
 while($row = mysql_fetch_assoc($getResultSupplies)){
        $showName=$row['name'];
         $code=$row['code'];
        echo "<option value=".$code.">".$showName."</option>";
    }
 
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: get the matching id of the selected item in the listbox

Post by zplits »

Thanks...I have made what you have advised, it's successful, but one more thing, the i can't save the selected description in the database.

This is the code:

Code: Select all

while($row = mysql_fetch_assoc($getResultSupplies)){
        $showName=$row['name'];
         $code=$row['code'];
        [b][color=#BF0000]echo "<option value=".$code.">".$showName."</option>"[/color][/b];
    }
take note of the bolded line of code, the value is $code, how can i also get the value of $showName and save it in the database?
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: get the matching id of the selected item in the listbox

Post by yacahuma »

you are not supposed to . Thats how relational tables work. to get the description you join the tables

http://www.w3schools.com/Sql/sql_join.asp
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: get the matching id of the selected item in the listbox

Post by onion2k »

Mmm.. Kraft Cheese.

:P
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: get the matching id of the selected item in the listbox

Post by Stryks »

I still don't get the table (for the purposes of this post, 'table' means the grid you are displaying the data in, not a database table) you are using to display the information.

I mean, I might be missing something, but you're carrying the quantity and cost values though three stages per row. What happens if the price changes? What about days where there are no purchases? What happens if you change supplier or manufacturer - Two rows for one product?

And having the date column makes no sense to me for listing all items. If you have 20 different items, you're going to have 20 rows of data, per day ... apparently for as many days as you have stored. This is not going to tell anyone anything at a glance.

I've tried to mention this to you once, but I'll try again. The data you are showing should, I believe, be in *at least* two separate tables. One without dates showing quantity on hand and total value (one row per product) and a second drill-down table showing the last, say 30 days of purchases and usages and the dates those changes occurred. This table might also show the price paid.

Anyhow, on topic, you store the ID and join in your SQL to pull the name. I know you don't really understand that, so you're going to have to go on faith a bit. It's just the way it's done.

Cheers
Post Reply