PHP Drop down ordering

Reply

Join Date: Feb 2008
Posts: 20
Reputation: myth3_16 is an unknown quantity at this point 
Solved Threads: 0
myth3_16 myth3_16 is offline Offline
Newbie Poster

PHP Drop down ordering

 
0
  #1
Feb 9th, 2008
<?
$username="*************";
$password="***************";
$database="****************";

mysql_connect("***************",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT product_name, url FROM product WHERE product_name like 'Day You Were%' group by product_name order by product_name";

echo "<form action=\"https://www.paypal.com/cgi-bin/webscr\" target=\"paypal\" name=\"mygallery\" method=\"post\">";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo "<font color=\"#FF0000\">1.</font> Select The Gift You Would Like:<br><select name=\"gift\" onChange=\"go()\">";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt

echo "<option label=\"$nt[product_name]\" value=\"$nt[url]\">$nt[product_name]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
In the drop down above I have the following options showing: Day You Were Born, Day You Were Married, Day You Were Christened. I want to select the correct option in the list according to the page the drop down appears on i.e I want the drop down to show 'Day You Were Marriedat the top of the list when the Day You Were Married page is displayed, Day You Were Born at the top of the list when the day you were born page is displayed and 'Day You Were Christened' at the top when the day you were christened page is displayed. So really what I am after is someone who can help with reordering the results according to the page it is on.

Can Anybody help????
Last edited by myth3_16; Feb 9th, 2008 at 3:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 100
Reputation: petr.pavel is an unknown quantity at this point 
Solved Threads: 14
petr.pavel's Avatar
petr.pavel petr.pavel is offline Offline
Junior Poster

Re: PHP Drop down ordering

 
0
  #2
Mar 9th, 2008
Hi there,
I assume you want to reorder the list just because you don't know of a better method of pre-selecting an option, right? If that's so I suggest using "selected" instead:

  1. $pageTitle = 'Day You Were Married';
  2. // this is for testing, I expect this variable to be filled somewhere else in your code
  3. $preSelect[$pageTitle] = ' selected';
  4. // $preSelect was an empty array, now it has just one item. I use this trick for pre-selecting options
  5.  
  6. ... while ...
  7. printf('<option label="%s" value="%s"%s>%s</option>',
  8. $nt['product_name'], $nt['url'], $preSelect[ $nt['product_name'] ], $nt['product_name']);

Does this solve your problem?
Petr 'PePa' Pavel

The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC