0

You guys have been patient with me and very helpful so far, so I'll try another one. I have looked around on here and tried many different options of select="selected" and I just can't get the code right or something, a simple fix I'm sure for you guys. What I'm trying to do is have a drop down list that is populated from a table of countries, default to the United States. Here is the code:

<select name="cnt" id="">
<option value="">Select</option>
<?php
$rescnt=mysql_query("select * from `country` ORDER by cnt_name ASC");
while($rowcnt=mysql_fetch_array($rescnt)) {
?>
<option value="<?php echo $rowcnt['cnt_id']; ?>"><?php echo $rowcnt['cnt_name']; ?></option>
<?php } ?>
</select>
  • you haven't got the selected attribute printed anywhere in the example above. – kalpaitch Mar 09 '13 at 15:07
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Mar 10 '13 at 21:46

1 Answers1

1

Assuming the cnt_id for the United States is 'US', the option line should be:

<option value='<?php echo $rowcnt['cnt_id']; ?>'<?php if($rowcnt['cnt_id']=='US') echo ' selected'; ?>><?php echo...
kainaw
  • 4,256
  • 1
  • 18
  • 38
  • I tried this but I get this error: Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in /home/content/44/10329144/html/admin/additem.php on line 258 – Tony Crouch Mar 10 '13 at 21:03
  • I added parenthesis around the if clause. I assume that is what caused the error. Without knowing what is on line 258, it is hard to tell. – kainaw Mar 10 '13 at 21:45