duanmei2459 2018-09-06 10:40
浏览 47
已采纳

使用自定义分页在PHP中编写mysql结果编号

I have PHP code in basic custom MVC design that is intended to search for records in a MySQL database and then return/output the results in a paginated page. My problem is that the results in page 1 are numbered 1 -20 as well as those in page 2, page 3 etc.

Ideally results in page 2 should be 21-40, those in page 3 should be 41-60...etc

Here is the code snippet of the controller

function getCitation() {
    $limit = 20;
    $currentpage = isset($_GET['page']) ? (int)$_GET['page'] : 1;
    $start_from =((($currentpage*$limit)-$limit)+1);
    $term = isset($_GET['searchterm']) ? $_GET['searchterm'] : null;
    //if(isset($_GET['mysearch']))
    if(isset($_GET['search1']) && empty($term) && ($_GET['choice'] == 'title' || $_GET['choice'] == 'author')){
        header ('Location: ' . '../index');     
    }
    $this->view->getCitation = $this->model->getCitation($term,$start_from,$limit);
    //The line below is critical for pagination in view.
    $this->view->getPages = $this->model->getPages($term, $limit);
    $this->view->render('search/index');
 }

The Model looks like this

public function getCitation($term, $start_from, $limit){
    $sth = $this->db->prepare("SELECT * FROM citations_new WHERE title 
        LIKE ? LIMIT {$start_from}, {$limit}");
    $sth->execute(array('%'.$term.'%'));
    return $rows = $sth->fetchAll(PDO::FETCH_ASSOC);
}
public function getPages($term, $limit){
    echo $term;
    $sth = $this->db->prepare("SELECT Count(*) FROM citations_new WHERE title 
        LIKE ?");
    //echo 1;
    $sth->execute(array('%'.$term.'%'));
    $results = $sth->fetchColumn();
    $total_records = $results;
    return $total_pages = ceil($total_records/$limit);
}

The view looks like this:

$x=1;
while($row = array_shift($this->getCitation())){
    echo $x++;
    echo $row['title'] . '<br>';
}
for($i=1; $i<=$this->getPages; $i++):?>
    <a href="?searchterm=<?php echo $_GET['searchterm'].'&search1=Submit+Query&page=' . $i; ?>">
    <?php echo $i;?></a>
<?php endfor;?>

When i echo-out the line $start_from =((($currentpage*$limit)-$limit)+1); on the controller, i am able to see the correct start_number. How can i get my results be numbered sequentially?

  • 写回答

1条回答 默认 最新

  • duanraotun1674 2018-09-06 10:59
    关注

    Unless I misunderstood, didn't you solve this yourself ? Why can't you set the counter as you did the test ?

    Adding a bit to the answer, so we need to set an array of the viewdata, an add to it the proper info Then we need to pass the counter from the controller to the view

    controller:

    $viewdata = array();
    $viewdata['results'] = $this->model->getCitation($term,$start_from,$limit);
    $viewdata['pagecounter'] = ((($currentpage*$limit)-$limit)+1);
    

    $this->view->getCitation = $viewdata;

    view:

    $viewdata = $this->getCitation();
    
    
        $x= $viewdata['pagecounter'];
        while($row = array_shift($viewdata['results'])){
            echo $x++;
            echo $row['title'] . '<br>';
        }
        for($i=1; $i<=$this->getPages; $i++):?>
            <a href="?searchterm=<?php echo $_GET['searchterm'].'&search1=Submit+Query&page=' . $i; ?>">
            <?php echo $i;?></a>
        <?php endfor;?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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时遇到的编译问题