I created the below function which will handle both single choice and multiple choice drop down values and mark them as selected when the user views that drop down again.
bastien
<?
function my_select($name,$rows,$multiple,$array_list,$match)
{
//this select box takes the above 5 arguements to create details
/*
1. $name = the name of the select box
2. $rows = the number of displayed rows (1 as the default)
3. $multiple = does the select allow multiple choices (boolean value 0 for no, 1 for yes
4. $array_list = the options (in a value:display pair ie "1:Informatique","2:Voyages","3:Immobilier")
5. $match = the items to be selected if trying to keep a value ( is "" if no values present or comma separated list if multiple)
*/
$numOpts = count($match); if ($numOpts>1){ $matches=implode(",",$match); }elseif ($numOpts==1){ $matches=$match; }else{ $matches=""; }
echo "<select name=\"$name\" "; echo "size=\"$rows\" "; if ($mutiple==1){ echo " MULTIPLE "; } echo ">";
$count=count($array_list);
for ($i=0;$i<$count;$i++){ //take apart the array key:value pairs $elements = explode(":",$array_list[$i]);
//compare matches to list to see if the item is selected if (($numOpts<2)&&($matches == $elements[0])){ echo "<option value=\"".$elements[0]."\" SELECTED>".$elements[1]; }elseif(($numOpts>1)&&(in_array($elements[0],$matches))){ echo "<option value=\"".$elements[0]."\" SELECTED>".$elements[1]; }else{ echo "<option value=\"".$elements[0]."\">".$elements[1]; } }//end for echo "</select>";
}//end function ?>
bastien
From: Craig Hoffman <choffman@xxxxxxxxxx> To: php-db@xxxxxxxxxxxxx Subject: dropdown list - help Date: Mon, 4 Oct 2004 08:20:15 -0500
I can use some help here. I have page where a user can make updates to a DB record. The user can then select items from a drop down list. I'm trying to get my drop down list to pull from the MySQL and have the value that is stored in MySQL be the one selected in the option selected tag <option selected value="$rating"> . So if the user does not edit this field the original value remains unchanged.
For example: <select> <option selected value='$rating'> <option> All the other options to choose from</option> </select>
The tricky part is the actual value, in this case the ($rating) is stored in a table called routes. The options which I want in the drop down list are stored in a table called ranking. Both tables have a rating column.
My apologies if this is not clear, its gets confusing trying to explain it. Any help anyone can give would be great. Thanks - CH
Here is my code snippet: The drop down list is marked with //Drop Down List
<?php if($_POST["postback_edit_private_results"]); { include "include/db.php";
$route_count = $_POST["route_count"]; $username = $_POST["username"]; $user_id = $_POST["user_id"]; $fall = $_POST["fall"]; $rating = $_POST["rating"];
$query = "SELECT * FROM users, routes, ranking WHERE route_count = '$route_count' AND username='$username' AND routes.rating = ranking.rating';
echo $query;
$result = mysql_query($query,$db) or die(mysql_error());
if ($row = mysql_fetch_array($result))
{
echo ("<form method='post' action='confirm_receipt.php' encType='multipart/form-data'>");
echo ("<table cellspacing='0' cellpadding='3' border='1' id='upload_table'>
<tr id='alt_3'>
<td>* Route:</td>
<td><input type='text' name='route' size='20' value='$row[route]'></td>
</tr>
<tr id='alt_2'>
<td>* Area:
<br />i.e: Yosemite</td>
<td><input type='text' name='area' size='20' value='$row[area]'></td>
</tr>
<tr id='alt_3'>
<td>* Crag:
<br />i.e.: Half Dome</td>
<td><input type='text' name='outcrop' size='20' value='$row[outcrop]'></td>
</tr>
<tr id='alt_2'>
<td >Current Rating:</td>
<td> <select name='rating'>");
//Drop Down List
$rating = $_POST['rating'];
$options .="<option value=\"$rating\"";
if ($_POST['rating'] == "$rating") { $options .=" selected=\"selected\""; }
$options .=">$rating</option>\n";
echo $options; echo("</select></td>");
The rest of the page...
-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
_________________________________________________________________
Powerful Parental Controls Let your child discover the best the Internet has to offer. http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines Start enjoying all the benefits of MSN® Premium right now and get the first two months FREE*.
-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php