当有一些GET参数时,Zend_paginator不会改变perPage值

时间:2011-10-06 06:46:40

标签: zend-framework zend-paginator

它完美无缺,当我在结果的第一页时 - 我可以显示我需要的结果数量。 但是当我在另一页时,结果的数量无法改变。那是为什么?

当我在3页上时,我在每页显示12个,我尝试将其更改为60,在链接结束时我有:

perPage/12/submitPagin/Zastosuj/page/3?perPage=60&submitPagin=Zastosuj

任何想法,任何人? :)

public listAction(){        
(...)
    $params = $this->_parseSearchParams( $this->getRequest()->getParams() );
    $searchForm = new ListIndexForm( );
    $searchForm->setAction( '' );
    $searchForm->setDefaults( $params );

    $this->view->searchForm = $searchForm;
    $this->view->params = $params;

    $auth = Zend_Auth::getInstance();

    $sql = 'SELECT * FROM Images  order by created desc LIMIT 500';
    $result = $db->fetchAll($sql);
    $this->view->images = $result;

    $page=$this->_getParam('page',1);
    $paginator = Zend_Paginator::factory($result);

    $paginator->setCurrentPageNumber( $this->getRequest()->getParam( 'page', 1 ) );

    $paginator->setItemCountPerPage( $params[ 'perPage' ] );

    $this->view->paginator = $paginator;
}

我解析params的功能:

private function _parseSearchParams ( $params )
{
    if ( ! isset( $params[ 'perPage' ] ) ) {
        $params[ 'perPage' ] = 24;
    }

    foreach ( $params as $key => $value ) {
        if ( is_null( $value ) or $value == '' ) {
            unset( $params[ $key ] );
            continue;
        }


        switch ( $key ) {
            case 'tags':
            case 'im':
            case 'module':
            case 'submitupdate':
            case 'controller':
            case 'action':
            case 'submit':
                unset( $params[ $key ] );
                continue;
            break;

        }
    }

    return $params;
}

我的表单更改每页结果的数量:

class ListIndexForm extends Zend_Form {
public function __construct($options = null) {
    parent::__construct ( $options );
    $this->setMethod ( 'GET' );


    $perPageValues = array(
        6    => 6,
        12    => 12,
        18    => 18,
        24   => 24,
        30   => 30,
        60   => 60,
        900  => 90,
        150  => 150,
        300  => 300,
        600 => 600,
    );
    $this->addElement ( 'Select', 'perPage', 
       array (
          'filters'           => array(
            'Digits'
          ),           
          'label'             => 'Obrazków na stronę',
          'MultiOptions'      => $perPageValues,
          'value'             => 24,
       )
    );

    $this->addElement ( 'Submit', 'submitPagin',
    array (
          'label' => 'Zastosuj',
      ) 
    );

}

}

查看list.phtml:

(...)
<?=$this->searchForm;?>
<?foreach ($this->paginator as $row):?>
<?=$row['id']?>
<?enforeach;?>
<?= $this->paginationControl($this->paginator, 'Sliding', '../helpers/searchpagination.phtml', array( 'urlParams' => $this->params) ); ?>

searchpagination.phtml帮助:

    <?php
    $urlParams = isset($this->urlParams) ? $this->urlParams : array();
?>

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

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

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

1 个答案:

答案 0 :(得分:0)

您似乎是两次指定perPage参数。 (Zend Framework将两者视为GET参数)

<强> perPage / 12 / submitPagin / Zastosuj / /第3页的 perPage = 60 &安培; submitPagin = Zastosuj

如果将第一个更改为60会怎样?我意识到这可能会导致您的URL结构出现问题。