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>

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.