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;?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法