Search code examples
phpcentoszend-paginator

Pagination in Zend not working


I have an array of items. Items are files paths. I have set up pagination but when I click on next it does not change. I want to display 1 pdf per page. Below is my code

class IndexController extends Zend_Controller_Action
{

    public function init(){}

    public function indexAction()
    {
        if($_SERVER['SERVER_NAME']=="portal-dev"){
            $location = "/data/scan/invoice";           
            $listOfFiles = $this->readFiles($location);
            $paginator = Zend_Paginator::factory($listOfFiles);
            $paginator->setItemCountPerPage(1);
            $this->view->paginator = $paginator;
        }
    }


    public function readFiles($location){
        $pattern="(\.pdf$)"; //valid image extensions
        $thelist = array();
        if ($handle = opendir($location)) {
            while (false !== ($file = readdir($handle)))
            {
                if (eregi($pattern, $file)) //if this file is a valid image
                {
                    $thelist[] = $file; //insert files into array
                    $max = count($thelist) - 1;
                    $max1 = count($thelist);
                }
            }
            closedir($handle);
        }
        return $thelist;
    }
}

index.phtml

<div id="main">
    <?php if(!$this->paginator){
        echo "No Files to process";
    }else{
    ?>
    <table>
        <tr>
            <td rowspan=2></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
        </tr>

    </table>
    <?php if (count($this->paginator)): ?>
    <ul>
        <?php foreach ($this->paginator as $item): ?>
        <li><?php echo $item; ?>
        </li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>

    <?php echo $this->paginationControl($this->paginator,'Sliding','partial/my_pagination_control.phtml'); ?>
    <?php }?>
</div>

pagination is found in directory partial

<?php if ($this->pageCount): ?>
<div
    class="paginationControl">
    <!-- Previous page link -->
    <?php if (isset($this->previous)): ?>
    <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
        Previous </a> <span class="bar"> | </span>
    <?php else: ?>
    <span class="disabled"> Previous</span> <span class="bar"> | </span>
    <?php endif; ?>

    <!-- Numbered page links -->
    <?php foreach ($this->pagesInRange as $page): ?>
    <?php if ($page != $this->current): ?>
    <a href="<?php echo $this->url(array('page' => $page)); ?>"> <?php echo $page; ?>
    </a> <span class="bar"> | </span>
    <?php else: ?>
    <?php echo $page; ?>
    <span class="bar"> | </span>
    <?php endif; ?>
    <?php endforeach; ?>

    <!-- Next page link -->
    <?php if (isset($this->next)): ?>
    <a href="<?php echo $this->url(array('page' => $this->next)); ?>"> Next
    </a>
    <?php else: ?>
    <span class="disabled">Next </span>
    <?php endif; ?>
</div>
<?php endif; ?>

Solution

  • Try after setting the current page number in indexAction

    $paginator->setCurrentPageNumber($this->_getParam('page'));
    

    Like this

    $paginator = Zend_Paginator::factory($listOfFiles);
    $paginator->setItemCountPerPage(1);
    $paginator->setCurrentPageNumber($this->_getParam('page'));
    $this->view->paginator = $paginator;