Reputation: 43
Is there a way to make foreach display by newest content first (descending ID)? I have tried finding but I couldn't make any of it work.
<?php
include_once('includes/connection.php');
include_once('includes/deal.php');
$deal = new Deal;
$deals = $deal->fetch_all();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GameDeals</title>
<link rel="shortcut icon" type="image/x-icon" href="assets/favicon.png">
<link rel="stylesheet" href="assets/deals.css" />
</head>
<body>
<div class="container">
<!-- START GAMEDEALS -->
<?php foreach ($deals as $deal) { ?>
<div class="deal-info">
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="100">ID: <?php echo $deal['deal_id']; ?></td>
<td width="500"><?php echo $deal['deal_title']; ?></td>
<td width="200">TS: <?php echo $deal['deal_timestamp']; ?></td>
</tr>
</table>
</div>
<div class="deal-content">
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="100"><img src="<?php echo $deal['deal_imageurl']; ?>" width="100" height="100" /></td>
<td width="400"><?php echo $deal['deal_content']; ?></td>
<td>
<table width="300" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Regular Price: <?php echo $deal['deal_normalprice']; ?></td>
</tr>
<tr>
<td>Offer Price: <?php echo $deal['deal_offerprice']; ?></td>
</tr>
<tr>
<td>Off: <?php echo $deal['deal_percent']; ?></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<div class="deal-ref">
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="500">URL: <?php echo $deal['deal_url']; ?></td>
<td width="300">ENDS: <?php echo $deal['deal_expire']; ?></td>
</tr>
</table>
</div>
<?php } ?>
<!-- END GAMEDEALS -->
</div>
</body>
</html>
Thanks in advance!
more text.more text.more text.more text.more text.more text.more text.more text.more text.more text.more text.more text.more text.more text.more text.more text.more text.
Upvotes: 1
Views: 2722
Reputation: 24645
you could use array_reverse
for an arrays but for database sourced items you should follow one of the other answers. This would only work if the array came in sorted ascending
Upvotes: 1
Reputation: 2164
If you are not able to change the SQL query itself, then you need to sort the array before you perform the foreach. PHP's built-in usort function is perfect for this - it sorts an array in-place based on the value of the array and a user-defined comparison function:
function compareRows($a, $b)
{
if ($a['deal_id'] == $b['deal_id']) {
return 0;
}
return ($a['deal_id'] < $b['deal_id']) ? 1 : -1;
}
usort($deals, "compareRows");
$deals
will then be ordered by the 'deal_id' field descending. Keep in mind that this is the sort of operation that MySQL is optimized for, and doing this in PHP is much more inefficient.
Upvotes: 2
Reputation: 16495
You are naming $deal
as an object, and then assigning it to assign is to an array. IMO, you need to change $deal
to something other
Upvotes: 1
Reputation: 363
You can get the desired order i.e ASCENDING by putting the mysql column name after ORDER BY id DESC clause in your sql query.
Upvotes: 1
Reputation: 276
Foreach will not order your array, you will need to handle that in your query that fetches the data from the database, or in another method after you fetch the data, and before your foreach.
Upvotes: 1