Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 18-03-2009
Member
 
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..
Reply With Quote
  #2  
Old 18-03-2009
Member
 
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>
Reply With Quote
  #3  
Old 18-03-2009
Member
 
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>
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Populate a Drop down list in php from SQL"
Thread Thread Starter Forum Replies Last Post
Auto populate a number of cells from a drop down selection in Excel LuniA MS Office Support 2 21-02-2012 07:48 PM
Add/remove programs will not populate list Chucky Windows XP Support 4 25-10-2011 08:36 PM
*HELP* MS Word 07 - Populate a text field based on drop-down list selection klutzington Software Development 1 09-09-2011 03:38 PM
Populate Text Field Based On Drop-Down List Selection Kaalicharan Software Development 6 21-07-2010 09:59 AM
How can i empty a drop down list in VB.Net McGrawh Software Development 3 11-06-2009 06:13 PM


All times are GMT +5.5. The time now is 11:49 PM.