Jump to content

Formatting page array items - first, second, or third items


hollyvalero
 Share

Recommended Posts

Hi!

I'm working on formatting blog results. Currently I do a check for the first record and give it a bigger treatment, with a basic thumbnail/blurb/link for the rest...

 

$items = $pages->find("template=articleblog|post, limit=30, sort=-cbdate");
foreach ($items as $item) {
     if ($item == $items->first()) {
         [ this is where we do the larger photo, blurb, etc. since it's the newest]
   } else {
     [ standard thumb/blub/treatment]
  }

 

 

I'd like to be able to extend this so that the newest record got the big treatment, and then, say the next 2-3 got something bigger than a blurb, followed by the older treatment.

I've seen the counter for first/last

$i = 0;
$length = count($items);
foreach ($items as $item) {
if ($i == 0) {
	// first
} else if ($i == $length - 1) {
	// last
}
$i++;
}

But my attempts to figure out how to do this and increment the counter are resulting in server errors... 

 

$i = 0;
$length = count($items);
foreach ($items as $item) {
 if ($i == 0) {
    // first
 } else if ($i ==  1) {
    // second 
 } else if ($i ==  2) {
    // third 
 } else   {
    // all the rest
 }
$i++;
}

 

 

Looking for a good tutorial or idea for figuring out how to increment the counter and compare the number to choose the formatting for those array items that are not first or last.

thanks in advance!

 







 

Link to comment
Share on other sites

The code does not look wrong to me, what's the exact error you encounter? If you only see a 500 you may either get the exact cause from the error log (Setup>Logs>errors) or you may activate debug mode in config.php ($config->debug = true;). In any case you probably want to install the Tracy Debugger...

Instead of the if cascade you may consider using a switch statement.

Link to comment
Share on other sites

It seems strange you get a 500 error for something so trivial. Perhaps you have other code you've stripped from your post that causes an endless loop or redirect... You can try this:

$refs = $pages->find("template=project, title%=Zürich, include=all");
$count = count($refs);
$last = $count-1;
$c = 0;

foreach($refs as $r) {
    switch ($c) {
        case 0:
            echo "<hr><strong>FIRST: {$r->title}</strong><br>{$r->project_desc_short}";
        break;
        case 1:
            echo "<hr><strong>SECOND: {$r->title}</strong><br>{$r->project_desc_short}";
        break;
        case 2:
            echo "<hr><strong>THIRD: {$r->title}</strong><br>{$r->project_desc_short}";
        break;
        case $last:
            echo "<hr><strong>LAST: {$r->title}</strong><br>{$r->project_desc_short}";
        break;
        default:
            echo "<hr><strong>DEFAULT: {$r->title}</strong><br>{$r->project_desc_short}";
        break;
    }
    $c++;
}

 

  • Like 1
Link to comment
Share on other sites

You could use something like this:

$refs = $pages->find("template=project, title%=Zürich, include=all, limit=3"); // limit here just for testing small result-sets
$count = count($refs);
$last = $count-1;
$c = 0;
echo "<p>$count</p>";
foreach($refs as $r) {
    switch (true) {
        case ($c === 0):
            echo "<hr><strong>FIRST: {$r->title}</strong><br>{$r->project_desc_short}";
        break;
        case ($c === 1):
            echo "<hr><strong>SECOND: {$r->title}</strong><br>{$r->project_desc_short}";
        break;
        case ($c === 2 && $c === $last):
            echo "<hr><strong>LAST: {$r->title}</strong><br>{$r->project_desc_short}";
        break;
        case ($c === 2 && $c != $last):
            echo "<hr><strong>THIRD: {$r->title}</strong><br>{$r->project_desc_short}";
        break;
        default:
            echo "<hr><strong>DEFAULT: {$r->title}</strong><br>{$r->project_desc_short}";
        break;
    }
    $c++;
}
echo "<hr><br>" . $refs->count . " Referenzen gefunden";

see also https://www.phpknowhow.com/basics/if-else-and-switch-case/#highlighter_461554

Perhaps you could use this technique (PHP 7.2+), with goto: https://derickrethans.nl/php7.2-switch.html

  • Like 2
Link to comment
Share on other sites

I haven't used the switch statement yet, but I'm looking into it... this morning I re-uploaded the code and this time, it worked... 

and I'm getting that first, second-third, rest view I was hoping for!  Thank you so much for your input!!

 

Blog.jpg

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...