Results 1 to 3 of 3

Thread: Populate a Drop down list in php from SQL

  1. #1
    Join Date
    Feb 2009
    Posts
    71

    Populate a Drop down list in php from SQL

    Does anyone here know how to populate a dropdown list in PHP from a MySQL table? where the list would be all the records from a particular column? I am using windows XP pro.

    Thanks for your help..

  2. #2
    Join Date
    Apr 2008
    Posts
    193

    Re: Populate a Drop down list in php from SQL

    Creates a simple drop down list from a SQL query. Column 1 of the query should be the reference, column 2 should be the descriptive text / list entry. You still have to create the <select name=""> tag within your HTML.

    The default value is used to preselect an entry. The value of default is a value that will be in column 1 of the query results.

    The value for blank is inserted at the top of the drop down list if required, either as a default value or for informative or instructive text.

    PHP Code:
    <?php
    function db_createlist($linkID,$default,$query,$blank)
    {
        if(
    $blank)
        {
            print(
    "<option select value=\"0\">$blank</option>");
        }

        
    $resultID pg_exec($linkID,$query);
        
    $num       pg_numrows($resultID); 
        
        for (
    $i=0;$i<$num;$i++)
        {
            
    $row pg_fetch_row($resultID,$i);
            
            if(
    $row[0]==$default)$dtext "selected";
            else 
    $dtext "";
        
            print(
    "<option $dtext value=\"$row[0]\">$row[1]</option>");
        }
    }
    ?>
    Example:
    PHP Code:
    <select name="select">
    <?php 
        
    // default is 0, no entry will be selected.
        
    db_createlist($linkID,0,
                
    "select id,description from list","Please select one ...");
    ?>
    </select>

  3. #3
    Join Date
    Mar 2008
    Posts
    258

    Re: Populate a Drop down list in php from SQL

    Hi

    I hope i can explain this well!!

    I have a dynamic drop down list (populated using a MYSQL query and PHP). When the user selects an item from the list and submits it runs another SQL query to pull items from another table where id equals that of the selected drop down item.

    Code:
    <form method="POST" action="view_project.php">
    	<input type="hidden" name="sector" value="sector_list">
    	<select name="sector_list" class="inputstandard">
    	<option value="default">Select Sector</option>
    
    	<option value="1" name="Housing & Residential">Housing & Residential</option><option value="2" name="Retail">Retail</option><option value="3" name="Public Sector">Public Sector</option><option value="4" name="Hotel & Leisure">Hotel & Leisure</option><option value="5" name="Commercial Development">Commercial Development</option>		
    	</select>
    	<input type="submit" value="Go!">
    	</form>
    PHP CODE

    PHP Code:
    <form method="POST" action="view_project.php">
        <input type="hidden" name="sector" value="sector_list">
        <select name="sector_list" class="inputstandard">
        <option value="default">Select Sector</option>

        <?
            $result 
    mysql_query('SELECT * FROM team ORDER BY T_ID ASC'
            or die (
    mysql_error()); 
        
            while (
    $row mysql_fetch_assoc($result)) {
                echo 
    '<option value="' $row['T_ID'] . '" name="' $row['T_Team']. '">' $row['T_Team']. '</option>';
            }
        
        
    ?>
            
        </select>
        <input type="submit" value="Go!">
        </form>

Similar Threads

  1. Replies: 2
    Last Post: 21-02-2012, 07:48 PM
  2. Add/remove programs will not populate list
    By Chucky in forum Windows XP Support
    Replies: 4
    Last Post: 25-10-2011, 08:36 PM
  3. Replies: 1
    Last Post: 09-09-2011, 03:38 PM
  4. Populate Text Field Based On Drop-Down List Selection
    By Kaalicharan in forum Software Development
    Replies: 6
    Last Post: 21-07-2010, 09:59 AM
  5. How can i empty a drop down list in VB.Net
    By McGrawh in forum Software Development
    Replies: 3
    Last Post: 11-06-2009, 06:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,664,944,708.65985 seconds with 17 queries