php pagination code is not working - php

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

Related

serial number changing next to next page in pagnation

I need serial number next to next page, first page from 1 to 10, next from 11 to 20...... but my code prints 1 to 10 for all pages.
<?php
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$number = 1;
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
$total_pages_sql = "SELECT COUNT(*) FROM regs";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
//print_r($total_pages);
$result = mysqli_query($conn, "SELECT * FROM regs LIMIT $offset, $no_of_records_per_page");
while($res = mysqli_fetch_array($result)) {
// How many elements per page
echo "<tr>";
echo "<td>".$res['FirstName']."</td>";
echo "<td>".$res['Email']."</td>";
echo "<td>".$res['Gender']."</td>";
echo "<td>View | Edit | Delete</td>";
$pageNumber = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$currentNumber = ($pageNumber - 1) * $no_of_records_per_page + $number;
echo "<td> ". $currentNumber++ ." </td>";
$number++;
}
?>
your code should be like that
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$number = 1;
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
$total_pages_sql = "SELECT COUNT(*) FROM regs";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
//print_r($total_pages);
$result = mysqli_query($conn, "SELECT * FROM regs LIMIT $offset, $no_of_records_per_page");
while($res = mysqli_fetch_array($result)) {
// How many elements per page
echo "<tr>";
echo "<td>".$res['FirstName']."</td>";
echo "<td>".$res['Email']."</td>";
echo "<td>".$res['Gender']."</td>";
echo "<td>View | Edit | Delete</td>";
$currentNumber = ($pageno - 1) * $no_of_records_per_page + $number;
echo "<td> ". $currentNumber++ ." </td>";
$number++;
}

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?

php next page blank for search result

my default load page is ok with next and prev results.. but for search results i can get only first page then blank for the next page. let say total search results row is 9, it display 3 pages with next link. but when i click the next link, it queries no result on the page 2. here is my code:
echo '<table><tr><td>';
echo '<form method="post" name="frmSearch" action="mypage.php>
Search by name
<input type="text" name="txtSearch">
<input type="submit" name="submit" value="Search">
</form>';
$per_page = 5;
$page = 1;
if (isset($_GET['page']))
{
$page = intval($_GET['page']);
if($page < 1) $page = 1;
}
$start_from = ($page - 1) * $per_page;
$Prev_Page = $page - 1;
$Next_Page = $page + 1;
if (!$_POST){
//$sql = "SELECT * FROM mytable LIMIT $start_from, $per_page";
//$totalr = mysql_query("SELECT COUNT(*) FROM mytable");
//$totalr = mysql_fetch_row($totalr);
//$totalr = $totalr[0];
//$total_pages = $totalr / $per_page;
//$total_pages = ceil($total_pages);
//$listresult = mysql_query($sql);
//$total = mysql_num_rows($listresult);
}
else {
if ($_POST['txtSearch']!="") {$cond1 = " name LIKE
'%".$_POST['txtSearch']."%' ";} else {$cond1 = 1; }
$sql = "SELECT * FROM mytable WHERE ".$cond1." LIMIT $start_from, $per_page";
$totalr = mysql_query("SELECT COUNT(*) FROM mytable WHERE name LIKE
'%".$_POST['txtSearch']."%' ");
$totalr = mysql_fetch_row($totalr);
$totalr = $totalr[0];
$total_pages = $totalr / $per_page;
$total_pages = ceil($total_pages);
$listresult = mysql_query($sql);
$total = mysql_num_rows($listresult);
}
if($total == 0){
echo "<center>No records found.</center>";
}
echo "<span style='float:right;margin-top:8px;'>[ <a href='new.php'>Add New</a>
]</span></td></tr>";
echo "<table>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
";
while ($_POST = mysql_fetch_assoc($listresult)){
echo "<tr>";
echo "<td>" . $_POST['id'] . "</td>";
echo "<td>" . $_POST['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</table>";
echo $totalr." result";
echo "<br>";
if ($Prev_Page) {
echo " <a href ='{$_SERVER['PHP_SELF']}?page=$Prev_Page'><< Prev</a> ";
}
if ($totalr > $per_page) {
for($i = 1; $i <= $total_pages; ++$i)
{
echo "<a href='{$_SERVER['PHP_SELF']}?page=$i'>$i</a> | ";
}
}
if ($page!=$total_pages) {
echo " <a href='{$_SERVER['PHP_SELF']}?page=$Next_Page'>Next >></a> ";
}
maybe you need to use SELECT COUNT(*) FROM mytable WHERE name LIKE '%$search%' ... remember to escape the $_POST['txtSearch']

Pagination Database Query Woes

I have been working at this all day and still at 8pm have had no luck. I was wondering if you guys can give me some advice on fixing it. I am building an Upload - gif sharing system for University.
Here is my code anyway -
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
mysql_connect("localhost","root","root") or die("Top Query");
mysql_select_db("UPLOAD") or die(mysql_error());
$count_query = mysql_query("SELECT NULL FROM details");
$count = mysql_num_rows($count_query);
//pagination
if(isset($_GET['page'])){
$page = preg_replace("#[^0-9]#","",$_GET['page']);
}else{
$page= 1;
}
$perPage = 5;
$lastPage = ceil($count / $perPage);
if($page < 1){
$page = 1;
}else if($page > $lastPage){
$page = $lastPage;
}
$limit = "LIMIT " .($page -1) * $perPage . ", $perPage";
//Query and gifs
$query = mysql_query("SELECT * FROM details ORDER BY date_added DESC") or die("2nd Query");
//Puts it into an array
$pagination="";
if($lastPage != 1){
if($page != $lastPage){
$next = $page + 1;
$pagination.='More';
}
if($page != 1){
$prev = $page - 1;
$pagination.='Back';
}
}
?>
And then the output in the html -
<?php
while($info = mysql_fetch_array($query)){
$shortlink = "".$info['photo']." " ;
//Outputs the image and other data
echo "<article class='upload-post'>" . "<div class='crop'>";
echo "<a href=uploads/".$info['photo'].">";
echo "<img class='scale-with-grid' src=uploads/".$info['photo'] .">"."</a>";
echo "</div>";
echo "".$info['name'] . "<br/>";
echo "Reaction ".$info['reaction'] ."<br/>";
echo "In " .$info['category'] ." <br/>";
echo "On " .$info['date_added'] ." <br/>";
echo "Link: $shortlink";
echo "</article>";
}
?>
<?php echo $pagination;?>
I can toggle between pages but its not limiting the number of posts displayed on the page. Id really appreciate the help as the deadline isn't too far away.
Thanks a bunch in advance!
You never append $limit to your query.
Try the follwing as a replacement...
...
//Query and gifs
$query = mysql_query("SELECT * FROM details ORDER BY date_added DESC ".$limit) or die("2nd Query");
//Puts it into an array
...

Categories