Hoping someone can help me with a MySQL/PHP sort order issue.
A brief outline of what I am doing is as follows:
I have three (chained) selection boxes. The first box's output is "fixed" (it contains all the top-level categories in my store). The two subsequent boxes are populated depending on the choice made in the previous box... standard, straightforward stuff, I hear you say!
It all works great, and the selection (string) from the last box is appended to a "search results" page -- exactly what I need. My problem comes when trying to organise the sort order of the second and third boxes though.
Here is a sample of my code:
public function ShowType()
{
$sql = "SELECT categories.*, categories_description.categories_name FROM categories, categories_description WHERE categories.categories_id = categories_description.categories_id AND categories.parent_id = $_POST[category] ORDER BY categories_name";
$res = mysql_query($sql,$this->conn);
$type = '<option value="0">Select...</option>';
while($row = mysql_fetch_array($res))
{
$type .= '<option value="' . $row['categories_name'] . '">' . $row['categories_id'] . '</option>';
}
return $type;
}
The Ajax replaces the (greyed out, non-clickable)
<option value="0">Select...</option>
part to
<option value="' . $row['categories_name'] . '">' . $row['categories_id'] . '</option>
once a selection (in the previous box) has been made.
My problem is the array displayed once the box is activated is not in alphabetical order. I want to sort the output in the selection box by categories_name
but no matter what I try in the MySQL query, it won't have it!
Is the output to my "dynamic" tag unsorted because of the way it's updated by Ajax? Do I need to re-sort the array with PHP, and if so, can someone point me to a beginner's level (I'd hate to call myself intermediate!) tutorial on how to go about it please?
I hope I don't spark off any more arguments like I did last time! :-)
Cheers,
Andy.