Hello, I have a products page which lists the pics of the products. I added pagination with bootstrap classes. I have 10 products in my database. I want to limit per page to display only 6 so then the second page should display 4. The issue im facing now is that the second page button doesn't "change" to the second page. but if i click >> button it does change.

pic for visualization:

1.JPG

code:

$per_pageAll = 6;
if (isset($_GET["page"]))
  $pageAll = $_GET["page"];
else
  $pageAll = 1;
  $start_fromAll = ($pageAll-1) * $per_pageAll;

  $stmtAll = $conn->prepare("SELECT id, name, price, type, img FROM tbl LIMIT $start_fromAll, $per_pageAll");
  $stmtAll->execute();
  $resultAll = $stmtAll->fetchAll();

code in html part:

<?php
    $stmtA = $conn->prepare("SELECT * FROM tbl_products_a154287");
    $stmtA->execute();
    $resultA = $stmtA->fetchAll();
    $total_recordsAll = count($resultA);
    $total_pagesAll = ceil($total_recordsAll / $per_pageAll);
    $total_recordsAll = count($resultAll);
    $total_pagesAll = ceil($total_recordsAll / $per_pageAll);

    if ($pageAll==1) { ?>
       <li class="disabled"><span aria-hidden="true">«</span></li>
       <?php } else { ?>
          <li><a href="products.php?page=<?php echo $pageAll-1 ?>" aria-label="Previous"><span aria-hidden="true">«</span></a></li>
       <?php
    }
    for ($i=1; $i<=$total_pagesAll; $i++)
       if ($i == $pageAll)
          echo "<li class=\"active\"><a href=\"products.php?pageAll=$i\">$i</a></li>";
       else
          echo "<li><a href=\"products.php?pageAll=$i\">$i</a></li>";
       ?>
    <?php if ($pageAll==$total_pagesAll) { ?>
       <li class="disabled"><span aria-hidden="true">»</span></li>
    <?php } else { ?>
       <li><a href="products.php?page=<?php echo $pageAll+1 ?>" aria-label="Previous"><span aria-hidden="true">»</span></a></li>
<?php } ?>

I am not sure what i did wrong. I only modified the names. thanks in advance.

I suggest to you use filter_input() method instead of $_GET['page'] and set min, max, default values:

<?php
$perPage = 6;

$stmt = $conn->prepare("select count(*) from tbl");
$stmt->execute();
$productCount = $stmt->fetchColumn();
$totalPages = ceil($productCount/$perPage)-1;

$options = array('options'=>
    array('min'=>0,'max'=>$totalPages,'default'=>0)
);
$page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, $options);
?>

Use 0 for first page is more conveniet for mathematical manipulations. Only innerHTML use (page+1) for show human readable page number.

<?php
if($page===0){
    echo '<li class="disabled"><span aria-hidden="true">«</span></li>';
}
else {
    echo '<li><a href="products.php?page='.($page-1).'" aria-label="Previous"><span aria-hidden="true">«</span></a></li>';
}

for($i=0; $i<=$totalPages; $i++){
    $active = ($i == $page ? ' class="active"' : '');
    echo '<li'.$active.'><a href="products.php?page='.$i.'">'.($i+1).'</a></li>';
}

if($page>=$totalPages){
    echo '<li class="disabled"><span aria-hidden="true">»</span></li>';
}
else {
    echo '<li><a href="products.php?page='.($page+1).'" aria-label="Previous"><span aria-hidden="true">»</span></a></li>';
}

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.