0

I tried to create pagination with database iteration. This is my code so far.

$per_page = 5;
$result = mysqli_query($connection,"SELECT * FROM inquiries");
$total_records = mysqli_num_rows($result);
$total_pages = ceil($total_records / $per_page);

if (isset($_GET['page'])) {
    $show_page = $_GET['page'];
    if ($show_page > 0 && $show_page <= $total_pages) {
        $start = ($show_page - 1) * $per_page;
        $end = $start + $per_page;
    } else {
        $start = 0;
        $end = $per_page;
    }
} else {
    $start = 0;
    $end = $per_page;
}
$page = intval($_GET['page']);

$tpages=$total_pages;
if ($page <= 0)
    $page = 1;
for ($i = $start; $i < $end; $i++) {
    if ($i == $total_records) {
        break;
    }
    echo mysqli_fetch_array($result,$i,'message');  

that. Since it makes following error.

Warning: mysqli_fetch_array() expects at most 2 parameters, 3 given in..

Can someone help me to solve this error.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Chathuri Fernando
  • 950
  • 3
  • 11
  • 22

1 Answers1

1

You can try SQL_CALC_FOUND_ROWS with limit at first query then SELECT FOUND_ROWS(); as total count.

SELECT SQL_CALC_FOUND_ROWS, Id, Name FROM my_table WHERE <give your condition> LIMIT 0, 10;

# Find total rows
SELECT FOUND_ROWS();
naf4me
  • 403
  • 7
  • 17
  • use echo mysqli_fetch_array($result); in replace of echo mysqli_fetch_array($result,$i,'message'); – naf4me Aug 31 '17 at 03:37
  • But then what happen to $i? Can you please explain that what exactly done through 'SQL_CALC_FOUND_ROWS'? – Chathuri Fernando Aug 31 '17 at 03:40
  • 1
    Handle the OFFSET by caluculation by looping like Select SQL_CALC_FOUND_ROWS uid, name users ORDER BY uid desc LIMIT 5 OFFSET 0 Select SQL_CALC_FOUND_ROWS uid, name users ORDER BY uid desc LIMIT 5 OFFSET 5 Select SQL_CALC_FOUND_ROWS uid, name users ORDER BY uid desc LIMIT 5 OFFSET 15 and do the total count like $count = mysqli_query($connection,"SELECT FOUND_ROWS()"; I hope it does make sense. code is not tested. – naf4me Aug 31 '17 at 04:09
  • Yes it helpful. Thank you – Chathuri Fernando Aug 31 '17 at 04:10