Forum Moderators: coopster

Message Too Old, No Replies

Select List/Menu using PHP and MYSQL

How to select a specific option value

         

Xman117

4:46 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



Here is my code:

<select name="country" class="sidelinks">
<?php
do
{
?>
<option value="<?php echo $row_AllCountries['country_ID']." ";?>"<?php if ($row_AllCountries['country_ID'] = $acountry) { echo "selected"; }?> >
<?php echo $row_AllCountries['countryname'];?></option>
<?php
}
while ($row_AllCountries = mysql_fetch_assoc($AllCountries));
?>
</select>

A few words to describe:
-$row_AllCountries['country_ID'](get from MySQL) is a unique country ID number
-$row_AllCountries['countryname'](get from MySQL) is the name of the country
-$acountry is the country ID number that i want to be selected when the form loads

I have all the values and country names named OK...the only problem is that when the form loads it selects the last country

Does anyone know wheres the mistake?
Thanks!

mcibor

6:20 pm on Jan 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<select name="country" class="sidelinks">
<?php
do
{
?>
<option value="<?php echo $row_AllCountries['country_ID']." ";?>"<?php if ($row_AllCountries['country_ID'] = $acountry) { echo "selected"; }?> >
<?php echo $row_AllCountries['countryname'];?></option>
<?php
}
while ($row_AllCountries = mysql_fetch_assoc($AllCountries));
?>
</select>

The statement in if is always true, that's why all <option selected> are. And that's why you end up with last option selected.

I'm not sure what you wanted to do, but
($row_AllCountries['country_ID'] = $acountry)
is not a comparison, but assignment. If you want to compare if $row... equals $acountry, then use == operator.

if ($row_AllCountries['country_ID'] == $acountry) echo "selected";

Best regards!
Michal Cibor

Xman117

7:47 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



I misstyped = ... it was == but it doesnt work anyway!

I am new to this kind of stuff...sorry! I'll try to explain it one more time.
I have a MySQL database where there are stored country names and their serial numbers(unique for each country).
I have a page with a form and there is a List Box(menu) with this countries in it.
When the page starts I want a specific country to be selected and that country ID to be equal to $acountries.

Thanks and best regards
Rok

mcibor

9:59 pm on Jan 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's one more mistake: do..while loop is being performed one time too much:

eg there are 3 rows with country and id
in first do you don't have any data. Why?

better use

<select name="country" class="sidelinks">
<?php
while ($row_AllCountries = mysql_fetch_assoc($AllCountries));
{
print("<option value=\"".$row_AllCountries['country_ID']."\"");
if ($row_AllCountries['country_ID'] == $acountry)
{
echo "selected";
}
print(">".$row_AllCountries['countryname']."</option>");

}
?>
</select>

To see the print mistakes see the source in your browser. It may clear things up.
Also using function print can clear the code a little. Using <?PHP?> a lot can hide many things.

Best regards!
Michal Cibor