1

I have a table of items. But it's over 25 items. What is needed is that the first 25 items will be outputted in a page. then the remaining items will be outputted to another page. That would only be seen when you print the html file.

I'm using smarty template in this. I'm using

.page {
    width: 745px;
    page-break-after: always;
}

to determine that there will be another page if every there is. Then I'm using this:

assign the limit:

{assign var='itemLimit' value=25}

count number of items:

{assign var='itemCount' value=$RO_ITEMS|@count}

count number of page to be looped:

{assign var='pageCount' value=$itemCount/$itemLimit}

count items after the first 25, or excess items:

{assign var='extraItems' value=$itemCount%$itemLimit}

assign the start and end:

{assign var='start' value=0}
{assign var='end' value=$itemLimit}

then I use {section} to loop items:

{section name='invoicePage' loop=$pageCount}

This is the foreach code I use to loop the items:

{foreach from=$ITEMS item=EACHITEM  name=foo}
{if $smarty.foreach.foo.index >= $start and $smarty.foreach.foo.index < $end}
<tr>
  <td>{$EACHITEM.ITEM_SKU}</td>
  <td>{$EACHITEM.ITEM_TYPE}</td>
  <td>{$EACHITEM.ITEM_BRAND}</td>
  <td>{$EACHITEM.ITEM_COLOR}</td>
  <td>{$EACHITEM.ITEM_SIZE}</td>
  <td>{$EACHITEM.ITEM_QTY}</td>
</tr>
{/if}
{/foreach}

and at the end of everything needed to be looped, this is the code:

{$start = $start + $itemLimit}
{$end = $end + $itemLimit}

{/section}

Somehow, it's working that it's printing 25 items. But that is that.The other items after 25 are not outputted. Can anyone tell me what's wrong with this code? Thanks and regards.

nicole101
  • 187
  • 3
  • 20

1 Answers1

0

Sorry, but this is example what shouldn't be done in Smarty. Smarty is template engine to display data and not for making advanced calculation.

Moreover performance in your application might be very poor if there were many items. In your Smarty you iterating via foreach and displaying only selected data and you don't know if someone would print them.

Such calculations should be done only in PHP. In PHP you should select only those data that should be displayed and in Smarty you should simple display them.

If I were you, I would create one page with simple view and the other one with displaying all data where user would print simple print it. In first one when user tries to print it I would display message that's only simple view, and to see advanced go to url - it may be done using media print in css

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291