I have table in mysql, have values in row i want when page loads the highest value is alwyas the selected one.

Im using two queries first one pulls the top value and adds it to box then it populates the rest, so it shows this top value twice, is there another way of addthem and if its the last or highest that one needs to be the selected one

<select name="price_to" id="select2">
$sqls = "SELECT fld_priceto FROM tbl_range ORDER BY fld_priceto DESC LIMIT 1";
$results = mysql_query($sqls);		
while($rows = mysql_fetch_assoc($results)){

echo "<option selected='".$rows['fld_priceto']."'>".$rows['fld_priceto']."</option>";

$sql = "SELECT * FROM tbl_range";
$result = mysql_query($sql);		
while($row = mysql_fetch_assoc($result)){
echo "<option value='".$row['fld_priceto']."'>".$row['fld_priceto']."</option>";
6 Years
Discussion Span
Last Post by branding4you

I would do something like this:
- Get all rows
- Determine the highest
- Print all options

$sql = "SELECT * FROM `tbl_range`";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) $rows[] = $row;
if($rows) {
  foreach($rows as $row) if($row['fld_priceto'] > $max) $max = $row['fld_priceto'];
  foreach($rows as $row) echo "<option value='".$row['fld_priceto']."'".($row['fld_priceto'] == $max ? ' selected' : '').">".$row['fld_priceto']."</option>";

Edited by twiss: n/a


I dint know it could be done that way, lol I was looking at other methods like search through the listbox/dropdown for the last value.
I never worked with the MAX before

Thanks Twiss you are a whiz

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.