dongxixiu9134 2013-09-21 12:24
浏览 36
已采纳

关于使用Yii进行PHP分页的一些问题

I am creating a gallery in my web page. In my web I also have some views in where I use some widgets like CGridView or CListView. In these views, when there are many elements in the page, automatically appear a pagination bar. Of course I created theese views using Yii, and the problem is that I don't know how to create this kind of pagination in my gallery view. For this reason, I searched for a pagination code on internet and I found some things. Now I am using this code

<div class="pagination">
<ul>
<?php

echo "<a href=\"?pag=".'1'."\" onclick=\"Pagina('1')\">&lt;&lt; Primero </a> ";
if($pag_act>1) echo "<a href=\"?pag=".$pag_ant."\" onclick=\"Página('$pag_ant')\"> &lt; Anterior</a> ";

for ($l=$pag_act;$l<=$pag_ult;$l++){
echo "<a href=\"?pag=".$l."\" onclick=\"Página('$l')\"> $l</a> ";

}
  if($pag_act<$pag_ult) echo " <a href=\"?pag=".$pag_sig."\"    onclick=\"Pagina('$pag_sig')\">Siguiente &gt; </a> ";
echo "<a href=\"?pag=". $pag_ult."\" onclick=\"Pagina('$pag_ult')\">Último &gt;&gt;   </a>";

?>
 </ul>
 </div>

Of course, I edited the code because I wanted that this display a pagination bar similar than the created by Yii in another views as I told you.

In the end, my pagination bar is similar the created by Yii but they don't work in the same way. For example, my pagination bar show "myApp/site/gallery.html?pag=1" in the URL, when I click a button it doesn't change his color background and the most important, when I click on a button the page is refreshed and you display the begining of this, I mean, the tittle, not the pagination bar.

I know that to change the background color I have to edit the css, in my case, in navi.css I have:

/* ----------------------------------------------Pagination-------------------------------------*/

.pagination{display:block; width:100%; text-align:center; clear:both;}
.pagination ul{margin:0; padding:0; list-style:none;}
.pagination li{display:inline;}
.pagination .next{margin:0;}
.pagination a{display:inline-block; padding:2px 5px 3px 5px; margin:0 2px 0 0; border:1px solid #DFDFDF;}
.pagination .current, .pagination .splitter{padding:6px; margin:0 2px 0 0;}
.wrapper .pagination a:hover, .pagination .current, .pagination       .splitter{color:#9B9B9B!important; background-color:#F9F9F9;}
.pagination ul li.selected a{color:#666666!important; background-color: #F9F9F9;border:0;}

But I don't know that to do to change the background color of my pagination bar.

I would like that my pagination bar work as the created by Yii, or to know how to created another like the Yii pagination bar.

Please, someone could help me? ¡THANKS!

EDIT:

I use this code of gallery.php to display the images:

<div class="wrapper col2">
  <div id="container" class="clear">
    <!--     ####################################################################################################### -->
    <div id="tabcontainer">
      <ul id="tabnav">
        <li><a href="#tabs-1">Wedding Photos</a></li>
        <li><a href="#tabs-2">Artistic Photos</a></li>
        <li><a href="#tabs-3">Children's Photos</a></li>
        <li><a href="#tabs-4">Fashion Photos</a></li>
        <li><a href="#tabs-5">Action Photos</a></li>
      </ul>
      <!-- ########### -->
      <div id="tabs-1" class="gallery clear">
        <ul>

        <?php 

        while($imagen_a_empezar<$imagen_a_terminar){

                if($imagen_a_empezar>=$total_imagenes) break;

            if (in_array($imagen_a_empezar,$ultimos)){ 
            ?>

                <li class="last"><a href="<?php echo Yii::app()->theme->baseUrl;?>/images/portfolioslider/<?php echo     $archivos[$imagen_a_empezar]?>.jpg" rel="prettyPhoto[gallery1]" title=""><img src="<?php echo Yii::app()->theme->baseUrl; ?>/images/gallery/<?php echo $archivos[$imagen_a_empezar]?>.jpg" alt="" /></a></li>


            <?php }else{ ?> 

                <li><a href="<?php echo Yii::app()->theme->baseUrl;?>/images/portfolioslider/<?php echo archivos[$imagen_a_empezar]?>.jpg" rel="prettyPhoto[gallery1]" title=""><img src="<?php echo Yii::app()->theme->baseUrl; ?>/images/gallery/<?php echo $archivos[$imagen_a_empezar]?>.jpg" alt="" /></a></li>

            <?php };?>

          <?php  $imagen_a_empezar++;
        };?>

        </ul>
      </div>
    </div>
  </div>
</div>
  • 写回答

1条回答 默认 最新

  • douyue1481 2013-09-21 12:52
    关注

    I think it's better to let yii do pagination for you,just use a criteria for fetching your model and give it an instance of CPagination .

    you can also define a css class and assign it to your gridview or whatever using "htmlOptions".like:

    $this->widget('bootstrap.widgets.TbGridView', array(
            'id' => 'my-grid',
            'dataProvider' => $model->search(),
            'summaryText'=>'', // hide summary
            'htmlOptions' => array(
              'class' => 'someClass',
              'style' => 'width: 600px;height: 300px;',
             ),
        ));
    

    here is a good example.

    hope this helps.

    UPDATE :or you can :

    first in your controller,

    $criteria = new CDbCriteria;
    ...
    $pages=new CPagination($count);
    
        // results per page
        $pages->pageSize=10;
        $pages->applyLimit($criteria);
    
        $models = Books::model()->findAll($criteria);
    
        $this->render('index', array(
           'models' => $models,
           'pages' => $pages
        ));
    

    and then in your view,

    <?php foreach($models as $model): ?>
        $this->renderPartial('_gallery' , array('model' => $model));
    <?php endforeach; ?>
    
    // display pagination
    <?php $this->widget('CLinkPager', array(
        'pages' => $pages,
    

    )) ?>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog