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:



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

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

code in html part:

    $stmtA = $conn->prepare("SELECT * FROM tbl_products_a154287");
    $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>
    for ($i=1; $i<=$total_pagesAll; $i++)
       if ($i == $pageAll)
          echo "<li class=\"active\"><a href=\"products.php?pageAll=$i\">$i</a></li>";
          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:

$perPage = 6;

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

$options = array('options'=>
$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.

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

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