pagination with search result - php

I am using pagination with my script. But even it has few search cafeterias. And my pagination is also not working because of that search criteria. I want to display all the records 1st and then if the search criteria given it has to display according to that. Then only my pagination works.
But i am not getting how to do
here is my script which is to display records from database with pagination
if(isset($_GET['page'])){ $page=$_GET['page']; } else { $page=1; }
$start_page=($page-1)*5;
`$sql="query";
if(isset($_POST['submit']))
{
$company = $_POST['company'];
$product = $_POST['product'];
if(isset($_POST['submit']) && ($_POST['company']!='') && ($_POST['product']!=''))
{
$sql= "My query goes here";
}
elseif(isset($_POST['submit']) && ($_POST['company']!=''))
{
$sql= "My query goes here";
}
elseif(isset($_POST['submit']) && ($_POST['product']!=''))
{
$sql= "My query goes here";
}
elseif(isset($_POST['submit']))
{
$sql= "My query goes here";
}
}
$sql1 = mysql_query($sql) or die(mysql_error());
//Display
?>
<div class="w-box w-box-blue">
<div class="w-box-header">
</div>
<div class="w-box-content">
<table id="dt_hScroll" class="table table-striped">
<thead><tr>
<th>Order ID</th>
<th>Company</th>
<th>Reference</th>
<th>Total Value</th>
<th>Action</th>
</tr>
</thead>
<?php
while($row=mysql_fetch_array($sql1))
{
//Display
}
?>
</table>
</div>
</div>
<?php
//PAGINATION SCRIPT
$sql1="select COUNT(order_id) from orders";
$rs_result = mysql_query($sql1);
$row1 = mysql_fetch_row($rs_result);
$total_records = $row1[0];
$total_pages = ceil($total_records / 5);
if ($page > $total_pages) {
// set current page to last page
$page = $total_pages;
} // end if
// if current page is less than first page...
if ($page < 1) {
// set current page to first page
$page = 1;
} // end if
$range=4;
echo "<div style='float:left; width:200px;text-align:center; margin-left:40%;'>";
// echo "<a href='view_student.php?page=".$i."'>".$i."</a> ";
if ($page > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?page=1'>First</a> ";
// get previous page num
$prevpage = $page - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>Previous</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($page - $range); $x < (($page + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $total_pages)) {
// if we're on current page...
if ($x == $page) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($page != $total_pages) {
// get next page
$nextpage = $page + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>Next</a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?page=$total_pages'>Last</a> ";
} // end if
echo "</div>";
?>

Try using some kind of query builder, that would make your task easier.
All popular frameworks have that kind of tool. It would be much easier to build count query for pagination.
You may look here for example http://framework.zend.com/manual/1.12/en/zend.db.select.html

Related

php search pagination is not working

i can make pagination by query database and it works just fine. but when i use form to search from database, i can only get the first page data, the next page data won't show up.
i just cannot figure out how to maintain the search query..
this is my code. the problem should be in the url links in the pagination, but i just cannot see the problem
<?php
require('koneksi.php');
if(isset($_GET['search'])) {
$search = $_GET['search'];
$keyword = $_GET['keyword'];
$koneksi = mysqli_connect("localhost","root","","mycompany");
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM product WHERE deskripsi LIKE '%" . $keyword . "%'";
$result = mysqli_query($koneksi,$sql);
$rss = mysqli_fetch_row($result);
$numrows = $rss[0];
//numbers or rows to show per page
$rowperpage = 6;
//find out total pages
$totalpages = ceil($numrows/$rowperpage);
//get the current page or set default
if(isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page number
$currentpage = 1;
} // end if
// if the current page is greater than total pages...
if($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than total pages...
if($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowperpage;
$sql = "SELECT * FROM product WHERE deskripsi LIKE '%" . $keyword . "%' LIMIT $offset, $rowperpage";
$result = mysqli_query($koneksi, $sql);
// while there are rows to be fetched
while ($list = mysqli_fetch_assoc($result)) {
// echo data
echo $list['product_code'];
echo "<br>";
echo $list['deskripsi'];
}
/****** build the pagination links ******/
// range of num links to show
$range = 6;
$url = "searchbar.php";
// if not on page 1, don't show back links
if($currentpage > 1) {
// show << link to go to page 1
echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=1'> << </a>";
//get previous page num
$prevpage = $currentpage - 1;
} // end if
echo " <li class='arrow'><a href='$url?currentpage=prevpage?&keyword=$keyword?cari=$cari'>«</a></li> ";
for($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it is a valid page number
if(($x > 0) && ($x <= $totalpages)) {
// if we are on current page
if($x == $currentpage) {
echo "<li class=''><a href=''> $x </a></li>";
} else {
// make it a link
//echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'> $x </a> ";
//echo "<li class=''><a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x </a></li>";
echo "<li class=''><a href='$url?currentpage=$x?&keyword=$keyword?cari=$cari'> $x </a></li>";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <li class='arrow'><a href='$url?currentpage=$nextpage?&keyword=$keyword?cari=$cari'>»</a></li> ";
// echo forward link for lastpage
// echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'> >> </a> ";
} // end if
} // end if get search
require('footer.php');
?>
Looks like there could be a couple things missing here. For instance:
echo " <li class='arrow'><a href='$url?currentpage=prevpage?&keyword=$keyword?cari=$cari'>«</a></li> ";
in this line you are missing a $ for the prevpage: $prevpage but the querystring in the URL should only start with a ? and not contain question marks elsewhere, so this line should read more like
echo " <li class='arrow'><a href='$url?currentpage=$prevpage&keyword=$keyword&cari=$cari'>«</a></li> ";
I'm not 100% sure if that's going to fix your issue, but there is one big thing that I would ask you to look into before actually using this code anywhere and that's the SQL Injection that you have in your query.
You might read a bit of How can I prevent SQL injection in PHP? to get a better idea of how to rewrite your sql queries.
So, checkout your links, make sure the querystring is formatted correctly (http://host.com/script.php?querystring=something&var2=anothervar) where variables are separated only by &
As #aaronott pointed out, most of your links are wrong.
You are using cari=$cari which is not set anywhere, while in fact I guess you should add search=1 (or search=$search, but it's not really useful).
Also, you cannot have more than on ? in your querystring, so fix all your links like this:
...
if($currentpage > 1) {
echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=1&search=1&keyword=$keyword'> << </a>";
$prevpage = $currentpage - 1;
}
echo " <li class='arrow'><a href='$url?currentpage=$prevpage&search=1&keyword=$keyword'>«</a></li> ";
...
if($x == $currentpage) {
echo "<li class=''><a href=''> $x </a></li>";
} else {
echo "<li class=''><a href='$url?currentpage=$x&search=1&keyword=$keyword'> $x </a></li>";
} // end else
...
echo " <li class='arrow'><a href='$url?currentpage=$nextpage&search=1&keyword=$keyword'>»</a></li> ";

Pagination not showing any results after 1st page

I'm using this pagination script, and it is showing the correct number of records for the first page, and the correct number of page links. When i click a page link however, the other pages show no records or page links at all.
Can anyone see where the problem is?
Thanks for looking.............
if(isset($_GET['brand'])){
$brand = $_GET['brand'];
$sql = mysqli_query($link, "SELECT COUNT(id) FROM products WHERE brand = '$brand'
AND status = 1 ORDER BY id DESC") OR die(mysqli_error($link));
$r = mysqli_fetch_row($sql);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 1;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];}
else {
// default page num
$currentpage = 1;} // end if
if ($currentpage > $totalpages) {
$currentpage = $totalpages;} // end if
// if current page is less than first page...
if ($currentpage < 1) {
$currentpage = 1;} // end if
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db
$sql = mysqli_query($link, "SELECT *FROM products WHERE brand = '$brand' AND
status 1 ORDER BY id DESC LIMIT $offset, $rowsperpage") OR
die(mysqli_error($link));
echo"<div class='brandheading'>",
$brand,
"</div>";
if (!mysqli_num_rows($sql)){
echo 'No Products Match That Brand';}
else{
/****** build the pagination links ******/
echo" <div class='pagination'>";
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
}
else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
echo"</div>";
while($row = mysqli_fetch_array($sql)){
$data = $row['image'];
$file = substr($data, strpos($data, "/") + 1);
echo "<div class='featuredproduct'>",
"<a class='featuredlink' href='products.php?product=" . $row['id'] . "'>",
"<div class='productimage'>",
"<img class='featuredproductimage' src='$file' alt='{$row['name']} Image'
/>",
"</div>",
"<div class='featuredproductname'>{$row['name']}</div>",
"<div class='featuredproductprice'>&pound{$row['price']}</div>",
"</a>",
"</div>";
}
}
}
When going to next and/or previous pages you are not passing the brand.
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
should be
echo " <a href='{$_SERVER['PHP_SELF']}?brand=$brand&currentpage=1'><<</a> ";

How to continue numbered list in all next pages with pagination?

I am trying to display data in an ordered list with pagination in php. It is working fine on page one but on next page, the list starts again from 1 instead of continuing from previous number.
Here is my code:
<?php
// find out how many rows are in the table
$sql01 = mysql_query("SELECT COUNT(*) FROM pm,pm_reply");
$r = mysql_fetch_row($sql01);
$numrows = $r[0];
$rowsperpage = 3;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
}
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
}
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
}
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db
// while there are rows to be fetched...
$sql=mysql_query("select *from pm where send_to='$_GET[name]' limit $offset, $rowsperpage");
$sql2=mysql_query("select *from pm_reply where send_to='$_GET[name]' limit $offset, $rowsperpage");
echo "<ol>";
while($rows=mysql_fetch_assoc($sql)) {
echo"<li>From: <a href='profile.php?name=$rows[send_by]'>$rows[send_by]</a><br><br><a style='text-decoration:none; font-size:14pt;' href='view_pm.php?name=$_GET[name]&pm=$rows[subject]'>$rows[subject]</a></li>";
echo "<hr>";
echo "<br>";
}
while($rows2=mysql_fetch_assoc($sql2)) {
echo"<li>From: <a href='profile.php?name=$rows2[replied_by]'>$rows2[replied_by]</a><br><br><a style='text-decoration:none; font-size:14pt;' href='view_pm.php?name=$_GET[name]&pm=$rows2[subject]'>$rows2[subject]</a></li>";
echo "<hr>";
echo "<br>";
}
echo "</ol>";
echo "</div>";
//End Div Content
echo "<div style='clear:both;'></div>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<div id='pagelinks'>";
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?name=$_GET[name]&currentpage=1'>First...</a> ";
// get previous page num
$prevpage = $currentpage - 1;
}
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
// if we're on current page...
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
} else {
// if not current page...
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?name=$_GET[name]&currentpage=$x'>$x</a> ";
}
}
}
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?name=$_GET[name]&currentpage=$totalpages'>...Last</a> ";
}
Just add start="$offset" to your echo "<ol>"; line.
echo "<ol start='$offset'>";

Automatic pagination for status board program

i have more data's in my mysql database. i want to display the data, per page 10 data only i need to display, for this i wrote pagination code. its working very fine but i want to run that pagination automatically that means automatically after few seconds the page turns to second page then third page etc... but i don't know how to implement please help anyone. Here below the sample code for reference:
<?php
include "config.inc";
$sql = "SELECT COUNT(*) FROM test";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
$rowsperpage = 3;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql = "SELECT * FROM test LIMIT $offset, $rowsperpage";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
while ($list = mysql_fetch_array($result)) {
echo $list['mark_cut_weld'] . " : " . $list['mark_cut_inves'] . "<br />";
}
$range = 3;
if ($currentpage > 1) {
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
$prevpage = $currentpage - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
}
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " [<b>$x</b>] ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
}
}
}
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
}
?>
Above code, i just fetch the data from mysql database by php. then set the data per page is 3. i just get the total count and then divide by number of rows into rows per page... then automatically it will display the data.
My target is display the data from database. per page 10 data's and then automatically it move to next page for next 10 data without any action click or submit...
Because it is status board program.. we going to display by big tv in factory... so workers can see the status of the work in this big tv.
You can set a header redirect to redirect to next page.
For example the following code will redirect you to next page in 10 seconds.
header('Refresh: 10; URL='.$_SERVER['PHP_SELF'].'?page='.$next_page);
Make sure you set the header before you echo anything in PHP.
You are going to want to use javascript to set timeouts that will redirect the location to the next page every so often.
For example, add this to the bottom of your HTML body:
<script type="text/javascript">
function switchPage(){
window.location = "<?php echo $next_page?>"; // set the next page of results to view.
}
setTimeout(switchPage,60*1000); // call callback every minute
</script>
The variable $next_page will need to be a URL to the next set of results using PHP.
To have it repeat you will need a modulus on the PHP side that flips back to page 0 when the end of the results has been reached.
<?php
$next_page_count = ++$currentpage % $totalpages;
$next_page = $_SERVER['PHP_SELF'] . '?currentpage=' . $next_page_count;

Using a count ++ function with pagination

The HTML table below is paginated. On page one, $count++ starts at one and then goes up, which is what I want.
The problem is that when I click on page two, $count++ starts at one again.
How can I make it start at 101 on page two?
$presult = mysql_query("SELECT COUNT(*) FROM login") or die(mysql_error());
$rr = mysql_fetch_row($presult);
$numrows = $rr[0];
$rowsperpage = 100;
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
$tzFrom3 = new DateTimeZone('America/New_York');
$tzTo3 = new DateTimeZone('America/Phoenix');
$sqlStr3 = "SELECT
loginid,
username,
created,
activated
FROM login
WHERE activated = 1
ORDER BY created ASC
LIMIT $offset, $rowsperpage";
$result = mysql_query($sqlStr3);
$arr = array();
echo "<table>";
while ($row = mysql_fetch_array($result)) {
$dt3 = new DateTime($row["created"], $tzFrom3);
$dt3->setTimezone($tzTo3);
echo '<tr class="backgroundnonttsu">';
echo '<td>'.$count++.'</td>';
echo '<td >'.stripslashes($row["username"]).'</a></td>';
echo '<td >'.$dt3->format('F j, Y').'</td>';
echo '</tr>';
}
echo "</table>";
$range = 3;
/****** build the pagination links ******/
// range of num links to show
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <div class='pages'><a href='http://www.domain.com/directory/file.php?currentpage=1' class='links'><<</a></div> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <div class='pages'><a href='http://www.domain.com/directory/file.php?currentpage=$prevpage' class='links'><</a></div> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " <div class='pages'>[<b>$x</b>] </div>";
// if not current page...
} else {
// make it a link
echo " <div class='pages'><a href='http://www.domain.com/directory/file.php?currentpage=$x' class='links'>$x</a></div> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <div class='pages'><a href='http://www.domain.com/directory/file.php?currentpage=$nextpage' class='links'>></a></div> ";
// echo forward link for lastpage
//echo " <div class='pages'><a href='http://www.domain.com/directory/file.php?currentpage=$totalpages' class='links'>>></a></div> ";
} // end if
/****** end build pagination links ******/
if($currentpage==1){
$count=1;
}else{
$count =1+($currentpage*$rowsperpage);
}
Just do
$count = $offset + 1
right after you set offset. You only need one line of code.

Categories