douduan5073 2010-10-01 15:35
浏览 32
已采纳

使用Zend_Paginator而不选择适配器

I want to use Zend_Paginator but Zend_Paginator requires Zend_Db_Select as input parameter.

My SQL query is some how a little complicated making it so difficult to implement with Zend_Db_Select.

Can i use Zend_Paginator with plain SQL query?

  • 写回答

2条回答 默认 最新

  • doudou8893 2013-04-26 13:18
    关注

    I have had the same problem, my SQL queries are a little complicated and work with Zend_Db_Select it does very complicated, but I got one solution through implementing Zend_Paginator_Adapter_Interface, which is easy.

    //$data, has a $rowSet() and a counter variable with the result of ('SELECT FOUND_ROWS() as count')
    //rowSet come from  $rowSet = $this->_db->fetchAll($sql);//Default adapter
    
    $adapter   = new Fidelizacion_Model_AdapterVisitasClientes($data);
    $this->_clientePager =  new Zend_Paginator($adapter);
    
    
    //My own adapter
    
    /**
     * @author Jose
     */
    class Fidelizacion_Model_PaginatorVisitasClientes implements Zend_Paginator_Adapter_Interface
    {
        /**
         * Array
         *
         * @var array
         */
        protected $_array = null;
    
        /**
         * Item count
         *
         * @var integer
         */
        protected $_count = null;
    
        /**
         * Constructor.
         *
         * @param array $array Array to paginate
         */
        public function __construct($array)
        {
            $this->_array = $array['rowSet'];
            $this->_count = $array['count'];
        }
    
        public function getItems($offset, $itemCountPerPage)
        {
            $clientes = array();
                if($this->_array){
                    foreach ($this->_array as $row) { 
                        $clpo = new Clientes_Model_ClienteMin();
                        $clpo->setId($row['id']);
                        $clpo->setNombre($row['nombre']);
                        $clpo->setNumeroDocumento($row['numero_documento']);
                        $clpo->setTelefonoContacto($row['telefono_contacto']);
                        $clpo->setLocalidad($row['localidad']);
                        $clpo->setPersonaContacto($row['nombre_corto']);
                        $clientes[] = $clpo;
                        $clpo = null;
                    }
               }   
            return $clientes;
        }
        /**
         * Returns the total number of rows in the array.
         *
         * @return integer
         */
        public function count()
        {
            return $this->_count;
        }
    }
    
    //As you can see with that you can encapsulate in objects and set the conunt() method with the value of FOUND_ROWS()
    

    So, now you have to ensure that you catch the 'page' parameter and calculated the LIMIT for the current page to add in your SQL query, like:

    $sql .= ' LIMIT '.(((ITEMS_PER_PAGE * (int)$this->_getParam('page',1)) - ITEMS_PER_PAGE)).','.(ITEMS_PER_PAGE);
    $set = $this->_getClientesDao()->getClientesFiltroVisitas($sql);
    $paginator = $this->_getClientesPager($set);//returns the pager instantie with my own adapter
    $paginator->setCurrentPageNumber($this->_getParam('page'));    
    $paginator->setItemCountPerPage(ITEMS_PER_PAGE);
    $paginator->setPageRange(5);
    

    And as in my case, I've got about thirty variables to make the SQL. I pass these by GET, then my navigation controls looks like this:

    <?php if ($this->pageCount): ?>
        <div class="pagination_control">
            <a class="ends rounded" href="<?php echo $this->url() . '?' . $_SERVER['QUERY_STRING'] . '&page=' . $this->first ;?>">
               Primero
            </a>
            <?php if (isset($this->previous)): ?>
            <a class="movement rounded" href="<?php echo $this->url() . '?' . $_SERVER['QUERY_STRING'] . '&page=' . $this->previous; ?>">
                  &lt;Anterior
              </a> 
            <?php else: ?>
              <span class="disabled rounded">&lt; Previous</span>
            <?php endif; ?>
            <?php foreach ($this->pagesInRange as $page): ?>
                <?php if ($page != $this->current): ?>
                    <a class="page square rounded" href="<?php echo $this->url() . '?' . $_SERVER['QUERY_STRING'] . '&page=' .$page; ?>">
                          <?php echo $page; ?>
                    </a> 
                <?php else: ?>
                     <?php echo "<span class='current square rounded'>$page</span>" ?>
                <?php endif; ?>
            <?php endforeach; ?>
            <?php if (isset($this->next)): ?>
            <a class="movement rounded" href="<?php echo $this->url() . '?' . $_SERVER['QUERY_STRING'] . '&page=' . $this->next;?>">
                Siguiente&gt;
              </a>
            <?php else: ?>
              <span class="disabled rounded">Next &gt;</span>
            <?php endif; ?>
            <a class="ends rounded" href="<?php echo $this->url() . '?' . $_SERVER['QUERY_STRING'] . '&page=' .$this->last; ?>">
                Ùltimo
            </a>
            <span class="pagecount">
                Página <?php echo $this->current; ?> de <?php echo $this->pageCount; ?>
            </span>
        </div>
    <?php endif; ?>
    

    And that's all, it works fine!
    I hope this will be useful for someone
    Greetings.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解