Jump to content

dropdown 'selected' not showing right


turpentyne

Recommended Posts

I must be missing something simple. I've got this little script that pulls rows from the database to populate a dropdown. If one of them matches a predetermined variable, then I want it to show selected. It's... almost working.

 

The dropdown prints, and shows the options. But the selected item shows separate, printed just below the dropdown?

 

Here's what I've got:

 

<?php
	$quer3=mysql_query("SELECT discount_id, discount_name,discount_amount FROM tbl_discount order by discount_amount");
	echo "    <select name='discount_id'><option value=''>Select</option>";
while($row = mysql_fetch_array($quer3)) { 
if($row[discount_id]==$discount_id){echo "<option selected value='".$row[discount_id]."'>".$row[discount_name]."  /   $".$row[discount_amount]."</option>";}
else{echo  "<option value='$row[discount_id]'>$row[discount_amount]</option>";}
echo "</select>";
} ?>

Link to comment
https://forums.phpfreaks.com/topic/257836-dropdown-selected-not-showing-right/
Share on other sites

As I see, the problem is the

echo "</select>";

inside the while loop.

Try to put it after the loop ending }:

<?php
	$quer3=mysql_query("SELECT discount_id, discount_name,discount_amount FROM tbl_discount order by discount_amount");
	echo "    <select name='discount_id'><option value=''>Select</option>";
while($row = mysql_fetch_array($quer3)) { 
if($row[discount_id]==$discount_id){echo "<option selected value='".$row[discount_id]."'>".$row[discount_name]."  /   $".$row[discount_amount]."</option>";}
else{echo  "<option value='$row[discount_id]'>$row[discount_amount]</option>";}
}
echo "</select>";
?>

 

As I see, the problem is the

echo "</select>";

inside the while loop.

Try to put it after the loop ending }:

<?php
      $quer3=mysql_query("SELECT discount_id, discount_name,discount_amount FROM tbl_discount order by discount_amount");
      echo "    <select name='discount_id'><option value=''>Select</option>";
while($row = mysql_fetch_array($quer3)) { 
if($row[discount_id]==$discount_id){echo "<option selected value='".$row[discount_id]."'>".$row[discount_name]."  /   $".$row[discount_amount]."</option>";}
else{echo  "<option value='$row[discount_id]'>$row[discount_amount]</option>";}
}
echo "</select>";
?>

 

 

Ahhh... missed that!

 

Turnpentine, I often find that laying out my code makes it 100000000000 times quicker to bug find later. Not doing so for what ever reason mean you will look over simple little mistake. For example your op:

<?php
    $quer3=mysql_query("SELECT discount_id, discount_name,discount_amount FROM tbl_discount order by discount_amount");
    
    echo "    <select name='discount_id'><option value=''>Select</option>";
    
    while($row = mysql_fetch_array($quer3))
        { 
            if($row[discount_id]==$discount_id)
                {
                    echo "<option selected value='".$row[discount_id]."'>".$row[discount_name]."  /   $".$row[discount_amount]."</option>";
                }
            else{
                    echo  "<option value='$row[discount_id]'>$row[discount_amount]</option>";
                }
            echo "</select>";
        } 
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.