Html Xmlns Blackberry Array Cat Id Laserjet 4100 Multimedia Products CMS Strange Person Rant Telegraph Variables Football Game Web Applications Borders Joomla Div Id Server Hostname Stylesheet Honesty Bandwidth Usage Index Last Straw Video And Multimedia Import Functionality Dashboard Content Management System SQL tools Ideas Intranet Information Systems Web App Phpmyadmin Nate Web Development Fellow Web Web Screen Resolutions Internet Explorer 7 Css File Import Search Google Config Php Gsa Gov Select Name Capital Letters Transparent Image organization Doctype database Server Username rss mySQL Term Relationships Habit Addressbook steps Tip Mobile Platforms Color Scheme Section 508 Sql Server Microsoft 2.6. 2.62 Mysql Fetch Array tricks Lotus Longest Time blog Templates W3c Telecommunications Products free templates Information Technology Products Time Frame Design System Administrator Strategy AJAX Login Php Full Time Page Password Web Developer Husker Football Email Account Reference App Mumbo Jumbo Tag Double Benefit Mysql Num Rows Time Web Development Database Functionality Variation Color Schemes Dynamic Css Web Design PHP World Wide Web develop includes Br Database Import Forefront SEO Php Database Gods Gift Internet Explorer Html Tag Free Csv Search Engine Optimization Insanity redirect 26b Transitional Dtd Accurate Time Marketing Php File University Web encryption Echo WordPress box model Lt Password Reset Adminemail Gsa Div Email Settings Asynchronous Javascript And Xml Portable Computers Address Book Sql Database Transition Export Php Variables Integrity Colors Branding Download PHP Tips Area 51 Hourly Fee Free Download Headaches Seo Search Engine Optimization Membership Database Categories tips Divs Formatting Indexing Services Php Password developer Services Software Page Rank News Releases View Source Google XML Hourly Rates Traveling Lotus Notes Images Links Firefox 2.1 Software Applications New Features Nuances Lot Css Files Ektron css Federal Employees Php Echo Working With Clients Work Log Php Mysql Social Networking Deans List Cms Content Membership Login Military Installation Web Developers
Html Xmlns Blackberry Array Cat Id Laserjet 4100 Multimedia Products CMS Strange Person Rant Telegraph Variables Football Game Web Applications Borders Joomla Div Id Server Hostname Stylesheet Honesty Bandwidth Usage Index Last Straw Video And Multimedia Import Functionality Dashboard Content Management System SQL tools Ideas Intranet Information Systems Web App Phpmyadmin Nate Web Development Fellow Web Web Screen Resolutions Internet Explorer 7 Css File Import Search Google Config Php Gsa Gov Select Name Capital Letters Transparent Image organization Doctype database Server Username rss mySQL Term Relationships Habit Addressbook steps Tip Mobile Platforms Color Scheme Section 508 Sql Server Microsoft 2.6. 2.62 Mysql Fetch Array tricks Lotus Longest Time blog Templates W3c Telecommunications Products free templates Information Technology Products Time Frame Design System Administrator Strategy AJAX Login Php Full Time Page Password Web Developer Husker Football Email Account Reference App Mumbo Jumbo Tag Double Benefit Mysql Num Rows Time Web Development Database Functionality Variation Color Schemes Dynamic Css Web Design PHP World Wide Web develop includes Br Database Import Forefront SEO Php Database Gods Gift Internet Explorer Html Tag Free Csv Search Engine Optimization Insanity redirect 26b Transitional Dtd Accurate Time Marketing Php File University Web encryption Echo WordPress box model Lt Password Reset Adminemail Gsa Div Email Settings Asynchronous Javascript And Xml Portable Computers Address Book Sql Database Transition Export Php Variables Integrity Colors Branding Download PHP Tips Area 51 Hourly Fee Free Download Headaches Seo Search Engine Optimization Membership Database Categories tips Divs Formatting Indexing Services Php Password developer Services Software Page Rank News Releases View Source Google XML Hourly Rates Traveling Lotus Notes Images Links Firefox 2.1 Software Applications New Features Nuances Lot Css Files Ektron css Federal Employees Php Echo Working With Clients Work Log Php Mysql Social Networking Deans List Cms Content Membership Login Military Installation Web Developers

This is really a very basic and very simple method of populating a select box with database data. For example, If you have a list of states in your database, you could create a drop-down box that you can select all the states without a long drawn out piece of code. Here’s what your table entitled ’states’ MIGHT look like

StateID (Primary Key)
Abbr
Name

This table simply includes an auto incrementing key, the state abbreviation and the state name. Now here is your code to create the drop-down!

<?php
### CREATE DATABASE LINK ###
$hostname=’localhost’;
$username=’username’;
$password=’password’;
$dbname=’databasename’;
mysql_connect($hostname, $username, $password) or die(mysql_error()) ;
mysql_select_db($dbname) or die(mysql_error()) ;

### FUNCTION ###
function getStateSelectBox() {
 $SQLstate = “SELECT * FROM `states` ORDER BY state ASC”;
 $results = mysql_query($SQLstate);
 $output = “<select name=\”state\”>\n”;
 while ($rows = mysql_fetch_array($results)) {
  $output .= “     <option name=\”$rows[1]\”>$rows[2]</option>\n”; 
 }
 $output .= “</select>”;
 return $output;
}

### DISPLAY OUTPUT ###
$stateselectbox = getGenreSelectBox();
echo $stateselectbox;
?>

This can be used for nearly any site or drop-down if its database driven. Note the “\n” at the end of some of the $output variables because in case you didn’t know, this forces a lie break so its cleans up the look of the HTML. Its not needed, but nice if you prefer some form of sanity to your HTML output.An example of the output html code would be like this with this formatting, otherwise it would all be in one continuous line:

<select name=”state”>
     <option name=”IA”>Iowa</option>
     <option name=”NE”>Nebraska</option>
etc….
</select>

I hope you find this useful…I know its very simple, but its beneficial to know.

No comments yet.

Would you like to login?