Alphabetical Order : (

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Alphabetical Order : (

Post by SheDesigns »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I need to make a dropdown menu that includes options from the database, and pre-existing options. The options need to all be in alphabetical order. However current records in the database don't have these options yet, so I can't just pull them all from the database.

Code: Select all

echo "<select name=\"vendortype\">
    <option value=\"$vendortype\">$vendortype</option>
    <option value=\"$vendortype\">--------------</option>
    <option value=\"Hauler\">Hauler</option>
    <option value=\"Single Source Recycle\">Single Source Recycle</option>
    <option value=\"Multi Source Recycle\">Multi Source Recycle</option>
    <option value=\"Broker\">Broker</option>
    <option value=\"Management Co\">Management Co</option>";
    // Look for options that were added later
$query_0 = "select distinct vendortype as something from Vendors where vendortype != 'Hauler' and vendortype != 'Single Source Recycle' and vendortype != 'Multi Source Recycle' and vendortype != 'Broker' and vendortype != 'Management Co' and vendortype != '' order by vendortype asc;";   
$result_0 = mysql_query($query_0);
while($fetch_0 = mysql_fetch_array($result_0))
{
extract($fetch_0);
echo "<option value=\"$something\">$something</option>";
}
echo "</select>";
In case you can't read my Spaghetti code, I wrote the HTML for the pre-existing options. Then, I pulled any types that weren't pre-existing from the database.

Point is, I need both pre-existing and database created options to display in alphabetical order together.


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Alphabetical Order : (

Post by John Cartwright »

Add them all your existing options to an array, add the options from the database, and apply a ksort, or sort() function (depending if you want to sort by key or value). Depending on how you store the values in your array, which is usually

array(
key => value
)

then you will have problems if you have multiple columns having the same value (because they will be overwritten). You should never have two options with the same value anyway though.

At that point, it's a simple matter of looping normally and building your <select> and <option> tags.
Post Reply