Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorted Pull Down Menu

Status
Not open for further replies.

ardan

Technical User
Apr 22, 2003
27
US
Hi All,

Need Sorted Pull down menu.

I have created a pull down form.
I set up an ODBC connection to a access database and
populated it with...

///
while (odbc_fetch_row($rs))
{
$c1=odbc_result($rs,"company");
echo "<option value = 'test'>$c1</option>";
}
///

Put since the values are placed it one at a time they are
not sorted....well they are really backwards..

Newbie to PHP, I have been using ASP and wanted to learn PHP.

I have not been able to find any examples of the proper way to do this. An example of a better way to create this would be appreciated...

Thanks,

End of Line
 
Just sort your recordset when you query it from the database. Then the menu will appear in the order you intend.

This might be done using the following query...
Code:
SELECT * from mytable WHERE (show=1) [b]ORDER BY name ASC[/b]

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Thanks for the reply,

I sorry I left that out, I am sorting the recordset but the pulldown list is displayed descending instead on ascending... I could sort the recordset decending and it would work but I just didn't feel that is the correct solution...

seems to me that the command is inserting the new value on top of the old instead of building a list one after another.....

I appreciate any ideas..

End of Line
 
What you are doing is not wrong. You need to sort your recordset... and then iterate through it to get your data. If you don't sort it at all, then you will get the data back in the order it was entered into the database. Normally I provide a "sortorder" field and sort my results on this (allowing the user to then change the sortorder using a Content Management System).

You could always process the whole recordset into an array and then sort the array server-side... but that's just a waste of effort for what you require. Let the database do the hard work.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Thanks for the help, reading your post I said to myself I have done all that and then it dawned on me, I never used the 'selected' keyword in the <select> statement. So the control never returned to the top. Boy do I feel stupid...

Thanks Again, I wouldn't have found it without your help....

:)

End of Line
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top