i am trying to archive pagination with single php sql query as i feel its too much running a select statement twice just because i want to paginate my data i have tried the query function (*)count
i have tried just setting a limit and if the number_of_rows
is equal to my limit i automatically paginate this works for two pages but i can't determine up to third page so its not a good approach.
another way i am thinking of is getting all the data then use java script to paginate but i have no idea how to do that so i am trying to do it with php but will like to learn how to do it with java script if possible.
i just want to run only one query and paginate thanks for your help
here is my code:
<?php
//count rows in table
$query= "SELECT * FROM $database.$table WHERE `id` <> '$id'";
$result= mysqli_query($conn, ($query));
$results_per_page =30;
//calculate total pages
if($result)
{
$num_row = mysqli_num_rows($result);
mysqli_free_result($result);
$total_pages = ceil($num_row / $results_per_page);
}
//to get page value
if(isset($_GET["page"])){
$page =$_GET["page"];
} else{
$page=1;
};
$start_from = ($page-1) * $results_per_page;
$start_from_here = mysqli_real_escape_string($conn, stripslashes($start_from));
$start_from_here = filter_var($start_from_here, FILTER_SANITIZE_NUMBER_INT );
//get data to display
$query1= " SELECT * FROM $database.$table
WHERE `id` <> '$id'
ORDER BY type ASC
LIMIT $results_per_page OFFSET $start_from_here ";
$result1= mysqli_query($conn, ($query1));
and my html pagination code is like so
<?php
if(isset($total_pages) && ($total_pages>1)) {
$url="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url=strtok($url, '?');
for ($i=1; $i<=$total_pages; $i++) {
//Print links for all pages
echo "<a class='pagination' href='".$url."?page=".$i."'>".$i."</a>";
}
}
?>
thank you for all your help