phpzend-frameworkpaginationzend-paginator

Zend Paginator: Show pagination links without loading(NON-DB) data


I am using a service which fetches results and total number of results present for the filters in one shot. Elaborating more, suppose there are total 100,000 records and I am showing 25 records on one page. I have an array having:

$myarr = array('totalCount' => 100000,
      'results'       => array(...data...));

This data is limited to 25 records in one go.

PROBLEM : When sending this data to zend_paginator it's showing only 1 page(anchor) with prev[disabled] & next[disabled]. According to code its working fine but how to show other paginating anchors[2,3,4,5,6,7,8,9] etc. as I know total counts of data.

I researched long for this but every where solutions are for DB queries and I don't get this result from DB, its from a service resulting in XML format which is converted to an array.

$paginator      = new Zend_Paginator(new Zend_Paginator_Adapter_Array($myarr ['results']));
    $paginator->setItemCountPerPage(25);
    $paginator->setPageRange(10);
    $paginator->setCurrentPageNumber(1);

How can I send total count so that it creates other paginating links without loading all data as I received a Memory Exhaust fatal error because of loading 100,000 records & it also looks unoptimized way of fetching all records every time for showing just 25 records when I have TOTAL COUNT in every service request.

pagination.phtml:

<div class="pagination" >
    <!-- Previous page link -->
    <?php if (isset($this->previous)): ?>
          <a href="javascript:void(0)" onclick='fetchPaginationData("<?php echo $this->divid;?>","<?php echo  $this->url(array('page' => $this->firstPageInRange -1)); ?>")'><span style="border-bottom-left-radius: 4px; border-left-width: 1px; border-top-left-radius: 4px;">Prev</span></a> 
    <?php else: ?>
        <span class="disabled" style="border-bottom-left-radius: 4px; border-left-width: 1px; border-top-left-radius: 4px;">Prev</span>
    <?php endif; ?>
    <!-- Numbered page links -->
    <?php foreach ($this->pagesInRange as $page): ?>
        <?php if ($page != $this->current): ?>
            <a href="javascript:void(0);" onclick='fetchPaginationData("<?php echo $this->divid;?>","<?php echo  $this->url(array('page' => $page)); ?>")' ><span><?php echo  $page; ?></span></a>
        <?php else: ?>
            <span class="page_current"><?php echo  $page; ?></span>
        <?php endif; ?>
    <?php endforeach; ?>
    <!-- Next page link -->
    <?php if (isset($this->next)): ?>
           <a href="javascript:void(0);" onclick='fetchPaginationData("<?php echo $this->divid;?>","<?php echo  $this->url(array('page' => $this->lastPageInRange +1)); ?>")'><span>Next &gt;</span></a> 
    <?php else: ?>
         <span class="disabled" style="border-bottom-right-radius: 4px; border-top-right-radius: 4px;">Next</span> 
    <?php endif; ?>


Solution

  • Alas, got the zendy solution :D

            $this->view->results = $myarr['results'];   // actual data used for generation of rows
            $this->view->totalCount = $myarr['totalCount'];   // needed to show count on view     
            $resultMY = new Zend_Paginator_Adapter_Null($myarr['totalCount']);
            $paginator = new Zend_Paginator($resultMY);
            $paginator->setItemCountPerPage(25);
            $paginator->setPageRange(10);
            $paginator->setCurrentPageNumber($page);  // from url
            $this->view->paginator = $paginator;      // passed this for generation of pagination