View Full Version : PHP and drop-down lists
john010117
06-20-2007, 01:09 AM
I want a simple HTML drop-down list like this:
<select name="select_1">
<option name="option_1" value="option_1" selected="true">
<option name="option_2" value="option_2" selected="false">
</select>
to be able to automatically select a value according to a value in a table in the database. For example, if a MySQL table has a field named "field_1", and one of the rows has a value of "option_1" in that field, I want the list (using PHP of course) to automatically select the option name "option_1". How would I go about doing that?
whizard
06-20-2007, 01:19 AM
Misunderstood Question ... rewriting answer
How is the select box being created? What you want to do is easiest to do while the box is being created.
Wherever you create the select box, a simple if statement should do the trick:
//Assumes that $value1 holds the value of field_1 (ie option_1) and $value2 holds the value that you are setting in the option tag
if($value1 = $value2)
{
print("selected=\"selected\"");
}
HTH
Dan
john010117
06-20-2007, 01:26 AM
I thought about doing that in the first place, but I had way too many drop-down lists AND options to do an if statement for each one. Is there another way?
whizard
06-20-2007, 01:34 AM
Once again, what process is used to create the select boxs?
Dan
john010117
06-20-2007, 01:44 AM
Oh, sorry about that. First, just the form (with the drop-down box) appears (with the values from the MySQL database already filled in). When the user has submitted edits to it, a PHP code processes it. If there are any errors, it shows an error message and the form again to correct any mistakes.
whizard
06-20-2007, 01:58 AM
When the select box is filled from the DB, it's through a loop, correct? Then wouldn't it be simple enough to add an if statement to the loop, something like this:
$value = "VALUE OF FIELD_1, THAT YOU WANT TO BE SELECTED"
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
print("<option value=\"".$row['field_1']."\"");
if($value = $row['field_1'])
{
print(" selected=\"selected\"");
}
print(">Option</option>");
}
That way, it doesn't matter how many options you have, there's only a cople of lines of code to add. I can't really think of a different way, so sorry if it seems like I am ignoring you when you asked for a different way.
Maybe one of the great minds in this forum will come up with a better solution, if mine is no good
HTH
Dan
john010117
06-20-2007, 02:05 AM
Actually, I expanded on your idea. I made a simple function to echo the values, and made an array with all the options in it. Thanks for your help, though.
whizard
06-20-2007, 02:07 AM
Glad you got it fixed :thumbsup:
Dan
daemonkin
06-20-2007, 09:27 AM
PS Whizard thats the way I would have done it too!
I generally use smarty templates for my forms anyway giving me a very easy to use system for building drop downs and inserting values.
D.
Cyber_type
06-20-2007, 02:44 PM
this my first post so, first of all, Hi to everybody.
So whit your code (thankx) i created a drop down list with country names.
Here's my code:
Mysql table
CREATE TABLE pays (iso varchar(2) NOT NULL,nom varchar(50) NOT NULL,UNIQUE KEY iso (iso)) ENGINE=InnoDB DEFAULT CHARSET=latin1;
this the form (only the select option)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Select Menu</title>
</head>
<form method="POST" action="insertion_1.php">
<p><b>Select table from country table</b>
<table align="center" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Select country</td>
<td><select name="pays" id="pays">
<?php
$dbhote = 'localhost';
$dbuser = '******';
$dbpass = '******';
$dbbase = '******';
$db = mysql_connect($dbhote,$dbuser,$dbpass);
mysql_select_db($dbbase,$db);
$sql = "SELECT nom FROM pays";
$req=mysql_query($sql) or die('Erreur SQL !'.$sql.'<br>'.mysql_error());
while($row = mysql_fetch_array($req))
{
print("<option value=\"".$row['nom']."\"");
if($value = $row['nom'])
{
print(" selected=\"selected\"");
}
print(">Option</option>");
}
mysql_close();
?>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Go!"></td>
</tr>
</table>
</p>
</form>
<body>
</body>
</html>
it works but got little problem: when you run form page, it load table content(pays) or country but it only displays "option, option....." not the name's country....seems that i have a problem with "print("<option value=\"".$row...." but can't find where (newbie...:confused: )
Anyway your code has already done what a was looking for, populate a drop down list with table content so very greatfull to yours.:thumbsup:
Best regards
Bruno
Edit PS. if someone want the txt file with all country's name(more than 200) to populate mysql table, please send pm
whizard
06-20-2007, 02:54 PM
That is being caused by this line:
print(">Option</option>");
Change it to
print(">".$row['nom']."</option>");
and it should work.
:thumbsup:
Dan
Cyber_type
06-20-2007, 03:00 PM
wow, that's what i call a "speedy anwser". Thank's lot Whizard :thumbsup: :thumbsup: :thumbsup:
whizard
06-20-2007, 03:23 PM
I could be wrong (it has happened before), but from your code, it looks like you don't need this part:
if($value = $row['field_1'])
{
print(" selected=\"selected\"");
}
That was just so that the select box would load with a certain field already selected. It doesn't look like you need that part, so you can take it out if you want, and combine these two lines:
print("<option value=\"".$row['nom']."\"");
print(">Option</option>");
like this:
print("<option value=\"".$row['nom']."\">".$row['nom']."</option>");
HTH
Dan
Mhtml
06-20-2007, 04:13 PM
if($value = $row['field_1'])//this is always going to be so?
whizard
06-20-2007, 06:40 PM
$value actually held the result of an earlier mysql query, the value that 'john' wanted to be selected when he built the select box
So it wasn't related to the current query/while loop
Dan
He's referencing that the code is setting $value to $row['field_1'] instead of checking if they're equal (= vs. ==)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.