Page 1 of 1

Alphabetical Order : (

Posted: Fri Jan 09, 2009 11:01 am
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.

Re: Alphabetical Order : (

Posted: Fri Jan 09, 2009 11:07 am
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.