|
|
|
<?
/*
File : fn_populateselectoptions.inc
Name : function PopulateSelectOptions()
Author : fernando ayala
Created : May 4, 2001
Version : 1.15
Description:
This function takes several arguments and generates
the <OPTION..>...</OPTION> HTML code fragment. A mysql_connect() is presumed
to exist already.
Arguments passed:
$dbname (required) Database name
$tblname (required) Table name
$tblkeyvarname (required) Name of table's key field
$tblkeyvaluesname (required) Name of column that describes key field (usually the
displayable value in the <select> options)
$defval (optional) Default key value, if any. <select> displays
this value by default
Return Value(s) $success: returns 1 if query was successful, 0 if query
failed
Usage: To use this, construct the following code in your .PHP file --
- - - - - - - - - - - - -
<? require("fx_populateselectoptions.inc"); ?>
<HTML>
...
<BODY>
...
<SELECT...>
<?php
PopulateSelectOptions ( $dbname,
$tblname,
$tblkeyvarname,
$tblvaluesname,
$defval
);
?>
</SELECT>
...
</BODY>
</HTML>
- - - - - - - - - - - - -
-----------
START FN:
-----------
*/
function PopulateSelectOptions ( $dbname, $tblname, $tblkeyvarname, $tblvaluesname,
$defval ) {
// Check passed arguments
if ( empty($dbname) ) {
echo "<!-- PopulateSelectOptions() Error: Database Name had no value --
>\n";
return 0;
}
if (empty($tblname) ) {
echo "<!-- PopulateSelectOptions() Error: Table name had no value -->\n";
return 0;
}
if (empty($tblkeyvarname) ) {
echo "<!-- PopulateSelectOptions() Error: Key Field name had no value --
>\n";
return 0;
}
if (empty($tblvaluesname) ) {
echo "<!-- PopulateSelectOptions() Error: Variable name for List Values
had no value -->\n";
return 0;
}
$success=0;
// Set up table query
$local_qry = "select * from " . $tblname;
$localresult=mysql_db_query( $dbname, $local_qry );
echo "<!-- DefVal passed was = " . $defval . " -->";
// Construct HTML code
if ($localresult) {
while( $row=mysql_fetch_array($localresult) ) {
echo "<option";
if ( isset($defval) ) {
if ( $row["$tblkeyvarname"]==$defval ) {
echo " selected ";
}
}
if (is_string($tblkeyvarname)) {
echo " value='".$row["$tblkeyvarname"]."'>";
} else {
echo " value=".$row["$tblkeyvarname"].">";
}
echo $row["$tblvaluesname"];
echo "</option>\n";
}
$success=1;
}
// Check if query was successful
if (!$success) {
if (!$localresult) {
echo "<!-- PopulateSelectOptions() Error: Could not access
database and/or table ".$dbname.".".$tblname.". -->\n";
}
}
return $success;
}
/*
----------
END FN:
----------
*/
?> |
|
| dynamic table columns Categories : PHP, HTML and PHP, Arrays, Databases, MySQL | | | Pull Down Surfing - Surf on Change Categories : Java Script, MySQL, HTML and PHP, PHP, Databases | | | Complex paging with no resultset limit Categories : PHP, MySQL, Databases, Output Control, HTML and PHP | | | Functions for loading images into a MySQL database and displaying them. Categories : Graphics, HTML and PHP, MySQL, PHP, Databases | | | phpFormGenerator for Dynamic Form Generation from MySQL Categories : PHP, PHP Classes, MySQL, Databases, HTML and PHP | | | Dynamically generated pop-ups (Select items) Categories : PHP, HTML and PHP, MySQL, Databases | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | Required form fields that pull from MySQL database Categories : PHP, HTML and PHP, Databases, MySQL | | | mySQL/PHP/search with multientry
form and table output with colored rows Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases | | | Record Set Paging with PHP (RSP) Categories : PHP, MySQL, Navigation, Databases, HTML and PHP | | | Database resultset navigation Categories : PHP, HTML and PHP, Databases, MySQL, Navigation | | | Storing / Retrieving pictures from database This can be used for banner / ads exchange. very useful for people developing big portal with ads in the site........ Categories : PHP, MySQL, Databases, HTML and PHP | | | Creating thumbnails from MySQL Blobs online Categories : PHP, MySQL, Graphics, HTML and PHP, Databases | | | 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 | |
|
|