|
|
|
Html Select box, with data taken from a db table
MySql example table creation:
CREATE TABLE name (
nname int(10) unsigned NOT NULL auto_increment,
name varchar(255) NOT NULL
KEY nname (nname, name)
)
Attention: table name and first column have the same name
db-connection.php
$dbaddress="localhost"; // subsstitute approrpiate data
$login="login"; // localhost,login, password,dbname
$password="password"; //
$dbname="dbname";
| <?php
function selectbox($name,$selected)
{
echo"<select name=\"$name\">";
include("../include/db-connection.php");
if(!($conn=mysql_connect($dbaddress, $login, $password)))
{echo "Unable to connect server";}
mysql_select_db($dbname, $conn);
$sql="SELECT $name, n$name FROM $name ORDER BY $name";
if (!($result=mysql_query($sql, $conn)))
{
echo "<font face=Arial size=2 color=#ff0000><br><br>Error in query (selectbox)<b><br><br>";//.$sql."<br><br>";
$sqlerrorm = mysql_error($conn);
echo ("error:$sqlerrorm\n<br><br>");
$sqlerrornum = mysql_errno($conn);
echo ("error n.:$sqlerrornum\n<br><br></font>");
exit;
}
else
{
while(($datarecord=mysql_fetch_row($result)))
{
echo "<option value=$datarecord[1] ";
if($datarecord[1]==$selected)
{
echo " selected>".$datarecord[0];
}
else
{
echo ">".$datarecord[0]."</option>";
}
}
}
echo"</select>";
mysql_close($conn);
}
?> | | |
|
| Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Automatically printing the contents of an sql table in MySQL. Categories : MySQL, PHP, HTML and PHP, Databases | | | Pull Down Surfing - Surf on Change Categories : Java Script, MySQL, HTML and PHP, PHP, Databases | | | Dynamically generated pop-ups (Select items) Categories : PHP, HTML and PHP, MySQL, Databases | | | Creating thumbnails from MySQL Blobs online Categories : PHP, MySQL, Graphics, HTML and PHP, Databases | | | Record Set Paging with PHP (RSP) Categories : PHP, MySQL, Navigation, Databases, HTML and PHP | | | Functions for loading images into a MySQL database and displaying them. Categories : Graphics, HTML and PHP, MySQL, PHP, Databases | | | dynamic table columns Categories : PHP, HTML and PHP, Arrays, Databases, MySQL | | | Editing the virtusertable and sendmail.cw via PHP3.0 and Mysql Categories : MySQL, HTML and PHP, PHP, Databases | | | This function will populate the options in a drop down HTML select list
in a form from a database query.
Categories : MySQL, General SQL, PHP, HTML and PHP, Databases | | | How can i Preload a 'SELECT MULTIPLE'? Categories : HTML and PHP, PHP, MySQL, Databases | | | Paginating the mySQL data Categories : PHP, Algorithms, Databases, MySQL, HTML and PHP | | | PHP and MySQL scripting for Muyltiple CheckBoxes Categories : HTML and PHP, MySQL, Databases, PHP | |
| | | | Jose Santos wrote : 1144
I use similary to this so mutch ways !!
I recomend to use de DB package of the PEAR (http://pear.php.net) of the PHP, because if you change for example from mysql to oracle.
You dont need modidy theses scripts !! Only had the database settings in a core script, used for all others scripts. And when you change to Oracle, change only the core script.
| | | | Sarah King wrote :1152
I use this type of code alot too. I would recommend a change to the while loop so that you are only doing a single echo (for performance and readability)
while(($datarecord=mysql_fetch_row($result)))
{
$select_string = ($datarecord[1]==$selected)?`selected`:``;
echo = "<option value=`{$datarecord[1]}` {$select_string}>{$datarecord[0]}</option>";
}
| |
|
|
|