zend_paginatorコンポーネント(二)

8055 ワード

array
//controller/index
$array=array('テスト1','テスト2','テスト3','テスト4','テスト5','テスト6','テスト7','テスト8','テスト9','テスト10','テスト11','テスト12','テスト13','テスト14','テスト15','テスト16','テスト17','テスト18','テスト19','テスト20'); $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($array));  $paginator->setCurrentPageNumber($this->_getParam('page')); $this->view->paginator = $paginator; $this->render('index');
Zend_Db_Select
$db = Zend_Registry::get('db'); $select = $db->select(); $select->from('blog_article', '*'); $paginator = Zend_Paginator::factory($select); $paginator->setCurrentPageNumber($this->_getParam('page')); $paginator->setPageRange(5); $this->view->paginator = $paginator; $this->render('index');
viewビューはtplとphtmlの2種類に分かれています
tpl形式
//index.tpl
<!--   -->
{$this->doctype()}
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{$title}</title>

{$this->headLink()}
{$this->jQuery()}
{$this->headLink()}
</head>
<body>
<p align="center"><a href="{$baseUrl}/index/addui/"> </a></p>
<table width="500px" border="1"  cellspacing="0" cellpadding="0" align="center">
    <thead>
      <tr align="center">
        <th colspan="4" height="30" bgcolor="#cccccc"> </th>
      </tr>

    </thead>
    <tbody>
    <tr align="center">
        <th height="30"> </th>
        <th height="30"> </th>
        <th height="30"> </th>
        <th height="30"> </th>

    </tr>
   {if (count($this->paginator))}
   {foreach from=$paginator item=message}
   <tr align="center">
   	<td height="30">{$message.message_id}</td>
   	<td height="30">{$message.title}</td>
    <td height="30">{$message.content}</td>
    <td height="30"><a href="{$baseUrl}/index/editui/id/{$message.message_id}"> </a>/
       <a href="{$baseUrl}/index/del/id/{$message.message_id}"> </a>
    </td>
   </tr> 
    {/foreach}
    {/if}
      </tbody>

    </table>
 
{$this->paginationControl($this->paginator, 'Sliding', '/global/pagination_control.tpl')}


</body>

</html>

//pagination_control.tpl
{if ($this->pageCount)} 
   <center>
    <table width="600"><tbody><tr>
    <!--   -->
    <td><div id="prev"><a href=""><img src="/blog/images/bg1.jpg"></a></div></td>
    <!--   -->
    {if (isset($this->previous))} 
     <td> <a href="$this->previous"><img src="/blog/images/bg2.jpg"></a></td>
    {/if} 
    <!--   -->
    {foreach from=$this->pagesInRange  item=page}

       {if ($page != $this->current)}
        <td width="20" id="indexclick"><a href="{$smarty.const.baseUrl}/page/{$page}">{$page}</a></td> 
      {else}
        <td width="20" id="indexclick"><font style="color: red;">{$page}</font></td>
      {/if}
    {/foreach}
    <!--   -->
    {if (isset($this->next))} 
       <td><div id="prev"><a href=""><img src="/blog/images/bg3.jpg"></a></td> 
    {/if} 
    <!--   -->
    <td><a href=""><img src="/blog/images/bg4.jpg"></a></div></td>
    <td> {$this->pageCount}   {$this->totalItemCount}  <input type="text" size="4" name="pag"></td>
    <td><input type="hidden" value="" name="datee"><input type="hidden" value="" name="class"><input type="submit" value=" "></td>
    </tr></tbody></table></center>
{/if}

 
 
phtml形式の場合
//index.phtml
<html>
<body>
<h1>Example</h1>
<?php if (count($this->paginator)): ?>
<ul>
<?php foreach ($this->paginator as $item): ?>
   <li><?= $item['id']; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?= $this->paginationControl($this->paginator, 'Sliding', 'index/my_pagination_control.phtml'); ?>
</body>
</html>


 
 
//my_pagination_control.phtml
<?php if ($this->pageCount): ?> 
   <center>
    <table width="400"><tbody><tr>
    <!--   -->
    <td><div id="prev"><a href="<?= $this->url(array('page' => $this->first)); ?>"><img src="<?= PUBLIC_PATH?>/blog/images/bg1.jpg"></a></div></td>
    <!--   -->
    <?php if (isset($this->previous)): ?> 
     <td> <a href="<?= $this->url(array('page' => $this->previous)); ?>"><img src="<?= PUBLIC_PATH?>/blog/images/bg2.jpg"></a></td>
    <?php endif; ?> 
    <!--   -->
    <?php foreach ($this->pagesInRange as $page): ?>
       <?php if ($page != $this->current): ?>
        <td width="20" id="indexclick"><a href="<?= $this->url(array('page' => $page)); ?>"><?= $page; ?></a></td> 
      <?php else: ?>
        <td width="20" id="indexclick"><font style="color: red;"><?= $page; ?></font></td>
      <?php endif; ?>
    <?php endforeach; ?>
    <!--   -->
    <?php if (isset($this->next)): ?> 
       <td><div id="prev"><a href="<?= $this->url(array('page' => $this->next)); ?>"><img src="<?php echo PUBLIC_PATH?>/blog/images/bg3.jpg"></a></td> 
    <?php endif; ?> 
    <!--   -->
    <td><a href="<?= $this->url(array('page' => $this->last)); ?>"><img src="<?php echo PUBLIC_PATH?>/blog/images/bg4.jpg"></a></div></td>
    <td> <?= $this->pageCount?>   <input type="text" size="4" name="pag"></td>
    <td><input type="hidden" value="" name="datee"><input type="hidden" value="" name="class"><input type="submit" value=" "></td>
    </tr></tbody></table></center>
<?php endif; ?>