Reputation: 522
I am creating a page to list select columns from each row in a table. I have the basics of that down by using a foreach loop as below and it works fine. The problem I am up against now is that I need to order the results by date (one of the columns) with the newest record being at the top.
This is what I have so far (that works but without sorting)
foreach ($visitors as $visitor) {
$id = htmlentities($visitor['family_id']);
$first_name = htmlentities($visitor['first_name']);
$last_name = htmlentities($visitor['last_name']);
$visit_date = htmlentities($visitor['visit_date']);
$phone = htmlentities($visitor['phone']);
$email = htmlentities($visitor['email']);
?>
<p><?php echo $visit_date; ?><a href="visitor-view.php?id=<?php echo $id; ?>"> <?php echo $first_name . ' ' . $last_name; ?></a> <?php echo $phone; echo $email; ?></p>
<?php
}
Hoping someone has a bright idea as to how to get it to sort.
Upvotes: 0
Views: 2681
Reputation: 522
Stupid oversight from myself, trying to make it more complicated that it should have been.
Using suggestion from Dragon Warrior was by far the simplest way to do it.
Upvotes: 0
Reputation: 19731
Use USORT
( Got it from PHP's official website - http://php.net/manual/en/function.usort.php )
function date_compare($a, $b)
{
$t1 = strtotime($a['visit_date']);
$t2 = strtotime($b['visit_date']);
return $t1 - $t2;
}
usort($array, 'date_compare');
Upvotes: 1