Forum Moderators: coopster

Message Too Old, No Replies

How can I solve my Problem?

I have a 1 table with 8 feilds in mysql...

         

Knowledge seeker

6:19 pm on Oct 16, 2009 (gmt 0)

10+ Year Member



I have a 1 table with 8 feilds in mysql, all of feidls values are echo in a form for taking information from the visitor using php 4+, which ever feilds i want to take from the this table is displaying properly, but their is one feild which is actually a drop down menu is not showing the value as I should retrieved it from the database, it display the Ist value of the feild, i.e I have country list
Afghnistan,Albania, Austria,......US,UK, and so on, if I want to display US, it will by automatically display the Afghanistan.
I am new to this forum if there is any difficulty to understand my question, please pardon me..
waiting for your sincer response.

skinsey

11:57 pm on Oct 16, 2009 (gmt 0)

10+ Year Member



<select name="country">
<?
echo "<option value = \"".$row[country]."\"";
echo "selected = \"selected\">".$row[country]."</option>";
?>
Then list out your others.

</select>

This will display the country twice. One selected and one not.

Knowledge seeker

8:41 am on Oct 17, 2009 (gmt 0)

10+ Year Member



Thank you very much sir, I have fully solved my problem using your coding, once again I am very thankful to you.

rocknbil

5:32 pm on Oct 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may want to use something that shows selected only if the user has selected something, either via a form or database value, like

$user_ctry = $_POST['country'];
(or loaded from a database, as in editing their info)


$select = "select c_code,c_fullname from $countries_table order by c_code asc";
// ASC is default anyway, but . . .
$select_list = '
<select name="country" id="country">
<option value="">Select</option>
';
// It is good practice, reliable at least, to have the top
// item blank in select lists, makes it easier to check input
$result=@mysql_query("$select");
while ($row=mysql_fetch_array($result)) {
$select_list .= '<option value="' . $row[0] . '"';
if ($row[0]==$user_ctry) { $select_list .= ' selected'; }
$select_list .= '>' . $row[1] . '</option>'; // use " if you want \n here
}
mysql_free_result($result);
$select_list .= '</select>';
echo $select_list;
// or return, if in a function, where it should be