<div class="col-9"> <div class=""/>

php mysql pagination

Published
2022-07-21
浏览次数 :  198

//主要是利用mysql的LIMIT方法和php链接传参?page方法进行实现。
<div class="row">
        <div class="col-9">
          <div class="row">
          <?php 
          $limit = 6;
          $page = isset($_GET['page']) ? $_GET['page'] : 1;
          $start = ($page - 1) * $limit;

          $sql = "SELECT * FROM books LIMIT $start,$limit";
          $stmt = $conn->prepare($sql);
          $stmt->execute();

          if ($stmt->rowCount() > 0) {
            $books = $stmt->fetchAll();

            $total_sql = $conn->query("SELECT count(id) as id FROM books");
            $total = $total_sql->fetchAll();
            $count = $total[0]['id'];

            //ceil 返回函数向上舍入最接近整数
            $total_pages = ceil($count / $limit);
            $previous = $page - 1;
            $next = $page + 1;
            $init = 0;

          } else {
            $books == 0;
          }

          if ($books == 0) {
          echo strtoupper("no result");
        } else {
          foreach ($books as $book) { $init++; ?>
            
              <div class="col-sm-4">
              <div class="card shadow-sm">
               <img src="uploads/cover/<?=$book['cover']?>" class="bd-placeholder-img card-img-top" width="auto" height="250">
                <div class="card-body">
                  <h4 class="mb-2"><?=$book['title']?></h4>
                  <i class="mb-2 d-block"><b>By:
                    <?php foreach ($authors as $author) {
                      if ($author['id'] == $book['author_id']) {
                        echo $author['name'];
                        break;
                      }
                    } ?>
                  </b></i>
                  <p class="card-text">
                    <?=$book['description']?></p>
                  <div class="d-flex justify-content-between align-items-center">
                    <div class="btn-group">
                      <a href="uploads/files/<?=$book['file']?>" type="button" class="btn btn-sm btn-outline-secondary btn btn-success text-white">View</a>
                      <a href="uploads/files/<?=$book['file']?>" download="<?=$book['title']?>" type="button" class="btn btn-sm btn-outline-secondary">Download</a>
                    </div>
                    <small class="text-muted">分类:<?php foreach ($category as $cat) {
                      if ($cat['id'] == $book['category_id']) {
                        echo $cat['name'];
                        break;
                      }
                      // code...
                    } ?></small>
                  </div>
                </div>
              </div>
              </div>
            
            <?php 
          }
        } ?>
        </div>

        <nav aria-label="Page navigation example">
          <ul class="pagination">
            <li class="page-item <?php if($page == 1) echo 'disabled'; ?>"><a class="page-link" href="index.php?page=<?=$previous?>">Previous</a></li>
            <?php for ($i=1; $i <= $total_pages; $i++) { ?>
              <li class="page-item <?php if($i == $page) echo 'active'; ?>"><a class="page-link" href="index.php?page=<?=$i?>"><?=$i?></a></li>
              <?php 
              // code...
            } ?>
            <li class="page-item <?php if($page == $total_pages) echo 'disabled'; ?>"><a class="page-link" href="index.php?page=<?=$next?>">Next</a></li>
          </ul>
        </nav>
        </div>
        
        <div class="sidebar col-3">
          <div class="list-group category mb-3">
            <a href="" class="list-group-item list-group-item-action active">Category</a>
            <?php 
            if ($category) {
              foreach($category as $cat) { ?>
                <a href="category.php?catId=<?=$cat['id']?>" class="list-group-item list-group-item-action"><?=$cat['name']?></a>
                <?php 
              }
            }

             ?>
          </div>
          <div class="list-group author">
            <a href="" class="list-group-item list-group-item-action active">Authors</a>
            <?php 
            if ($authors) {
              foreach($authors as $author) { ?>
                <a href="author.php?authorId=<?=$author['id']?>" class="list-group-item list-group-item-action"><?=$author['name']?></a>
                <?php 
              }
            }

             ?>
          </div>
        </div>
      </div> 

  • 标签1
  • 标签1
  • 标签1
Top