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

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 10-05-2012, 03:29 PM   PM User | #1
jawittdesigns
New Coder

 
Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
jawittdesigns is an unknown quantity at this point
How to post Multiple values from one selection

I have a while loop that generates a select dropdown.

The dropdown displays the name of the object from a database table for the user to select. When selected that name gets posted to another table.

PHP Code:
<?php
    $plane_sql 
"SELECT * FROM wp_flr_planes";
    
$plane_result mysql_query($plane_sql);
    echo 
'<input type="hidden"  value="" name="flt_plane_id"/>';
    echo 
'<select name="plane">';
    while (
$rows mysql_fetch_assoc($plane_result)) {
        
$plane_name $rows["plane_name"];
        
$plane_id $rows["id"];
        
$seating_amount $rows['seating_amount'];
        echo 
'<option value="'.$plane_name.'">'.$plane_name.'</option>';
    }
    echo 
'</select>';
?>
What I want to do is post the $plane_name, $plane_id, and $seating_amount from the option the user selected.
If this wan't a loop I would just use a hidden input field for that, but I'm not sure how to do that in the a loop.
jawittdesigns is offline   Reply With Quote
Old 10-05-2012, 04:30 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Short answer: don't.
Assuming that the id is the primary key of the wp_flr_planes, there is never any reason to carry the plane_name into a different table. You can simply join the records and look up the plane_name from the wp_flr_planes table. All you need to store is the primary key of the wp_flr_planes in order to look up any corresponding information.

If this is a composite key, then you do require both to be provided as the option value. The only way to do this in a select control is by using a method that allows you to pass both of the keys. An array of the two serialized would allow you to pass it as the option value, and you can unserialize it on the receiving side.
Fou-Lu is offline   Reply With Quote
Old 10-05-2012, 04:40 PM   PM User | #3
jawittdesigns
New Coder

 
Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
jawittdesigns is an unknown quantity at this point
OK I think I understand (maybe) what you are saying. Just store the ID then query the ID from the other table using the ID I just posted.

How would you do that? Would it it be something like a foreach loop then check if the ID exists? Does that sound right?
jawittdesigns is offline   Reply With Quote
Old 10-05-2012, 05:11 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yep, and nope. That would be a join:
Code:
SELECT wfp.plane_name, ynt.destination
FROM yournewtable ynt
INNER JOIN wp_flr_planes wfp ON wfp.id = ynt.planeid
WHERE wfp.plane_name = 'This is the plane name'
I can't really suggest a good query since I don't know what all of your properties are, and I'm not sure how you intend to use it after. I see something like a booking, so a plane has a known name and number of seats, and is used in a booking to see a destination and date/time of departure/arrival, etc.

If you use INNODB database engine, then you can add constraints as well (that are enforced). So in the secondary table, you can specify that it's "planeid" must reference the id in the wp_flr_planes. If it doesn't exist, it will throw an error, and you can set up cascading for updates and deletes.
Fou-Lu is offline   Reply With Quote
Old 10-05-2012, 09:12 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,209
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
On the other hand, if you really *MUST* pass all 3 values--or if, for example,you don't want to even use the DB on the next page--then you can do this:
Code:
    while ($rows = mysql_fetch_assoc($plane_result)) { 
        $plane_name = $rows["plane_name"]; 
        $plane_id = $rows["id"]; 
        $seating_amount = $rows['seating_amount']; 
        $item = $plane_id . "##" . $plane_name . "##" . $seating_amount;
        echo '<option value="' . $item . '">' . $plane_name . "</option>\n"; 
    }
That puts all three values into the <option> value and, on the next page, if you do
Code:
$item = $_POST["item"];
then you can split (explode, I think, in PHP terms?) on the ## to get the 3 separate values.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-09-2012, 02:35 PM   PM User | #6
jawittdesigns
New Coder

 
Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
jawittdesigns is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
On the other hand, if you really *MUST* pass all 3 values--or if, for example,you don't want to even use the DB on the next page--then you can do this:
Code:
    while ($rows = mysql_fetch_assoc($plane_result)) { 
        $plane_name = $rows["plane_name"]; 
        $plane_id = $rows["id"]; 
        $seating_amount = $rows['seating_amount']; 
        $item = $plane_id . "##" . $plane_name . "##" . $seating_amount;
        echo '<option value="' . $item . '">' . $plane_name . "</option>\n"; 
    }
That puts all three values into the <option> value and, on the next page, if you do
Code:
$item = $_POST["item"];
then you can split (explode, I think, in PHP terms?) on the ## to get the 3 separate values.
I almost got this method almost working for me. The problem I having is the the last option values for $item is being posted to the database instead of the option I select.
jawittdesigns is offline   Reply With Quote
Old 10-09-2012, 09:03 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,209
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Bring up the page in your browser.

Click on the VIEW menu of your browser.

Click on the SOURCE or PAGE SOURCE menu item.

Now you are looking at the HTML a the browser sees it.

In that HTML, find the <select> and <option>s that this code is producing.

Copy/paste that code here.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-09-2012, 09:05 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,209
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ummm...you do realize that my
Code:
$item = $_POST["item"];
was an example?

In your code, the <select> is <select name="plane"> so you would want to do
Code:
$item = $_POST["plane"];
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   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 11:13 PM.


Advertisement
Log in to turn off these ads.