Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,449
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    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.

  • #6
    New Coder
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,449
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    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.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,449
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •