Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Drop Down Box

30 views
Skip to first unread message

Bob Rice

unread,
Mar 30, 2006, 7:58:59 PM3/30/06
to

I need to construct a drop down box that selects records from a sql table.
I have constructed the code to populate the drop down box, but I do not know
what to do with the result when a selection is made. I want to use the
selection to define a criteria so that I can then use the result from the
drop down box in a sql SELECT statement to get the proper records from the
table and present them.

I do not care if I have a select button or not. I have a table, students,
and the drop down box will be based upon the filed, LastName in the table.

Any help is most appreciated.
--
Bob Rice


fiziwig

unread,
Mar 30, 2006, 10:27:38 PM3/30/06
to
I use a dropdown for my navigation bar in one of my sites. The code
looks like this:

Navbar has the form:

<form action="redirect.php" method="GET">
<select name="pg">
<option value="0">- Start Here -
<option value="1">Home
<option value="2">Login
<option value="3">Search
<option value="4">Browse by Author
<option value="5">Browse by Title
<option value="7">Browse by Publication
<option value="8">Database Add or Change
<option value="6">Edit Membership Info
<option value="9">Register
<option value="10">Help
<option value="11">Contact Us
</select>
<input type="submit" value=" GO ">
</form>

redirect.php uses it this way:

if (isset($_GET['pg'])) {
$page=$_GET['pg'];
}
switch ($page) {
case 0 :
case 1 : $goto='somepage.php'; break; // home
case 2 : $goto='somepage.php'; break; // login
case 3 : $goto='somepage.php'; break; // search
case 4 : $goto='somepage.php'; break; // browse author
case 5 : $goto='somepage.php'; break; // browse title
case 6 : $goto='somepage.php'; break; // edit membership
case 7 : $goto='somepage.php'; break; // Browse by publication
case 8 : $goto='somepage.php'; break; // database update
case 9 : $goto='somepage.php'; break; // register
case 10 : $goto='somepage.php'; break; // Help
case 11 : $goto='somepage.php'; break; // Contact
case 12 : $goto='somepage.php'; break; // Logout
case 13 : $goto='somepage.php?ak='.$extra; break;
case 14 : $goto='somepage.php?ak='.$extra; break;
case 15 : $goto='somepage.php?tk='.$extra; break;
}
$url='http://' . $_SERVER['HTTP_HOST'] .
dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\')){
$url = substr($url,0,-1); // chop off any slash
}
$url .= '/'.$goto;
header("location: $url"); // bail out to the redirect page
exit(); // and stop executing this script
}

Go ye and do likewise.

--gary

Geoff Berrow

unread,
Mar 31, 2006, 1:21:50 AM3/31/06
to
Message-ID: <1143775658....@v46g2000cwv.googlegroups.com> from
fiziwig contained the following:

> if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\')){

spot the error...
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011

Geoff Berrow

unread,
Mar 31, 2006, 1:23:49 AM3/31/06
to
Message-ID: <leip22h9mvs6hip5f...@4ax.com> from Geoff
Berrow contained the following:

>
>> if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\')){
>
>spot the error...

Ooops, my bad. Not an error.

Memo to self. Don't try to be smart early in the morning.

sdk

unread,
Mar 31, 2006, 2:18:41 AM3/31/06
to

define ("SELECTALL", "SELECT * FROM `%s` ");
define ("SELECTBOX", "\n<select name=\"%s\">\n%s\n</select>\n");
define ("OPTION", "\n<option value=\"%s\">\n%s\n</option>");
define ("OPTIONSEL", "\n<option value=\"%s\"
selected>\n%s\n</option>\n");
...
function selectbox($sometable,$selected=NULL)
{
$query=sprintf(SELECTALL,$sometable);
$result = mysql_query($query);
while ($row = mysql_fetch_row($result))
{
if($row[0] != $selected)
{
$output .= sprintf( OPTION,$row[0],$row[0]);
}
else
{
$output.=sprintf(OPTIONSEL,$row[0],$row[0]);
}
}
return sprintf(SELECTBOX,$table,$output);
}


usage:


<? selectbox("firstnames","john"); ?>

would produce a selectbox with john selected.

strawberry

unread,
Mar 31, 2006, 7:16:52 AM3/31/06
to
Here's how I do it...


The form:

echo "<select name='student'>\n";
echo "<option value=''>Select...\n";
while ($row = mysql_fetch_array ($students,MYSQL_ASSOC))
{
echo "<option value={$row['student_id']}>{$row['f_name']}
{$row['l_name']}\n";
}
echo "</select>\n";

The query that populates this list goes something like this. Apologies
for any errors:

//Get a list of all students (e.g. student_id,f_name,l_name) and order
them by last name.
//Put records with no last name at the bottom of the list.
$query = "SELECT * ,
IF(l_name IS NULL or l_name='', 1, 0)
AS isnull
FROM students
ORDER BY isnull ASC, l_name ASC;";

$students = mysql_query($query) or die ("Couldn't gather a list of
students!!!");

The trick is simply passing the student_id to the 'option value'
statement.

HTH

0 new messages