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>

Dani AI

Generated

A simple, robust approach is to build the dropdown from a single de-duplicated set and mark the highest when you render it. That avoids printing the top value twice (the cause in the original post) and keeps output predictable. Use a modern DB API (PDO or mysqli), treat the price column as numeric when ordering/comparing, and always escape values with htmlspecialchars before printing them.

Here is a compact PDO example that fetches distinct prices, computes the maximum in PHP, and prints each option with the correct selected attribute:

<?php
// $pdo = new PDO(...); // create PDO with ERRMODE_EXCEPTION
$stmt = $pdo->query("SELECT DISTINCT fld_priceto FROM tbl_range ORDER BY CAST(fld_priceto AS UNSIGNED) ASC");
$prices = $stmt->fetchAll(PDO::FETCH_COLUMN);
if ($prices) {
  $max = max(array_map('floatval', $prices));
  echo "<select name=\"price_to\" id=\"select2\">".PHP_EOL;
  foreach ($prices as $p) {
    $safe = htmlspecialchars($p, ENT_QUOTES, 'UTF-8');
    $sel = (floatval($p) == $max) ? ' selected' : '';
    echo "<option value=\"{$safe}\"{$sel}>{$safe}</option>".PHP_EOL;
  }
  echo "</select>".PHP_EOL;
}
?>

Practical tips: use DISTINCT to remove duplicate option values, or exclude the max in a second query with WHERE fld_priceto != (SELECT MAX(fld_priceto) FROM tbl_range) if you prefer two queries. Cast to numeric in SQL (or store prices in a numeric column) so ordering and comparisons behave correctly. Always escape output to avoid XSS. As noted, computing the max in PHP works fine; , the key fix is to stop pre-appending the top value and instead mark it as selected while printing the single option list.

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.