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
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>";
?>

 

Link to comment
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>";
?>

 

 

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.