douqinlu4217 2018-07-24 14:31
浏览 14

分页MVC PHP

I would like to make a pagination. Right now my model looks like that :

class Movies extends Connection{

public function getMovies($offset = null) {
$sql = 'SELECT * FROM `movies` ORDER BY `id` DESC';
if ($offset) $sql .= ' LIMIT '.$offset;
return $this->query($sql, null, 'all');
}

Here is my controller

class Controller {

public $movie;
$this->movie = new Movies();
  public function list(){
$movie = $this->movie;
$view = require 'Views/list.php';
}
}

Here is my view :

<div class="single mb-5 mt-5">
<div class="container">
<div class="scroll">
<table id="movie_list">
<thead>
<tr>
  <th data-sort="string">Titre <i class="fa fa-sort"></i></th>
  <th data-sort="string">Genre <i class="fa fa-sort"></i></th>
  <th data-sort="string">Date de sortie <i class="fa fa-sort"></i></th>
  <th data-sort="string">Poster <i class="fa fa-sort"></i></th>
  </tr>
</thead>
<tbody>
<?php foreach($movie->getMovies() as $m) { ?>

  <tr>
  <td id="a"><a href="?p=single&id=<?php echo $m['id']; ?>" class="card-link"><?php echo $m['title']; ?></a></td>
  <td id="b"><?php echo $m['genres']; ?></td>
  <td id="a"><?php echo $m['release_date']; ?></td>
  <td id="b"><img class="img-thumbnail img-fluid" src="<?php echo $movie->getPosterPath($m['poster_path'], false, 92, 138); ?>" alt="<?php echo $m['title']; ?>"></td>
  </tr>

 <?php } ?>
 </tbody>
 </table>
 <script>
  $(document).ready(function($) { 
  $("#movie_list").stupidtable();
  }); 
</script>
</div>
</div>
</div>

For the pagination, I have this code :

$page = (isset($_GET['page']) ? $_GET['page'] : 1);
$perPage = (isset($_GET['per-page']) && ($_GET['per-page']) <= 50 ? $_GET['per-page'] : 5);
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;


$sql = "select * from movies limit ".$start." , ".$perPage." ";
$total = $db->query("select * from tasks")->num_rows;
$pages = ceil($total / $perPage);

$rows = $db->query($sql);

and

        <ul class="pagination">
            <?php for($i = 1 ; $i <= $pages; $i++): ?>
            <li><a href="?page=<?php echo $i;?>&per-page=<?php echo $perPage;?>"><?php echo $i; ?></a></li>

        <?php endfor; ?>
        </ul>

I have tried to integrate the pagination code into mine but I got stuck. Could you tell me how to do it? Thanks alot

  • 写回答

1条回答 默认 最新

  • doulu1325 2018-07-24 15:02
    关注

    OMG. Please try to use some popular MVC frameworks.

    So far just few ideas and comments I have about your code and pagination you want.

    1st:

    public function getMovies($offset = null) {
    

    That means you was about to pass $offset to this method somewhere. At the moment I don't see where do you do that.

    if ($offset) $sql .= ' LIMIT '.$offset;
    

    This part a bit surprising, because usually we need to pass 2 variables OFFSET and LIMIT to LIMIT in query like LIMIT 101,20. But probably you will pass $offset = "101,20" that can be the option but bit weird to me.

    2nd:

    $total = $db->query("select * from tasks")->num_rows;
    $pages = ceil($total / $perPage);
    

    Please never do that again. You are running this query just to get $total number of records but you do SELECT * which means if we have 50 columns in this table and 1M records you receive huge portion of data from DB server which you absolutely not needed. So you can just "SELECT COUNT(*) AS total FROM tasks" which will return you just one row with 1 value!

    3rd:

    I don't know what is an order your code is executing but since you have $start, $perPage variables precalculated somewhere you can/should pass them to your model when getMovies() moethod is called. So you will get something like this in your view file:

    <?php foreach($movie->getMovies($start, $limit) as $m) { ?>
    

    and I would change method definition to:

    public function getMovies($offset, $limit) {
    ...
       if ($offset) $sql .= ' LIMIT '.$offset.','$limit;
    

    PS: This is not the real answer. You should improve your code and avoid sql injections vulnerability. But I hope that will give you some direction you can move.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题