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?