Nadeem Sial
Nadeem Sial

Reputation: 23

How to create pagination?

How to create pagination for this script:

if(isset($_GET['cat'])){

  $cat_id=$_GET['cat'];
  $get_cat_pro = "select * from products where cat_id='$cat_id'";

  $run_cat_pro = mysqli_query($db, $get_cat_pro);
  $count=mysqli_num_rows($run_cat_pro);

  if($count==0){ 
    echo "<h2>No Product Found in This Categroies!</h2>";  
  }

  while ($row_cat_pro=mysqli_fetch_array($run_cat_pro)){

    $pro_id = $row_cat_pro['product_id'];
    $pro_title = substr($row_cat_pro['product_title'],0,25);

    $pro_cat = $row_cat_pro['cat_id'];
    $pro_brand = $row_cat_pro['brand_id'];
    $pro_desc = $row_cat_pro['product_desc'];

    $pro_image = $row_cat_pro['product_img1'];

    echo "
      <div id='single_product' style='margin:0.5%; width:242px; height:470px; float:left; border:5px solid white; box-shadow:0px 0px 10px #000;' class='bg-warning'>

        <a href='details.php?pro_id=$pro_id' style='text-decoration:none;'>
          <h5 style='margin-left:10%; text-decoration:none;'><b>$pro_title...</b></h5>
        </a>

        <p>     
          <a href='details.php?pro_id=$pro_id' style='text-decoration:none;'>
            <img src='products_images/$pro_image' style='border:5px solid white;' width='210' height='210' vspace='10' hspace='10'/>
          </a>

          <br><br><b style='margin-left:10%;'>Views:</b> &nbsp &nbsp
          <span class='badge'>15000</span>
          <br><b style='margin-left:10%;'>Downloads:</b> &nbsp
          <span class='badge'>100</span>
        </p>

      </div>

    ";

  }
}

Upvotes: 0

Views: 275

Answers (1)

First you need to add "LIMIT N,M" to your query, where N stands for the offset and M for the amount of results.

Next you need to do some math.

For example, if you want to have 6 Results per Page

your LIMIT would look like this LIMIT 0,6. But only on the first page. So now, you need a Get Parameter which holds the current page.

So you could multiply the current page.

For example 0,6 ist the first page, second page needs to start at 7 and end at 13 and so on

Upvotes: 1

Related Questions