Pages

Sunday, January 2, 2011

Zend Framework-Pagination

controller
-----------------------------------------

//For pagination with result
        $result= $oFrd->getfriendslist($user_id,$friend_key);
        $page=$this->_getParam('page',1);
       
        if($this->getRequest()->getParam('pageno'))
        $page=$this->getRequest()->getParam('pageno');
       
        $paginator = Zend_Paginator::factory($result);
        $paginator->setItemCountPerPage(15);
        $paginator->setCurrentPageNumber($page);
   
        $this->view->paginator=$paginator;
         $this->view->totCount=count($result);


Views
-----------------------------------------

<?= $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml',array('type'=>'myfriends')); ?>

<div class="mygrid_add_friend" style="height: 550px;clear: both;" id="mygrid_add_friend">
            <?php       
     if($this->totCount > 0)
        {
            foreach($this->paginator as $friends_values)
            {
                 if($friends_values['Friend_Request_Flag']){   
                ?>
                <div class="mygrid_add_friend_man" style="padding:3px">
                    <?php if($friends_values['user_image']!=""): ?>
                <span class="floatl"><img height="75px" src="<? echo $this->urls()->manMedia(); ?><?=$friends_values['user_image'];?>" /></span>
                  <?php else: ?>
                 <span ><img src="<? echo $this->urls()->images(); ?>man_blue_acc.gif" /></span>
                 <?php endif; ?>
                    <b  title="<?=$friends_values['user_fname'];?>"><?=$this->cutstring($friends_values['user_fname'],12,true);?>
                     </b>
                <b  title="<?=$friends_values['user_lname'];?>"><?=$this->cutstring($friends_values['user_lname'],12,true);?>
                     </b>   
               
                    <div style="height:50px" >                       
                        <em><a href="javascript:;" onclick="removefriend(<?=$this->user_id?>,<?=$friends_values['user_id']?>)"><img src="<? echo $this->urls()->images(); ?>btn_removeFriedns.gif" alt="" border="0"/></a></em>
                                    
                    </div>
            </div>
            <?php           
              }
            }
        }
        else
        {
          echo "<p align='center'>No Friends</p>";
        }
            ?>
           
        </div>


Application/scripts/pagination.phtml
--------------------------------------------
<?php
//pagination for my friends and search
 if($this->type=='myfriends')
 {
?>
<div class="pagination" style="width:100%">
    <div style="float:left;width:28%">
    </div>
    <div style="float:right;width:auto;">
        <!-- First page link -->
        <?php if (isset($this->previous)): ?>
              <a href="javascript:;" onclick="return moveNextPage('<?=$this->first;?>')">&lt;&lt; First</a> 
        <?php else: ?>
                <span class="disabled">&lt;&lt;First</span> 
        <?php endif; ?>
   
        <!-- Previous page link -->
   
        <?php if (isset($this->previous)): ?>
              <a href="javascript:;" onclick="return moveNextPage('<?=$this->previous;?>')">&lt;&lt; Previous</a> 
        <?php else: ?>
            <span class="disabled">&lt;&lt; Previous</span> 
        <?php endif; ?>
      
        <!-- Numbered page links -->
        <?php
        $totItems  = $this->totalItemCount;
        $curentPagelastItem  = $this->currentItemCount;       
       
        if($curentPagelastItem<$this->itemCountPerPage)
        {
           $totcount = $this->itemCountPerPage * ($this->current-1);       
           $endItem = $totcount + $curentPagelastItem;
           $startItem = $totcount+1;
        }
        else
        {
            $endItem   = $this->itemCountPerPage * $this->current;           
            $startItem = ($endItem+1)-$this->itemCountPerPage;
        }
           ?>
          <span class="disabled">&nbsp;<?php echo $startItem." - ".$endItem." of ".$totItems;?>&nbsp;</span>
        <!-- Next page link -->
        <?php if (isset($this->next)): ?>
               <a href="javascript:;" onclick="return moveNextPage('<?=$this->next;?>')">Next &gt;&gt;</a> 
        <?php else: ?>
             <span class="disabled">Next  &gt;&gt; </span> 
        <?php endif; ?>
        <!-- Last page link -->
        <?php if (isset($this->next)): ?>
              <a href="javascript:;" onclick="return moveNextPage('<?=$this->last;?>')">Last&gt;&gt;</a>  &nbsp;&nbsp;
        <?php else: ?>
            <span class="disabled">Last  &gt;&gt; &nbsp;&nbsp;</span>
        <?php endif; ?>
   
    </div>
 </div>
 <?php }else if($this->type=='allfriends'){ ?>

 <div class="floatr">
  <!-- Numbered page links -->
  <?php
        $totItems  = $this->totalItemCount;
        $curentPagelastItem  = $this->currentItemCount;       
       
        if($curentPagelastItem<$this->itemCountPerPage)
        {
           $totcount = $this->itemCountPerPage * ($this->current-1);       
           $endItem = $totcount + $curentPagelastItem;
           $startItem = $totcount+1;
        }
        else
        {
            $endItem   = $this->itemCountPerPage * $this->current;           
            $startItem = ($endItem+1)-$this->itemCountPerPage;
        }
    ?>
     <span>&nbsp;<?php echo $startItem." - ".$endItem." of ".$totItems;?>&nbsp;</span>
    <div class="add_friend_arrow_a">
   
       <!-- First page link -->
        <?php if (isset($this->previous)): ?>
        <a href="javascript:;" onclick="return moveNextPage('<?=$this->previous;?>')">
        <img border="0" class="fl" src="<? echo $this->urls()->images(); ?>left_play_icon.gif"></a>
        <?php else: ?>
        <a href="javascript:;"><img border="0" class="fl" src="<? echo $this->urls()->images(); ?>left_play_icon.gif"></a>
        <?php endif; ?>
       
        <!-- Next page link -->
         <?php if (isset($this->next)): ?>
        <a href="javascript:;" onclick="return moveNextPage('<?=$this->next;?>')">
        <img border="0" class="fr" src="<? echo $this->urls()->images(); ?>right_play_icon.gif"></a>
        <?php else: ?>
        <img border="0" class="fr" src="<? echo $this->urls()->images(); ?>right_play_icon.gif"></a>
        <?php endif; ?>
    </div>
 </div>
                                 

 <?php }else if($this->type=='friends_suggestion'){ ?>
 <div class="floatr">
    <div class="add_friend_arrow_a">
   
       <!-- First page link -->
        <?php if (isset($this->previous)): ?>
        <a href="javascript:;" onclick="return moveNextPage('<?=$this->previous;?>')">
        <img border="0" class="fl" src="<? echo $this->urls()->images(); ?>left_play_icon.gif"></a>
        <?php else: ?>
        <a href="javascript:;"><img border="0" class="fl" src="<? echo $this->urls()->images(); ?>left_play_icon.gif"></a>
        <?php endif; ?>
       
        <!-- Next page link -->
         <?php if (isset($this->next)): ?>
        <a href="javascript:;" onclick="return moveNextPage('<?=$this->next;?>')">
        <img border="0" class="fr" src="<? echo $this->urls()->images(); ?>right_play_icon.gif"></a>
        <?php else: ?>
        <img border="0" class="fr" src="<? echo $this->urls()->images(); ?>right_play_icon.gif"></a>
        <?php endif; ?>
    </div>
 </div>
 <?php } ?>



Scripts->js
----------------------------------------------------------
//js funcctions for pagination
function moveNextPage(pageNo)
{
    var page=$('#page').val()
    if(page=='friends')
    {       
        friends_pagination(pageNo)
    }
    else if(page=='allfriends')
    {       
        friends_search_pagination(pageNo)
    }
    else if(page=='friends_suggestion')
    {       
        friends_sugestion_pagination(pageNo)
    }
   
}

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

function friends_pagination(pageNo)
{   
    var friend=trim(document.getElementById('friends_test').value);
    if(friend=='')
     friend="''"   
   
    if(pageNo!='')
    {
        $.ajax({
                type : "POST",
                url: g_site_path+'/account/friendslist',
                data :'friend='+friend+'&pageno='+pageNo+'&rand='+Math.random(),
                dataType: "html",
                success: function(html) {
                    $('#friends').html(html)
                }
        });
    }
}


/ajax pagination/friendslist->controller
-----------------------------------------------
//For pagination with result
        $result= $oFrd->getfriendslist($user_id,$friend_key);
        $page=$this->_getParam('page',1);
       
        if($this->getRequest()->getParam('pageno'))
        $page=$this->getRequest()->getParam('pageno');
       
        $paginator = Zend_Paginator::factory($result);
        $paginator->setItemCountPerPage(15);
        $paginator->setCurrentPageNumber($page);
       
   
        $this->view->paginator=$paginator;
         $this->view->totCount=count($result);

No comments:

Post a Comment