-2

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.

10
  • Do you mean that your array is changes after first foreach loop...is it so? Commented Apr 15, 2016 at 10:56
  • 1
    Surely you want to sort it before your foreach loop Commented Apr 15, 2016 at 10:57
  • I think you can use php sort() function for the second block. Commented Apr 15, 2016 at 11:00
  • 3
    What I want to do is then sort the second block, also by this same Order column Does not make sense. If you want it sorted in the same way, you dont have to do anything to it. Its already sorted on order Unless you have done something to change the order between the first use and the second use! Commented Apr 15, 2016 at 11:03
  • 3
    Can you show more of your code? If you don't change the array, the array does not change its order by itself. Commented Apr 15, 2016 at 11:04

2 Answers 2

2

Look at using array_reverse

$reversed = array_reverse($sitelinks);

foreach ($reversed as $sitelink) {
    echo    "\n<li class=\"desktoplinkitem\"><a href=\"{$sitelink['URL']}\">{$sitelink['Text']}</a></li>";
}
Sign up to request clarification or add additional context in comments.

Comments

0

In your particular case, I suggest to use array_reverse

It will basically returns the same array but in inverse order. This way is much easier and clear that your using the same order criteria, just changing asc for desc.

<?php
$siteLinkDesc = array_reverse($siteLinkDesc);
?>

Hope this helps. In case you'd still' like to go for the multisort way. It's more complex, and I only recommend using if the order criteria is different than the original. This is what you should do (sort of): 1. Get an array of your sorting criteria: foreach ($sitelinks $key => $sitelink) { $orderSorting[$key] = $sitelink['Order']; }

  1. Apply multi-sorting to original array:

    array_multisort($orderSorting, SORT_DESC, $sitelinks);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.