2018-09-06 10:40



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['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);

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}");
    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;
    $results = $sth->fetchColumn();
    $total_records = $results;
    return $total_pages = ceil($total_records/$limit);

The view looks like this:

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • duanraotun1674 duanraotun1674 3年前

    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


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

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


    $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;?>
    点赞 评论 复制链接分享