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">
<?php
				
				
$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>";
$i++;
}
				
?>
</select>

Recommended Answers

All 2 Replies

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

<?php
$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>";
}
?>

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.