Search code examples
phpsortingitemsprojects

Order items by last upload -> first show


I'm working on a website for a friend of mine. Someone made a CMS for me and placed the website on it.

Now i'm busy with finetuning. The thing is, the website launches projects. Only the last uploaded project end up as last on the website instead of up front.

So I wanted to show the recent uploaded project as first. And the first uploaded project as last. Is here a code for how to order this?

The website what this is about can be viewed on http://tinyurl.com/p36hz6u The order can bee seen at Portfolio -> Objects -> Now the first shown in list is Shine a Light on me. This one must be last and Roling on grils must be at first. Its now Z->A and it has to be A->Z.

This is the html / php

 <div class="portfoliomenu">
  <?php foreach ($portfolioCategories as $category) { ?>

    <a class="w-inline-block portfoliolink" href="#" onClick="swapCategory(<?=$category[ID]?>)"><img class="w-hidden-small w-hidden-tiny" data-lightbox="roadtrip" src="includes/uploads/<?=$category[IMAGE]?>" height="20"><img class="w-hidden-main w-hidden-medium" src="includes/uploads/<?=$category[IMAGE]?>" height="15">
    </a>
  <?php } ?>
  </div>

This is the PHP

              <div class="w-row portfoliorow">
      <div class="w-col w-col-2 portfolioarrowleft">
      <?php if ($page != 1 && $pages > 1) { ?>
      <a href="#" onClick="slidePortfolio(-1,<?=$objects[0][CATEGORY_ID]?>)"><img src="includes/images/left.png" width="30" alt="548818b8ba4bc2be7fd987c7_left.png"></a>
      <?php } ?>
      </div>
      <?php
      $i = 0;
       foreach ($objects as $object) { ?>
      <div class="w-col w-col-2">
        <a class="w-inline-block portfolioobjectlink" href="#" onClick="swapProduct(<?=$object[ID]?>)"><img src="includes/php/afbeelding.php?afbeelding=<?=$object[IMAGE1]?>&breedte=150&hoogte=150" alt="548828104511409e78b0c627_150x150.gif">
          <div><?= $object[TITLE]?></div>
        </a>
      </div>
      <?php $i++; } ?>
      <?php if ($i < 4) { 
        for($j = 0; $j < 4-$i; $j++)
        {
      ?>
      <div class="w-col w-col-2">
      </div>

      <?php }
      } ?>
      <div class="w-col w-col-2 portfolioarrowright">
      <?php if ($page < $pages) { ?>
      <a href="#" onclick="slidePortfolio(1,<?=$objects[0][CATEGORY_ID]?>)"><img src="includes/images/right.png" alt="5488189c4511409e78b0c518_right.png"></a>
      <?php } ?>
      </div>
    </div>

I hope you guys can help me out and understand me. (Sorry for the English) Thanks in advance!


Solution

  • Assuming items in $objects are inputted in project order (first to latest) use krsort on the array to reverse order by keys since items would retain key values as 0, 1, 2, ...

    $i = 0;
    krsort(objects);
    foreach ($objects as $object) { ?>
    ...
    

    Alternatively, you can use array_reverse which reverses the order of the elements in an array:

    $i = 0;
    foreach (array_reverse($objects) as $object) { ?>
    ...
    

    Finally, if there is a date element inside $objects array you can sort by that element using usort:

    function datesort($a, $b) {
       return $a['date'] - $b['date'];
    }
    
    usort($objects,'datesort');
    
    $i = 0;
    foreach ($objects as $object) { ?>