|
|
|
NewbieNotes is a little series of tips for people who are new to PHP to give them a few handy tips that the more experienced of us use often
something we all do over and over again is write select statements in forms. Worst of all the site owner invariably comes back six months later wanting another option adding!
All that messy going and getting the script back from the server (probably had a few tweaks since you wrote it), finding out where it was that this particular select was, what it did, how it's going to affect everything else etc can be a real chore.
Do it all sensibly - store all the options for your dropdown in a table in a database and use that to generate the drop down. Then when someone needs something changing you don't have to go near the code - you just fire up phpMyAdmin, add a record and it's done in seconds!
Ideally you want to make yourself a little function to allow you to create the drop down list whenever you want, wherever you want. Best way to do this is to start crating yourself a little library of functions you use all the time and include it in the scripts you write. If you do it this way then all you have to do to create a drop down list is to define the options in a database table, include your library at the start of your script and then add a single line of PHP to make it all happen.
There are many times that you'll use exactly the same drop down lists. Things like someone's title (Mr, Miss, Mrs), How they heard about you, what type of credit / debit card they want to use to buy things from you with etc. To this end it makes sense to have a separate database with all these commonly used lists in it so you don't have to re-create it or copy it every time.
Assuming you're going to use a separate database we'll start off by expanding what open_mysql_link from Newbie Notes #2 does...
| <?php
$sql_host = "localhost";
$sql_user = "username";
$sql_pass = "password";
$sql_data = "database";
function open_mysql_link($altdb = false)
{
if($altdb === false)
{
$db = $GLOBALS["sql_data"];
}
else
{
$db = $altdb;
}
if($link = mysql_pconnect($GLOBALS["sql_host",
$GLOBALS["sql_user"], $GLOBALS["sql_pass"]))
{
if(!mysql_select_db($db, $link))
{
$link = 0;
}
}
return $link;
}
?> | |
The original version of open_mysql_link() assumed you were only ever using one database, this one knows you might want to use an alternative database.
You may not have seen or used a function declaration quite like this before
function open_mysql_link($altdb = false)
This merely says that if I don't give you a $altdb parameter then set $altdb to false - this is called a default value for the parameter.
It means that you can call with or without a parameter. open_mysql_link() by itself sets $altdb to false while open_mysql_link("sundry") sets $altdb to "sundry". If you use this kind of declaration in your own functions note that default parameters must come at the end, you can't have a required parameter (no default) after a default parameter.
Our new open_mysql_link now wither opens "database" or some other database we tell it to by specifying the name of the database in the call.
On to creating your general drop down list...
Lets suppose that we want to create a drop down list of the states of the USA. Our table, at it's simplest, would have two fields. The two letter state code, e.g. CA, and the name of the state, e.g. California. Why not just store the names? Well you can do but CA is shorter and you might be storing this information hundreds of thousands of times so it makes sense to store the shorter version [note - an upcoming Newbie Note will expand titled 'Normal Data' on this idea a lot].
So - we have a table, the first field is called code and holds CA etc, the second field is calls state and hold California etc. You can create this quite quickly and will use it lots so it's worth the time doing it once because of all the time you'll save later.
Lets have some code then...
| <?php
function drop_down($database, $table, $name, $code, $text, $order = false, $default = false)
{
if($link = open_mysql_link("sundry"))
{
$sql = 'select ' . $code . ', ' , $text . ' from ' . $table;
if($order !== false)
{
$sql .= ' order by ' . $order;
}
if($res = mysql_query($sql, $link))
{
if(mysql_num_rows($res) > 0)
{
echo '<select name = \"' . $name . '\">\n';
while($row = mysql_fetch_array($res))
{
echo 'option value=\"';
echo $row[$code];
echo '\"';
if($code === $default)
{
echo ' selected';
}
echo '>';
echo $row[$text];
echo '</option>\n';
}
echo '</select>\n';
}
}
}
}
?> | |
This function performs everything you need to handle a database driven drop down box.
Usage Example
| <?php
drop_down("sundry", "states", "selState", "code", "name", "name", $selState);
?> | |
This invocation will use the states table in the sundry database. It'll read in and use the code and name fields of that table to build the drop down box. The list of options in the drop down will be sorted by the state name and the default selected option will be whatever is in $selSate which is useful if your form reloads itself on error as the users last selection will be already selected for them.
In much the same way as this automated drop down box works you can also automate radio buttons, check boxes and other common things you do in forms.
You can use this as a basis to write the other ones yourself. |
|
| Cut your MySQL Connections to 1 line of code Categories : PHP, Beginner Guides, Databases, MySQL | | | color codes for positive and negative numbers Categories : PHP, MySQL, Databases, HTML | | | email new items in db Categories : PHP, Email, Databases, MySQL, Beginner Guides | | | Specify your connection settings and create a link to a MySQL database. Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides | | | Convert a File database into MySQL Categories : PHP, Filesystem, Databases, MySQL, Beginner Guides | | | Newbie Notes #4 - Trapping dumb MySQL query errors Categories : PHP, Databases, MySQL, Debugging, Beginner Guides | | | mySQL/PHP/search with multientry
form and table output with colored rows Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases | | | This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | Paginator - a class that can help you to split MySQL database query result sets to pages. Categories : MySQL, Databases, HTML, PHP | | | for each record, do this to the first record, and do that to any subsequent record Categories : PHP, Databases, MySQL, Beginner Guides | | | How to Insert a Date Format Into MySQL from PHP Categories : PHP, Databases, MySQL, Date Time, Beginner Guides | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | Making a simple Hit-Log using PHP and MySql Categories : PHP, Log Files, Beginner Guides, Databases, MySQL | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | |
| | | | Potchavit Aphinives wrote :1094
If not to much option, I usually use array in such file as `config.inc.php` which is required in all scripts.
The array looks like:
$array1=("2","option1","option2");
and writing select option in the script by using
for($i=1; $i<=$array[0]; $i++){
echo "<option value=$i>$array1[$i]</option>
}
When I want to add two more options, I change this array to
$array1=("4","option1","option2","option3","option4")
| |
|
|
|