Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Return first x items in array, return items after x

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
benhamilton
Smarty n00b


Joined: 10 Apr 2019
Posts: 1

PostPosted: Wed Apr 10, 2019 6:58 am    Post subject: Return first x items in array, return items after x Reply with quote

Hi all,
I'm new to using Smarty, and I have a template where I'm wanting the first 25 items of the array returned on the first page, and then the remaining ones on the second page.

The code I have currently is below, but I'm getting a series of blank lines (being total array length less 25) after the first 25 array entries on the first page, and a series of blank lines (being 25) before the remaining entries on the second page. I'm sure there is a tidier way of doing this that produces just the 25 rows on the first page and remaining on the second page. How do I do that?

On Page One I have:

Code:

{foreach from=$crt_certificates_prd_products_1 item=IDENTIFIER}
  {if $IDENTIFIER@index lt 25}
    {$IDENTIFIER@index} - {$IDENTIFIER.name}
  {elseif $IDENTIFIER@index gte 25}
    {break}
  {/if}
{/foreach}


Then on Page Two I have:

Code:

{foreach from=$crt_certificates_prd_products_1 item=IDENTIFIER}
  {if $IDENTIFIER@index lt 25}
    {continue}
  {elseif $IDENTIFIER@index gte 25}{$IDENTIFIER@index} - {$IDENTIFIER.name}
  {/if}
{/foreach}


I've not found anything in the forum that has answered this, but will happily go read any posts you can point me to,
thanks in advance,
Ben.[/code]
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Apr 12, 2019 9:34 pm    Post subject: Reply with quote

Do pagination in your PHP code, not in Smarty code.
See also https://use-the-index-luke.com/no-offset
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sun Apr 21, 2019 5:54 pm    Post subject: Reply with quote

This effect could be caused by Smarty honoring line endings after the control structure tags. If so, you may need to run the tags together:
Code:
{foreach from=$crt_certificates_prd_products_1 item=IDENTIFIER}{if $IDENTIFIER@index lt 25}{$IDENTIFIER@index} - {$IDENTIFIER.name}
{* don't actually need this {elseif $IDENTIFIER@index gte 25}{break} *}{/if}{/foreach}


You might also try a simple {for} loop:
Code:
{for $i=0 to count($crt_certificates_prd_products_1) max=25}
    {$i} - {$crt_certificates_prd_products_1[$i].name}
{/for}

{for $i=25 to count($crt_certificates_prd_products_1)}
    {$i} - {$crt_certificates_prd_products_1[$i].name}
{/for}
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP