Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2007
    Posts
    24

    Question Unanswered: Listbox with multiple column in PHP

    I am using PHP and MySql,
    I want to display listbox with multiple column, i.e. ID,First_Name,Last_Name
    and save only the ID in database. How is it possible?

  2. #2
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    A multiple column listbox is not a valid html entity, and as far as i am aware does not exist.

    Tell me what this looks like :
    Code:
    <select>
      <option value="1">Jones, Tom</option>
      <option value="2">Smith, Clara</option>
      <option value="3">Goat, Garry</option>
      <option value="4">Rhubarb, Phil</option>
      <option value="5">Friend, My</option>
      <option value="6">Wonderland, Alice</option>
    </select>
    Outputting the ID is completely pointless, it is a useless piece of information for anyone using your application and therefore doesn't belong in the viewable text for your listbox.

    How is this listbox populated???? I bet it's populated from the Database isn't it?

  3. #3
    Join Date
    Apr 2007
    Posts
    24
    Yes, I want to show listbox values from database, with 2 column say first_name and Last_name and store ID in other database table.

  4. #4
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    In that case it's pretty much what i've posted above, just substitute in the ID for the value and the first_name last_name into a select html entity.
    e.g.
    Code:
    $conn = mysql_connect('localhost','username','password');
    mysql_set_db('dbname');
    $sql = "SELECT ID,first_name,last_name FROM <mytable>";
    $result = mysql_query($sql);
    
    echo "<select>";
    
    while($row = mysql_fetch_assoc($result)){
      echo "<option value=\"{$row['ID']}\">{$row['first_name']}, {$row['last_name']}</option>";
    }
    
    echo "</select>";

    WOOOO MY 300th POST !

  5. #5
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Put all of this in a <form> element and give the <select> box a name attribute and when you post the form you can retrieve the select value that was chosen using the $_POST superglobal.

Posting Permissions

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