PHP Function - drop-down based on database entries
This one is slightly trickier than the Array Based function, but at the same time it allows you to do a little more, too.
The function requires, as the name suggests, your values being stored in a database, so you will need to have a table in your database dedicated to that function.
Well, not really dedicated, since this script can work with any table you want.
Initiating the function:
/* *** drop down menu function served for DB selection *** */
/* *** usage: dropMenuDB('deliverto', $row['deliverto'], 'staff', 'id', 'name', '- Select Staff -', 'jobtypeID = '5' && active > '0''); *** */
function dropMenuDB($f_name, $f_value, $f_table, $f_key, $f_val, $f_default, $f_whereclause='', $f_css='', $f_tabindex='', $f_js='')
echo '<select name="' . $f_name . '" id="' . $f_name . '"';
if ($f_css != '') echo ' class="' . $f_css . '"';
if ($f_tabindex != '') echo ' tabindex="' . $f_tabindex . '"';
if ($f_js != '') echo ' ' . $f_js;
if ($f_whereclause != '') $wcl = ' where ' . $f_whereclause; else $wcl = '';
For the example let's use one of the tables created for this very site. Let's display all the projects that are listed in the main page.
I have this WP table where all of my posts, pages, attachments etc. are stored. Here it is:
Real Estate Manager
(This is only a small exerpt from the table to give you a better picture of what's going to happen.)
First Example - selecting everything from the Project table:
We want to narrow the display to only Posts printed out in the landing page. These posts are marked in the db as 'publish' (in "post_status" column) and 'post' (in "post_type" column).
To narrow the display we're going to use the 7th parameter from the function's options, $f_whereclause.
dropMenuDB('gregs_projects', '', 'gb_posts', 'ID', 'post_title', '- Select Published Project -', 'post_status = 'publish' AND post_type = 'post'');
The drop down menu was reduced to posts only.
This function, same as the Array Based one allows you to pre-select one item directly on the first display, eg. highligting your country so the websurfer doesn't have to scroll the whole list down.
For doing so, we going to use the 2nd parameter which initialy stayed empty. Say we wan't to pre-select one of my other sites, meGreg. We already know that the ID of this site is 47 so as the second parameter we would use 47 and that's it!
dropMenuDB('47_selected', '47', 'gb_posts', 'ID', 'post_title', '- Pre-select Published Projects of mine -', 'post_status = 'publish' AND post_type = 'post'');
Note, how easy is to change the name of your <select> tag. HTML result:
<select name="47_selected" id="47_selected">
<option value="">- Pre-select Published Projects of mine -</option>
<option value="4">CPW Living </option>
<option value="5">CPW Style </option>
<option value="6">Golden State Games </option>
<option value="46">meGreg </option>
<option value="47" selected>me, Greg </option>
<option value="48">Real Estate Manager </option>
<option value="119">Skillset / Services </option>
<option value="120"> </option>
By default the additional parameters are empty, so all you need to do is to fill the proper variable with your own input.
1. Add CSS to the dropdown
dropMenuDB('gregs_projects', '47', 'gb_posts', 'ID', 'post_title', '- Select Published Project -', 'post_status = 'publish' AND post_type = 'post'', 'free_db_class');
If you want your form's elements to be focused in particular order, you can add a value as the next-to-last parameter of the dropdown function ($f_tabindex).