?
Solved

selected items in a dropdown

Posted on 2003-02-25
15
Medium Priority
?
349 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:robbie35
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
15 Comments
 
LVL 15

Expert Comment

by:VGR
ID: 8019186
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...
0
 

Author Comment

by:robbie35
ID: 8019684
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..
0
 

Expert Comment

by:kganger
ID: 8019823
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:kganger
ID: 8019868
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.  

0
 

Expert Comment

by:kganger
ID: 8019910
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.  

0
 
LVL 5

Accepted Solution

by:
etrain01 earned 100 total points
ID: 8020000
Try something like this:
<?php
$selected=0;
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])
 {
 if($selected == 0)
 {
   echo ("<option value=\"$sp[0]\" selected>$sp[0]</option>");
   $selected = 1;
 }
 else
 {
   echo ("<option value=\"$sp[0]\" >$sp[0]</option>");
 }
 else
 {
  echo ("<option value=\"$sp[1]\">$sp[1]</option>");
 }                    
}
?>

This way it will only select the first one and not put the selected on all the others, this is the reason it choses the last selection.

hope this helps...

also don't forget to close your <option>'s for the flavour...

etrain01
0
 

Author Comment

by:robbie35
ID: 8020180
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???

0
 

Author Comment

by:robbie35
ID: 8020219
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.
0
 

Assisted Solution

by:kganger
kganger earned 100 total points
ID: 8020264
Try something like this

<?php
$selected=0;
while($sp=mysql_fetch_row($sp_query))
{
if (isset($_POST["size"]) && $_POST["size"]==$sp[0] && $selected==0)
{
 echo ("<option value=\"$sp[0]\" selected>$sp[0]</option>");
$selected++;
}
elseif(!isset($_POST["size"])&& $selected==0)
{
 echo ("<option value=\"$sp[0]\" selected>$sp[0]</option>");
$selected++;
}
else
{
 echo ("<option value=\"$sp[1]\">$sp[1]</option>");
}                    
}
?>
0
 
LVL 5

Expert Comment

by:etrain01
ID: 8020309
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
0
 
LVL 5

Expert Comment

by:etrain01
ID: 8020314
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
0
 
LVL 1

Expert Comment

by:KC_Speedball
ID: 8020547
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>
0
 
LVL 1

Expert Comment

by:KC_Speedball
ID: 8020565
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question