Link to home
Start Free TrialLog in
Avatar of robbie35
robbie35

asked on

selected items in a dropdown

I am trying to create a dropdown that will have a selected item at the top of the list, but there are a few quirks.  The items are coming from a MySQL db, since I can't nail an onChange js function to dynamically load a variable based on a selection from the dropdown, I have created a 2nd button on my form...when the button to obtain price is submitted, the item selected from the list when the page is reloaded should be the selected one, otherwise just the first one pulled from the db will be the selected item in the dropdown.

I tried something like this:
<?php
while($sp=mysql_fetch_row($sp_query))
{
 if (isset($_POST["size"]) && $_POST["size"]==$sp[0])
 {
  echo ("<option value=\"$sp[0]\" selected>$sp[0]</option>");
 }
 elseif($sp[0])
 {
  echo ("<option value=\"$sp[0]\" selected>$sp[0]</option>");
 }
 else
 {
  echo ("<option value=\"$sp[1]\">$sp[1]</option>");
 }                    
}
?>

but this obviously doesn't work, any help is appreciated.
Avatar of VGR
VGR

try the same but with this kind of writing :

echo ("<option value=\"".$sp[0]."\" selected>".$sp[0]."</option>");

array elements' references CAN'T be inside quotes, even double ones...
Avatar of robbie35

ASKER

i just get the same display, i also think that there is a logic problem with the if condition, it should be "if size is set, then have that the selected value", but i don't know how to compare the size value to those coming out of the db.  but for some reason the piece in the db that appears second is the one that is being displayed in the dropdown, but if you pulldown the box it is second, here is a link to make it easier www.germiphene.com/newproducts/nproducts2.html then click any of the 6 products on the menu in the main body, the page you go to is the page that has the dropdown, notice how the second item in the dropdown is the one being displayed at in the box when the page it loaded.  i am not sure what other info is needed, but let me know if there is anything else i can clarify.

thanks again for the repsonse..
I think Your logic bug is in the elsif($sp[0]) as long as $sp[0] isset this is going to evaluate to true.  What are you trying to do in the elsif.
I just went and looked at the html source for your page.
Both of the options have the selected tag.  

I definatly think your problem is with the elsif. The while loop is geting executed twice and both times it is printing out selected because if($sp[0]) is always true.



ALSO FYI there is some php source code showing up in your html just after the body tag.  

I just went and looked at the html source for your page.
Both of the options have the selected tag.  

I definatly think your problem is with the elsif. The while loop is geting executed twice and both times it is printing out selected because if($sp[0]) is always true.



ALSO FYI there is some php source code showing up in your html just after the body tag.  

ASKER CERTIFIED SOLUTION
Avatar of etrain01
etrain01

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for the FYI, those were pointless comments, but nonetheless...you never know.  the if/elseif/else statement basically can be rundown like this:
1.if size is set, have that the selected value
2.if nothing is set (first time to page) first value from db is selected
3.then display all the other records to the dropdown
trust me I know I am way off, it's been a LONG day, "no beer no tv make homer go something something!"

etrain01 I got yours up, but I am getting an error on the line that is the "else" above "echo ("<option value=\"$sp[1]\">$sp[1]</option>");", that second else is that part of the inner part of the elseif or it that the else to the if/elseif/else???

I sorted that else out, got the page to display, you can check it out at the same address as before, the one thing I don't have is when you "get price" the size relating to the price isn't the one that is selected...any ideas??

and I appreciate all the help...I am also getting hurried out of my work, so I may not respond for a bit, but I will return, any suggestions I will be grateful for.

thanks again all.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The problem now is that it is taking the last  selection and returning the first price.  Is this correct, or is it just taking the last selection and returning its price?

It might be helpful to see more of the code, I think the problem is further down in the code now.

etrain01
The problem now is that it is taking the last  selection and returning the first price.  Is this correct, or is it just taking the last selection and returning its price?

It might be helpful to see more of the code, I think the problem is further down in the code now.

etrain01
perhaps this helps you a little bit:


echo'<select name="new_genre">';
   $res = mysql_query("select * from video_genre order by Genre");
   $num = mysql_num_rows($res);
   for ($i=0;$i<$num;$i++)
     {
     $genre_nr = mysql_result($res,$i,"genre_nr");
     $genre = mysql_result($res,$i,"genre");
     if ($genre == $ge)$sel = 'selected'; else $sel = '';
echo'<option value="'.$genre_nr.'" '.$sel.'>'.$genre.'</option>';
     }
echo'</select>
for $ge you can use $new_genre so that after a reload of the page the last selected item in drop down is selected.
$ge can also be a variable coming from other pages etc. fit it to your needs ..

greets