2

I am trying to build a function that displays the reviews from an external json feed in a loop with php. Everything works fine, except the sorting.

I would like to sort the items based on the date.

<?php
$json_url = get_field('review_json_url', 'option'); // path to your JSON file
$data = file_get_contents($json_url); // put the contents of the file into a variable
$reviews = json_decode($data); // decode the JSON feed  
?>

<?php 
$count=0;               
foreach ($reviews->beoordelingen as $review) if ($count < 10) {
// vars
$publish_date = $review->date_created;
$name = $review->klant_naam;
$title = $review->title;
$content = $review->tekst;
$date = new DateTime($publish_date);
?>
<div class="review">
    <h3 class="name"><?php echo $name; ?></h3>
    <h5 class="location"><?php echo $date->format('d-m-Y'); ?></h5>
    <h4 class="subtitle">"<?php echo $title; ?>"</h4>
    <div class="content">
        <p><?php echo $content; ?></p>
    </div>
</div>
<?php $count++; } ?>
Justus
  • 123
  • 2
  • 10
  • 3
    Can you explain the exact problem with the given code? What is not working yet? What have you tried to achieve what you need? If you ask for help: what is the expected outcome of that code? – Nico Haase Mar 14 '19 at 18:40
  • Indeed, there doesn't seem to be any sorting facility coded in your snippet. You may need to sort the data before displaying it. – Félix Adriyel Gagnon-Grenier Mar 14 '19 at 19:38

3 Answers3

3

Typically in these situations I find it is best to organize the data prior to displaying. Below I have done this by setting up 1 foreach to handle the "logic" and 1 foreach to handle actually displaying the data.

From here it is as simple as adding a custom sort algorithm. (see PHP Sort Array By SubArray Value for a more in depth example)

<?php 

$sorted_reviews = array();
foreach ($reviews->beoordelingen as $review) {
    $date             = new DateTime($publish_date);
    $sorted_reviews[] = array(
        'publish_date' => $review->date_created,
        'name'         => $review->klant_naam,
        'title'        => $review->title,
        'content'      => $review->tekst,
        'date'         => $date,
        'sortable'     => $date->getTimestamp(),
    );
}

usort($sorted_reviews, 'sortByOption');
function sortByOption($a, $b) {
    return strcmp($a['sortable'], $b['sortable']);
}

$count=0;
foreach ($sorted_reviews as $review) if ($count < 10) {
?>
<div class="review">
    <h3 class="name"><?php echo $review['name']; ?></h3>
    <h5 class="location"><?php echo $review['date']->format('d-m-Y'); ?></h5>
    <h4 class="subtitle">"<?php echo $review['title']; ?>"</h4>
    <div class="content">
        <p><?php echo $review['content']; ?></p>
    </div>
</div>
<?php $count++; } ?>
Nicholas Summers
  • 4,444
  • 4
  • 19
  • 35
1

Maybe something like that:

$dates = [];
foreach ($reviews->beoordelingen as $i => $review)
    $dates[$i] = $review->date_created;
asort($dates); // this will sort array by values but keep keys attached to values
foreach (array_keys($dates) as $i)
{
    $review = $reviews->beoordelingen[$i];
    // rest of your code
}

edit: actually there is a better way to do this with usort()

function date_compare($a,$b)
{
    return strcmp($a->date_created,$b->date_created);
}
usort($reviews->beoordelingen, 'date_compare');
foreach ($reviews->beoordelingen as $review)
{
    // rest of your code
}
0

Thank you all for your explanation. I am still a rookie with php. I tried your code but this is still not sorting descending or ascending the reviews on date. For now i added "array_reverse" at the foreach section. And that did the trick for me.

<?php 

$sorted_reviews = array();
foreach ($reviews->beoordelingen as $review) {
    $sorted_reviews[] = array(
        'name'         => $review->klant_naam,
        'title'        => $review->title,
        'content'      => $review->tekst,
        'id'           => $review->CustorateID,
        'date'         => new DateTime($review->date_created),
    );
}

$count=0;
foreach (array_reverse($sorted_reviews) as $review) if ($count < 10) {
?>
<div class="review" id="<?php echo$review['id']; ?>">
    <h3 class="name"><?php echo $review['name']; ?></h3>
    <h5 class="location"><?php echo $review['date']->format('d-m-Y'); ?></h5>
    <h4 class="subtitle">"<?php echo $review['title']; ?>"</h4>
    <div class="content">
        <p><?php echo $review['content']; ?></p>
    </div>
</div>
<?php $count++; } ?>
Justus
  • 123
  • 2
  • 10