php postgres SQL pagination - php

I need a pagination based on php/postgres.
With the code below, I can break de records but only shows the page 1 (link).
Any idea to fix this?
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 5;
$query = pg_query($dbconn,"select * from my table limit 5 offset 0") or die(pg_result_error($dbconn));
$total_query = pg_num_rows($query);
$total_pages = ceil($total_query / 5);
the query result:
while($row = pg_fetch_assoc($query)){
...
}
for the pagination:
for ($i=1; $i<=$total_pages; $i++) {
echo "".$i." ";
}

I don't understand your code very well but the basic logic for a pagination sql query is
Select * from pages limit $page_size offset $page_size*($page_no-1);

I've fix it.
The code:
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$recortds = 10; // change here for records per page
$start_from = ($page-1) * $records;
$qry = pg_query($dbconn,"select count(*) as total from table");
$row_sql = pg_fetch_row($qry);
$total_records = $row_sql[0];
$total_pages = ceil($total_records / $records);
$select = pg_query($dbconn,"select * from table limit $records offset $start_from");
the select result:
while($row = pg_fetch_assoc($select )){
echo $row['col1'].' | '.$row['col2'].' | '.$row['col3'].'<br />';
}
the pagination links:
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='index.php?page=".$i."' class='yourclass'>".$i."</a> ";
}

Related

Detect Previous Next ID

I have a little script that have a Previous & Next button.
My problem is I want to detect & place the ID in those buttons
this is the code from the pagination
<body>
<?php include_once 'data.php'; ?>
<center>
<ul class="pagination">
<?php
if($page_counter == 0){
echo "<li><a href=?start='0' class='active'>0</a></li>";
for($j=1; $j < $paginations; $j++) {
echo "";
}
}else{
echo "<a href=?start=$previous><button>Previous</button></a>";
for($j=0; $j < $paginations; $j++) {
if($j == $page_counter) {
echo " ";
}else{
echo " ";
}
}if($j != $page_counter+1)
echo "<a href=?start=$next><button>Next</button></a>";
}
?>
</ul>
</center>
In this part I have the ID but the problem is I can`t get it with this example to place it into the pagination.
<?php
foreach($result as $row) {
echo '
<div class="card"><button1>
'. $row['notice_id'] .'
<div class="time-left">
<div class="dropdown1">
' ;
}
}
else {
echo '';
}
$conn->close();
?>
</div></div>
This is the code from data.php I think I need to place some code into for detection from the ID
<?php
//include configuration file
require 'configuration.php';
$start = 0; $per_page = 1;
$page_counter = 0;
$next = $page_counter + 1;
$previous = $page_counter - 1;
if(isset($_GET['start'])){
$start = $_GET['start'];
$page_counter = $_GET['start'];
$start = $start * $per_page;
$next = $page_counter + 1;
$previous = $page_counter - 1;
}
// query to get messages from messages table
$q = "SELECT * FROM group_notice LIMIT $start, $per_page";
$query = $db->prepare($q);
$query->execute();
if($query->rowCount() > 0){
$result = $query->fetchAll(PDO::FETCH_ASSOC);
}
// count total number of rows in students table
$count_query = "SELECT * FROM group_notice";
$query = $db->prepare($count_query);
$query->execute();
$count = $query->rowCount();
// calculate the pagination number by dividing total number of rows with per page.
$paginations = ceil($count / $per_page);
?>
You can use some formulas for calculate limit and offset of results.
For example you have 100 records, and you want to paginate it into 10 records per page, and the formula is like below.
// Get Page from Query String
$page = 1;
if(!empty($_GET["page"])){
$page = $_GET["page"];
}
// Get record count
$count_query = "SELECT * FROM group_notice";
$query = $db->prepare($count_query);
$query->execute();
$count = $query->rowCount();
$per_page = 10; // records per page
$pages = ceil($count/$per_page); // get total page
$start = $page * $per_page - $per_page; // get offset
// Do something with your records
$query = db->prepare("SELECT * FROM group_notice LIMIT ?, ?");
$query->bind_param("ii", $start, $per_page); // you should use bind param if you use prepared statement
$query->execute();
if($query->rowCount() > 0){
$result = $query->fetchAll(PDO::FETCH_ASSOC);
}
if($page > 1){
echo 'Prev'; // Print Prev Page
}
for($i = 1; $i <= $pages){
echo ''.$i.''; // Print Page Number
}
if($page < $pages){
echo 'Next'; // Print Next Page
}
Hope it helps.

url generate multiple time in pagination

My pagination is working good .But problem is that when I click on pagination then its does not generate new link .Its add a page id with old link .Like
videos.php?page=2page=3 and if again i cliked on 4th number pagination .Its show like this videos.php?page=2page=3page=4.
<?php
$limit = 20;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "$que LIMIT $start_from, $limit";
$rs_result = mysql_query($sql);
$result = mysql_query($que);
$total_bookss = mysql_num_rows($result);
$full_linkp = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$full_linkEx=explode('&page',$full_linkp);
$full_link=$full_linkEx[0];
if($total_bookss>$limit){
$total_records = $total_bookss;
$total_pages = ceil($total_records / $limit);
$pagLink = "<nav><ul class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
//$pagLink .= "<li><a href='$full_link.php&page=". $i."'>".$i."</a></li>";
$pagLink .= "<li><a href='$full_link&page=".$i."'>".$i."</a></li>";
}
//show pagination variable
$show_pagination=$pagLink . "</ul></nav>";}
?>
<script>
jq(document).ready(function(){
jq('.pagination').pagination({
items: <?php echo $total_records;?>,
itemsOnPage: <?php echo $limit;?>,
cssStyle: 'light-theme',
currentPage : <?php echo $page;?>,
hrefTextPrefix : '<?=$full_link?>page='
});
});
</script>
Try that
<?php
$limit = 20;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "$que LIMIT $start_from, $limit";
$rs_result = mysql_query($sql);
$result = mysql_query($que);
$total_bookss = mysql_num_rows($result);
$full_linkp = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$full_linkEx=explode('?page',$full_linkp);
$full_link=$full_linkEx[0];
if($total_bookss>$limit){
$total_records = $total_bookss;
$total_pages = ceil($total_records / $limit);
$pagLink = "<nav><ul class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
//$pagLink .= "<li><a href='$full_link.php&page=". $i."'>".$i."</a></li>";
$pagLink .= "<li><a href='$full_link?page=".$i."'>".$i."</a></li>";
}
//show pagination variable
$show_pagination=$pagLink . "</ul></nav>";}
?>
<script>
jq(document).ready(function(){
jq('.pagination').pagination({
items: <?php echo $total_records;?>,
itemsOnPage: <?php echo $limit;?>,
cssStyle: 'light-theme',
currentPage : <?php echo $page;?>,
hrefTextPrefix : '<?=$full_link?>?page='
});
});
</script>

php pagination code is not working

Can anyone point me out why my pagination is not working, What I am doing wrong here ? Working on it for a long time. my url generates like : http://localhost/medapp/admin/medorder.php?page=%209.
<?php
//pagination
$perpage = 3;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
$start_from = ($page-1)*$perpage;
//pagination
$medorder = "SELECT * FROM `medorder` WHERE status='1' order by ID desc";
$result = $db->select($medorder);
if($result){
$i=0;
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>".$i++."</td>";
echo "<td>".$row["uid"]."</td>";
echo "<td>".$row["fullname"]."</td>";
echo "</tr>";
}
// pagination
$query = "select * from medorder";
$result = $db->select($query);
$total_rows = mysqli_num_rows($result);
$total_pages = ceil($total_rows/$perpage);
echo "<span class='pagination'><a href='medorder.php?page=1'>".'First Page'."</a>";
for ($i=1; $i <= $total_pages; $i++) {
echo "<a href='medorder.php?page=".$i."'>".$i."</a>"; }
echo "<a href='medorder.php?page=$total_pages'>".'Last Page'."</a></span>";
//pagination
}
?>
Change your query like this by using LIMIT and OFFSET
$medorder = "SELECT * FROM `medorder` WHERE status='1' order by ID desc LIMIT $start_from,$perpage"; //
in this following line, you have a space before the $i variable:
echo "<a href='medorder.php?page= ".$i."'>".$i."</a>";
should be
echo "<a href='medorder.php?page=".$i."'>".$i."</a>";

PHP pagination script not work for search

I used this code in my all pages. Everywhere this code work well. In case of my search page this code count all page by search words but when I click next(2nd or 3rd) page, its cannot display any item. That means sql query for 2nd or 3rd or any others pages not worked. No error also. Pagination page url goes well also.
My Search code:
if(isset($_POST['searchword'])){
$w = mysqli_real_escape_string($dbh, $_POST['searchword']);
$q = strip_tags($w);
$q = trim ($w);
$limit = 12;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "SELECT * FROM search_data WHERE MATCH(detail) AGAINST('+$q*' IN BOOLEAN MODE) ORDER BY id ASC LIMIT $start_from, $limit";
$execute = $dbh->query("$sql");
$rowcount = $execute->num_rows ;
if ($rowcount > 0 ) {
$row = $dbh->query($sql) ;
while ($row = $execute->fetch_assoc()) {
$detail = $row['detail'];
$url = $row['url'];
echo $detail;
}
}
}
//Pagination
$sql = "SELECT COUNT(id) FROM search_data WHERE MATCH(detail) AGAINST('+$q*' IN BOOLEAN MODE)";
$result = mysqli_query($dbh,$sql);
$row = mysqli_fetch_row($result);
$total_records = $row[0];
if($total_records > 0){$total_pages = ceil($total_records / $limit); }
$pagLink = "<ul class='pagination'>";
if(!empty($total_pages)){for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<li><a href='".$thispage."?page=".$i."'>".$i."</a> </li>";
};
echo $pagLink . "</ul>";
}

How to do pagination in PHP?

I'm trying to do pagination in PHP, everything seems to be working, except that next and previous links don't work, it's only when I manually insert the page number in the URL that it displays data from the database on the next page.
Here is my code:
This is where I initialised the perpage and page. These are at the beginning of the page.
<?php
$per_page=4;
if (isset($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page=1;
}
$page;
echo $start_from = ($page-1) * $per_page;
//$search = $_POST['search'];
?>
And this is for the next and previous links, those ones that display the results depending on what the user wants to see.
<?php
$query = "select * from services";
$result = mysqli_query($link, $query);
$total_records = mysqli_num_rows($result);
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
$prev = $page - 1;
if($page == 1){
echo "<span align='right' class='inactive'>← Prev</span>";
}else{
echo "<a href='livebands.php?page=$prev'><span class='paging-prev'>".'← Prev'."</span></a>";
}
for ($i=1; $i<=2; $i++) {
for ($i=1; $i<=$page; $i++) {
echo "<a href='livebands.php?page=$i'><span class='paging'>" .$i. "</span></a>";
}
}
$page++;
if ($page>$total_pages){
echo "<span align='right' class='inactive'>→ Next</span>";
}else{
echo "<a href='livebands.php?page=$page&per_page=$per_page'><span align='right' class='paging-next'>".'Next →'."</span></a>";
}
?>
If I use you code and hardcode the $total_records variable to 5 for example, the links seem to work.
// $query = "select * from services";
// $result = mysqli_query($link, $query);
// $total_records = mysqli_num_rows($result);
$total_records = 5;
Are you sure that your $total_records is more than 4?

Categories