donglian7879 2017-04-20 14:41
浏览 50
已采纳

CakePHP自定义分页

I made a paginator but the problem is that it shows page number on bottom when there is only one page. So I tried to add if~else but I can't get it to work. "Error: Maximum execution time of 120 seconds exceeded "

before

<div class="pagination">
    <?php for($x = 1; $x <= $pages; $x++): ?>
        <a href="?page=<?php echo $x; ?>"<?php if($page === $x) { echo ' class="selected" '; } ?>><?php echo $x ?></a>
    <?php endfor; ?>
</div>

after

<div class="pagination">
    <?php 
       if($pages > 1){
           for($x = 1; $x <= $pages; $x++): 
              if($x = 1)
              { 
              ?>
               <a href="?page=<?php echo $x; ?>"<?php if($page === $x) { echo ' class="selected" '; } ?>><?php echo $x ?></a>
              <?php
              } else {
                echo '';
              }
           endfor;
        }
    ?>
</div>
  • 写回答

1条回答 默认 最新

  • dongyi1939 2017-04-20 16:04
    关注

    Assuming $pages is the total count of pages you have, you need to wrap your custom pagination in an if statement to check you have more than 1 page before displaying your pagination.

    <?php if ($pages > 1): ?>
        <div class="pagination">
            <?php for ($x = 1; $x <= $pages; $x++): ?>
                <a href="?page=<?php echo $x; ?>"<?php if($page === $x) { echo ' class="selected" '; } ?>><?php echo $x ?></a>
            <?php endfor; ?>
        </div>
    <?php endif; ?>
    

    Your code is breaking because you are doing the if statement inside your for loop and setting the value of $x to 1 every time (you're using = instead of ==). This will get you stuck in an infinite loop.

    If you're using CakePHP is there a reason why you are not using it's inbuilt Pagination? The PaginatorHelper should provide you all the methods you need to display pagination if you are.

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能