Login +

How To ? / Paginate

Imagine you want to paginate a classic recordsets of news.

In the Controller layer:

// Page numbering        
$start = 0;
$length = 10;
$page = ($this->_http->getParam("page") > 1) ? $this->_http->getParam("page") : 1;
$xml->startTag("page");
$xml->addFullTag("start", $start = ($page < 2) ? 0 : (($page-1) * $length), true);
$xml->addFullTag("length", $length, true);
$xml->addFullTag("total", $news->countModels("news"), true);
$xml->endTag("page");
// /Page numbering

// Get bounded news
$newss = $news->searchModels("news",
                             array(),
                             array(),
                             array(),
                             array(array("column" => "news_date",
                                         "order" => "desc")),
                             array("start" => $start,
                                   "length" => $length));
$xml = $news->pdoToXML($xml, $newss);
// /Get bounded news

In the View layer:

<!-- Display all news -->
<div class="newss">    
    <xsl:for-each select="//View/newss/news">
        <div class="news">
            <h2><xsl:value-of select="news_title" /></h2>
            <div class="news_content">
                <xsl:value-of select="news_content" disable-output-escaping="yes" />
            </div>
        </div>
    </xsl:for-each>    
</div>
<!-- /Display all news -->

<!-- Paginate -->
<div class="paginate">
    <xsl:value-of select="php:functionString('SLS_String::paginate', //View/page/start, //View/page/length, //View/page/total)" disable-output-escaping="yes" />
</div>
<!-- /Paginate -->

It's will produce the following xHTML DOM:

<div class="paginate">
    <a class="pager_link" href="http://vhost.domain.tld/Controller/Action/page/1.sls" >
        <
    </a>
    <a class="pager_link" href="http://vhost.domain.tld/Controller/Action/page/1.sls">
        1
    </a>
    <span class="pager_selected">
        2
    </span>
    <a class="pager_link" href="http://vhost.domain.tld/Controller/Action/page/3.sls" >
        3
    </a>
    <a class="pager_link" href="http://vhost.domain.tld/Controller/Action/page/3.sls" >
        >
    </a>
</div>

Note that if you have parameters in POST in your current page, they will be preserved in GET.

If you want to adjust other settings, check the paginate() reference guide of SLS_String1.

References

  1. SLS_String - Tool SLS_String - String Treatment