Re: [PHP] Sorting a multidimensional array

2007-02-08 Thread Roman Neuhauser
# [EMAIL PROTECTED] / 2007-02-08 14:08:13 +:
 Hi all. I am building an online events directory and as part of the system
 users can search for events by date, category etc.
 
 The logic involved in finding events that match the user-entered dates works
 like so:
 
 1. we create a range of dates from the start and end dates specified by the
 user
 2. we then poll the database for any events that encompass or intersect this
 range (using the start_date and end_date fields from events table).
 3. we then find out the frequency of the event ie weekly, monthly and then
 map out its lifetime.
 4. we then go through those dates, and for any that match a date in the user
 range, the event on that date (ie all it details, name, venue, date, start
 time etc) is added to an array.

Order in the database. SELECT ... ORDER BY.

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Sorting a multidimensional array

2007-02-08 Thread Dave Goodchild

Thanks for that - can't do that as all I know in the database is the start
and end date for each event (so I don't have to create mapping tables and
perform massive joins), the rest is handle dynamically.

I think I can do it using usort, this seems to work, any comments?

function compare($x, $y) {

if (($x['date'] == $y['date'])  ($x['start_time'] ==
$y['start_time'])) {
   return 0;
   } else if (($x['date'] == $y['date'])  ($x['start_time'] 
$y['start_time'])) {
   return -1;
   } else if (($x['date'] == $y['date'])  ($x['start_time'] 
$y['start_time'])) {
   return 1;
   } else if ($x['date']  $y['date']) {
 return -1;
} else {
 return 1;
   }
   }


Re: [PHP] Sorting a multidimensional array

2007-02-08 Thread Németh Zoltán
array_multisort?

http://php.net/manual/en/function.array-multisort.php

hope that helps
Zoltán Németh

On cs, 2007-02-08 at 14:08 +, Dave Goodchild wrote:
 Hi all. I am building an online events directory and as part of the system
 users can search for events by date, category etc.
 
 The logic involved in finding events that match the user-entered dates works
 like so:
 
 1. we create a range of dates from the start and end dates specified by the
 user
 2. we then poll the database for any events that encompass or intersect this
 range (using the start_date and end_date fields from events table).
 3. we then find out the frequency of the event ie weekly, monthly and then
 map out its lifetime.
 4. we then go through those dates, and for any that match a date in the user
 range, the event on that date (ie all it details, name, venue, date, start
 time etc) is added to an array.
 
 ...etc etc
 
 So we eventually have a large multidimensional array whose elements contain
 arrays of events that fall on a specific date.
 
 I am using usort to ensure the events are sorted by date, but I also want to
 sort by time, for instance if there are five events on one day I want to
 sort them by start_time.
 
 Here's an example of how the event array looks:
 
 element [0] contains an array as follows ('date', 'name' venue',
 'start_time', 'category...');
 element [1] contains an array as follows ('date', 'name' venue',
 'start_time', 'category...');
 
 I tried using array_multisort but it didn't seem to do what I wanted. I want
 to sort by date, then start_time if the dates are equivalent.
 
 Any ideas?
 
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Sorting a multidimensional array

2007-02-08 Thread Edward Kay
How about this:
http://uk.php.net/manual/en/function.array-multisort.php#53779

Edward

 -Original Message-
 From: Dave Goodchild [mailto:[EMAIL PROTECTED]
 Sent: 08 February 2007 14:30
 To: Roman Neuhauser
 Cc: php-general@lists.php.net
 Subject: Re: [PHP] Sorting a multidimensional array


 Thanks for that - can't do that as all I know in the database is the start
 and end date for each event (so I don't have to create mapping tables and
 perform massive joins), the rest is handle dynamically.

 I think I can do it using usort, this seems to work, any comments?

 function compare($x, $y) {

  if (($x['date'] == $y['date'])  ($x['start_time'] ==
 $y['start_time'])) {
 return 0;
 } else if (($x['date'] == $y['date'])  ($x['start_time'] 
 $y['start_time'])) {
 return -1;
 } else if (($x['date'] == $y['date'])  ($x['start_time'] 
 $y['start_time'])) {
 return 1;
 } else if ($x['date']  $y['date']) {
   return -1;
  } else {
   return 1;
 }
 }



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php