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 3 of 3
  1. #1
    New Coder
    Join Date
    May 2008
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    The best way to get a drop down field to show data from a MySQL database

    Hi,

    I'm a little new to PHP and server side programming in general. But I was looking for some advice of what is the best way to get multiple drop down fields from within a search form display data from a MySQL database.

    And once the user has selected from the multiple fields and they have clicked "submit" for the user to be taken to a new page displaying the search results relating to their query.

    And help/advice with this would be appreciated.

    Many thanks
    Gary

  • #2
    New Coder
    Join Date
    Mar 2009
    Posts
    29
    Thanks
    0
    Thanked 2 Times in 2 Posts
    If I read you right, this is more a HTML problem. Read up on HTML forms and work out which method of multiple selections input you'd like (checkboxes, list) depending on how many options there are.

    Build the form as normal, when it's submitted, the receiving script can then use the varialbes passed from the form. With multiple selections you need to explode (PHP function) as these values will be comma seperated. From the array you can build your database query, same as normal.

  • #3
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Basically, the way to fill a dropdown from MySQL is this:

    PHP Code:

    <?php

    $query 
    "SELECT * FROM `table`";
    $result mysql_query($query) or die(mysql_error());

    print 
    "<select name=\"mySelect\">";

    while(
    $row mysql_fetch_assoc($result))
    {
      
    ?>
      <option value="<?php print $row['id']; ?>"><?php print $row['value']; ?></option>
     <?
    }

    print 
    "</select>";

    ?>

    Hope this helps,
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  

    Posting Permissions

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