At the moment, I'm pulling in data from an MySQL database using PDO. At one point in the page I have
<?php
$sitelinks=$pdo->query("SELECT * FROM sitelinks WHERE `show` = 'yes' ORDER BY `Order` ASC")
->fetchAll();
foreach ($sitelinks as $sitelink) {
echo "\n<li class=\"linkitem\"><a href=\"{$sitelink['URL']}\">{$links['Text']}</a></li>";
}
?>
and then later on in the page I'm using the $sitelink values again like so:
<?php
foreach ($sitelinks as $sitelink) {
echo '\n<li class=\"desktoplinkitem\">
<a href=\"{$sitelink['URL']}\">{$sitelink['Text']}</a>
</li>";
}
?>
In the first block I'm getting the $sitelink elements in the correct order (as they are being sorted in the MySQL query by the values in the Order column)
What I want to do is then sort the second block, also by this same Order column. From looking at the PHP manual I'm guessing an array_multisort would probably be best, but I'm a little confused as to the correct syntax. I tried the following:
<?php
foreach ($sitelinks as $sitelink) {
array_multisort($sitelink[`Order`], SORT_ASC);
echo "\n<li class=\"desktoplinkitem\">
<a href=\"{$sitelink['URL']}\">{$sitelink['Text']}</a></li>";
}
?>
EDIT: apologies, I've totally confused myself here - the order ISN'T changing, it's processing the correct way. My confusion came from the fact that I am then floating the li elements with float: right in the second block, which makes the links appear in reverse.
What I actually want to do is sort the second block descending by Order so that the second block reads 1, 2, 3, 4, 5 instead of 5, 4, 3, 2, 1 as it does now.
foreachloop