Jump to content

[SOLVED] Array sorting problem


Grimloch

Recommended Posts

I am using CMS system, PHP-Fusion ver7.00.05, coding a user birthday script/mod and cannot seem to get an array sorted properly. User birthdays are stored in the db as Yr-Mo-Da ... i.e. 1965-08-22. All I need to work with is 08-22 (month and day) but the query 'ORDER BY' on the user birthdate column, automatically sorts the resulting dataset on the birth year. I don't think you can 'order by' on a portion of a column; at least I can't find it in any documentation. Here is the query w/where qualifier:

 

$thisdate = "____-".date("m")."-__";

$result = dbquery("SELECT user_id, user_name, user_birthdate FROM ".DB_USERS." WHERE user_birthdate like '$thisdate' ORDER BY user_birthdate ASC");

 

In the code below I am using the 'substr' function to extract only the month and day portion of the field.

 

while ($data = dbarray($result)) {

$my_date = substr($data['user_birthdate'], -5);

$newdate = explode ("-", $my_date);

$bdate = array($newdate[0], $newdate[1]);

asort($bdate, intval($bdate[1]));

echo "".$bdate[0]."/".$bdate[1]."".$locale['bp_017']."<a href='".BASEDIR."profile.php?lookup=".$data['user_id']."' title='".$data['user_name']."'>".$data['user_name']."</a><br />";

}

echo "<hr />";

 

I have tried numerous code snippets using 'foreach loops' and 'while loops' but to no avail. The resulting output *see attached image*, does not change from the original query format. What I am trying to do is format(sort) on the 'day' of the month. Please help me figure this out, bearing in mind that I am still learning the basic functions of php and mysql.

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/171210-solved-array-sorting-problem/
Share on other sites

Try this:

 

... ORDER BY MONTH(user_birthdate), DAY(user_birthdate);

 

OK; I feel really dumb. I was hoping that the answer was in the query. And now I know how to do it. Thanks Daniel0. That did the trick; exactly what I needed and I appreciate your quick response. I'll mark this as solved.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.