I have the following sql query in my Php code:
$sql = "SELECT id, date_day, date_month, date_year, title, content FROM $table_name ORDER BY date_year desc";
...
Although it lists in year order descending (in fact what I asked it to do) I would like to learn to sort the months and days into desc order properly too.
Can anyone suggest how I achieve this with php code.
I would do it in the SQL the same way as you have sorted on 'date_year' e.g.:
$sql = "SELECT id, date_day, date_month, date_year, title, content FROM $table_name ORDER BY date_year, date_month desc";
this assumes you have a numerical value stored for 'date_month', otherwise you will have the months sorted alphabetically which probably does not help. (my gut feeling is that storing the names of the month in the database is probably not the best way to go about it - have you looked at datetime/timestamp fields?)
BTW you can also mix DESC/ASC sorting (although this probably does not make sense to do when considering ordering by 'date' data) e.g.:
$sql = "SELECT id, date_day, date_month, date_year, title, content FROM $table_name ORDER BY date_year ASC, date_month desc";
the lowdown on sorting in MySQL can be found here: http://www.mysql.com/doc/en/Sorting_rows.html
more info on datetime fields in MySQL can be found here: http://www.mysql.com/doc/en/DATETIME.html and here: http://www.mysql.com/doc/en/Date_calculations.html
---
alernatively familiarize yourself with PHPs array sorting functions - e.g. array_multi_sort() (I may have spelt that function incorrectly!), but I think that it will be alot more hassle to set up an multilevel array with a second/third array for mapping the month names (assuming the 'date_month' field contains text) to numbers for sorting purposes than using the DB beforehand.
Thank you in advance
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php