CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   MySQL (http://www.codingforums.com/forumdisplay.php?f=7)
-   -   How to post Multiple values from one selection (http://www.codingforums.com/showthread.php?t=275317)

jawittdesigns 10-05-2012 03:29 PM

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.

Fou-Lu 10-05-2012 04:30 PM

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.

jawittdesigns 10-05-2012 04:40 PM

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?

Fou-Lu 10-05-2012 05:11 PM

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.

Old Pedant 10-05-2012 09:12 PM

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.

jawittdesigns 10-09-2012 02:35 PM

Quote:

Originally Posted by Old Pedant (Post 1276737)
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.

Old Pedant 10-09-2012 09:03 PM

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.

Old Pedant 10-09-2012 09:05 PM

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"];


All times are GMT +1. The time now is 01:33 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.