Tuesday, July 27, 2010

Zend_Paginator分页效果实例

效果实现主要有三个文件
用于View的 ——
   index.phtml (最终用户看到的结果列表页面)
      pagelist.phtml(相当于是分页的一个模板)
   indexController.php (用于分页控制的文件)

Zend_Paginator分页效果实例


--------------------------------------------------

index.phtml




<?= $this->paginationControl($this->paginator, 'Sliding', 'pagelist.phtml'); ?>
<br />
<table>
    <tr>
        <th class="th1">Nom</th>
        <th class="th1">Immatriculation</th>
        <th class="th1">Longueur</th>
        <th class="th1">Patron</th>
        <th class="th1">Téléphone fixe </th>
        <th class="th1">Téléphone portable</th>
    </tr>
    <?php if (count($this->paginator)): ?>
    <?php foreach ($this->paginator as $navire): ?>

    <tr>
        <td class="td1"><?php echo $navire['nom']; ?></td>
        <td class="td1"><?php echo $navire['immatriculation']; ?></td>
        <td class="td1"><?php echo $navire['longueur']; ?></td>
        <td class="td1"><?php echo $navire['patron']; ?></td>
        <td class="td1"><?php echo $navire['num_tel_fixe']; ?></td>
        <td class="td1"><?php echo $navire['num_tel_portable']; ?></td>

    </tr>
    <?php endforeach; ?>
    <?php endif; ?>
</table>


-----------------------------------------------------------------

pagelist.phtml




<style type="text/css">
body{font-family:Tahoma;}
.page{padding:2px;font-weight:bolder;font-size:12px;}
.page a{border:1px solid #ccc;padding:0 5px 0 5px;margin:2px;text-decoration:none;color:#333;}
.page span{padding:0 5px 0 5px;margin:2px;background:#09f;color:#fff;border:1px solid #09c;}
</style>
<?php if ($this->pageCount): ?>
<div class="page">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->baseUrl ?>/gestboard/site/navire/index?page=1">&lt;&lt;</a>
<?php else: ?>
<span class="disabled">&lt;&lt;</span>
<?php endif; ?>

<?php if (isset($this->previous)): ?>
   <a href="<?php echo $this->baseUrl ?>/gestboard/site/navire/index?page=<?php echo $this->previous; ?>">&lt;</a>
<?php else: ?>
   <span class="disabled">&lt;</span>
<?php endif; ?>

<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
  <?php if ($page != $this->current): ?>
     <a href="<?php echo $this->baseUrl ?>/gestboard/site/navire/index?page=<?php echo $page; ?>"><?= $page; ?></a>
  <?php else: ?>
     <span class="disabled"><?= $page; ?></span>
  <?php endif; ?>
<?php endforeach; ?>

<!-- Next page link -->
<?php if (isset($this->next)): ?>
   <a href="<?php echo $this->baseUrl ?>/gestboard/site/navire/index?page=<?php echo $this->next; ?>">&gt;</a>
<?php else: ?>
   <span class="disabled">&gt;</span>
<?php endif; ?>
<?php if (isset($this->next)): ?>
<?php $last=count($this->pagesInRange);?>
<a href="<?php echo $this->baseUrl ?>/gestboard/site/navire/index?page=<?php echo $this->last; ?>">&gt;&gt;</a>
<?php else: ?>
<span class="disabled">&gt;&gt;</span>
<?php endif; ?>

<span><?php echo $this->current; ?>/<?php echo $this->last;;?></span>

</div>
<?php endif; ?>


------------------------------------------------------

indexControler.php




class NavireController extends Zend_Controller_Action {

    function init(){
        $this->initView();
        Zend_Loader::loadClass('bateau');

        $this->view->baseUrl = $this->_request->getBaseUrl();
    }
    function indexAction(){
        $this->view->headTitle("Navires");
      
        $bateau  = new bateau();
        $results = $bateau->fetchAll();

        $page = 1;
        $numPerPage = 30;
         if(isset($_GET['page']) && is_numeric($_GET['page'])){
            $page = $_GET['page'];
         }
        $offset = $numPerPage*$page;
        $count = count($results);
        $this->view->count = $count;

        $paginator = Zend_Paginator::factory($results);
        $paginator->setCurrentPageNumber($page)
             ->setItemCountPerPage($numPerPage);

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

        echo $this->view->render('navig.phtml');
        $this->render();
    }


}

或者:

function indexAction(){
        $this->view->headTitle("Navires");
      
        $bateau  = new bateau();
        $results = $bateau->fetchAll();

        $page = 1;
        $numPerPage = 30;
         if(isset($_GET['page']) && is_numeric($_GET['page'])){
            $page = $_GET['page'];
         }
        $offset = $numPerPage*$page;

//        $params = array (
//            'host'     => 'localhost',
//            'username' => 'root',
//            'password' => '',
//            'dbname'   => 'gestboard'
//        );
//        $db = Zend_Db::factory('PDO_MYSQL', $params);
//        $select = $db->select()
//             ->from('bateau' , '*')
//             ->limit($offset , $numPerPage)
//             ->order('id asc');
//        $stmt = $select->query();
//        $results = $stmt->fetchall();
     

        $count = count($results);
        $this->view->count = $count;
        $this->page($page,$numPerPage);

        echo $this->view->render('navig.phtml');
        $this->render();
    }

    public function page($page,$numPerPage){

        $params = array (
            'host'     => 'localhost',
            'username' => 'root',
            'password' => '',
            'dbname'   => 'gestboard'
        );

        $db = Zend_Db::factory('PDO_MYSQL', $params);

        $select = $db->select()
             ->from('bateau' , '*');
        $stmt = $select->query();
        $array = $stmt->fetchall();

        $paginator = Zend_Paginator::factory($array);
        $paginator->setCurrentPageNumber($page)
             ->setItemCountPerPage($numPerPage);

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







其实很简单,不明白折大家讨论

0 评论:

Post a Comment