Pagination in search php - php

I have a search form and i want to put a pagination, but when i press the next button or second page on the search using pagination , all the results get messed up, and there is no data being fetched from the query. I don't know where the exact problem could be, but this is my search code:
include ('paginate.php'); //include of paginat page
$per_page = 5;
$find = $_POST['find'];
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find); // number of results to show per page
$result = mysql_query("SELECT * FROM projects WHERE p_name LIKE '%$find%'");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);//total pages we going to have
//------------if page is setcheck-----------------//
if (isset($_GET['page'])) {
$show_page = $_GET['page']; //it will telles the current page
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
// error - show first set of results
$start = 1;
$end = $per_page;
}
} else {
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
$show_page=1;
}
// display pagination
if(isset($_GET['page'])){
$page = intval($_GET['page']);
}else{
$page =1;
}
$tpages=$total_pages;
if ($page <= 0)
$page = 1;
?>
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>Project</th> <th>Country</th> <th>Active</th>
</tr></thead>";
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
?><form name="frmactive" method="POST" action="">
<input type="hidden" name="id" value="<?php echo mysql_result($result, $i, 'p_id');?>" />
<?php
// echo out the contents of each row into a table
echo '<tr><td>' . mysql_result($result, $i, 'p_name') . '</td>';
echo '<td>' . mysql_result($result, $i, 'p_country') . '</td>';
if (mysql_result($result, $i, 'p_isActive')=='1'){
echo '<td>Active</td>';
echo '<td align="center">Edit</td>';
}
else{
echo'<td>Inactive</td> ';
echo '<td align="center">Edit</td>';
echo "</tr>";
}
// close table>
}
echo "</table>";
// pagination
?>
and this is the paginate.php
function paginate($reload, $page, $tpages) {
$adjacents = 2;
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "";
// previous
if ($page == 1) {
$out.= "<span>" . $prevlabel . "</span>\n";
} elseif ($page == 2) {
$out.= "<li>" . $prevlabel . "\n</li>";
} else {
$out.= "<li>" . $prevlabel . "\n</li>";
}
$pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
$pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out.= "<li class=\"active\"><a href=''>" . $i . "</a></li>\n";
} elseif ($i == 1) {
$out.= "<li>" . $i . "\n</li>";
} else {
$out.= "<li>" . $i . "\n</li>";
}
}
if ($page < ($tpages - $adjacents)) {
$out.= "<a style='font-size:11px' href=\"" . $reload . "&page=" . $tpages . "\">" . $tpages . "</a>\n";
}
// next
if ($page < $tpages) {
$out.= "<li>" . $nextlabel . "\n</li>";
} else {
$out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
}
$out.= "";
return $out;
}
Could you please point out where the bug is taking place ? Thank you for your time

Try this (pay attention to the comments please) :
<?php
/*
Assuming that your search form has a POST method set to it,
be sure to do a redirect to this page and send the find parameter gotten
from the posted form urlencoded
ie path_to_my_file?find='.urlencode(trim($_POST['find']))
*/
include ('paginate.php'); //include of paginat page
$per_page = 5;
$find = strip_tags(strtoupper(trim(urldecode($_GET['find']))));
$page = intval($_GET['page']);
$result_total = mysql_query("SELECT COUNT(*) AS total FROM projects WHERE p_name LIKE '%".$find."%'");
$tmp_total = mysql_fetch_assoc($results_total);
$totalpages = ceil($tmp_total['total'] / $per_page);
if ($totalpages != 0 && $page > $totalpages) {
$page = $totalpages;
} else if ($page < 1) {
$page = 1;
}
$offset = ($page - 1) * $per_page;
$result = mysql_query("SELECT * FROM projects WHERE p_name LIKE '%".$find."%' LIMIT ".$offset.", ".$per_page);
$show_page = 1;
//You need to pass the phrase you are searching for in every link in order for the pagination to work (or user Sessions to save the keyword somewhere if you don't want it in the link)
$reload = $reload = 'http://localhost/Personal/Stackoverflow/2/index.php?find='.urlencode($find);
echo ' <div class="pagination">
<ul>';
if ($totalpages > 1) {
echo paginate($reload, $show_page, $totalpages);
}
echo ' </ul>
</div>';
// display data in table
if(mysql_num_rows($result) > 0) {
echo ' <table class="table table-bordered">
<thead>
<tr>
<th>Project</th>
<th>Country</th>
<th>Active</th>
</tr>
</thead>';
while($row = mysql_fetch_assoc($result)) {
echo ' <tr>
<td>'.$row['p_name'].'</td>
<td>'.$row['p_country'].'</td>
<td>'.((intval($row['p_isActive']) == 1) ? 'Active' : 'Inactive').'</td>
<td align="center">
Edit
</td>
</tr>';
}
echo ' </table>';
}
?>

Related

PHP MySQL pagination mysqli_result() error

I'm working on pagination, but it didn't work I tried using mysqli_fetch_array and also tried mysqli_result.
Please guide me if you have some ideas.
For the pagination page I tried a method that I found but still have problem with it.
Here is the code I tried,
<?php
$conn = mysqli_connect("localhost", "root", "", "vm");
include 'paginate.php';
$per_page = 2;
$result = mysqli_query($conn, "SELECT * FROM table1");
$total_results = mysqli_num_rows($result);
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page'])) {
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
$start = 0;
$end = $per_page;
}
} else {
$start = 0;
$end = $per_page;
}
$page = intval($_GET['page']);
$tpages = $total_pages;
if ($page <= 0) {
$page = 1;
}
function paginate($reload, $page, $tpages)
{
$adjacents = 2;
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "";
// previous
if ($page == 1) {
$out .= "<span>" . $prevlabel . "</span>\n";
} elseif ($page == 2) {
$out .= "<li>" . $prevlabel . "\n</li>";
} else {
$out .= "<li>" . $prevlabel . " \n</li>";
}
$pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
$pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out .= "<li class=\"active\"><a href=''>" . $i . "</a></li>\n";
} elseif ($i == 1) {
$out .= "<li>" . $i . "\n</li>";
} else {
$out .= "<li>" . $i . "\n</li>";
}
}
if ($page < ($tpages - $adjacents)) {
$out .= "<a style='font-size:11px' href=\"" . $reload . "&page=" . $tpages . "\">" . $tpages . "</a>\n";
}
// next
if ($page < $tpages) {
$out .= "<li>" . $nextlabel . " \n</li>";
} else {
$out .= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
}
$out .= "";
return $out;
}
Thanks in advance.

PHP pagination page zero issue

My pagination is showing a page zero (0) when you go to page 2. Not sure why. I don't want to show a page zero.
I'll try to only show the necessary code.
Here is my code:
<?php
$rec_limit = 100;
$targetpage = "dispatch.php";
if (isset($_GET['page']))
{
$page = $_GET['page'];
$offset = $rec_limit * ($page - 1);
}
else
{
$page = 1;
$offset = 0;
}
*** $left_rec = countRecords() - ($page * $rec_limit); ***
$total_records = countRecords(); // countRecords() should be self-explanatory
$total_pages = ceil($total_records / $rec_limit); // $rec_limit is 100
$adjacents = 2;
$previousPage = $page - 1;
$nextPage = $page + 1;
$querystring = "";
$start = ($page < $adjacents ? 1 : $page - $adjacents); // <-- i think the issue is here
$beginning = 1;
$end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);
foreach ($_GET as $key => $value)
{
if($key != "page") $querystring .= "$key=$value&";
}
echo "<div class="row-fluid"><div class="span2"><ul class="pager"><li>First</li>";
if ($left_rec < $rec_limit)
{
$last = $page - 1;
echo #"<li>Previous</li>";
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
}
else if($page == 0)
{
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
}
else if ($page > 0)
{
$last = $page - 2;
echo "<li>Previous</li> ";
for($i= $start; $i <= $end; $i++)
{
echo #"<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
}
echo "<li>Last</li>";
echo '</ul></div></div>';
?>
I would really appreciate the help in removing page 0 from the application. Please disregard any typos or missing quotes. The code works with the exception of it showing page 0.
I added a picture of what the application showing page 0. It only shows page 0 when I go to page 2. After that, I no longer see page 0.
Please let me know what I have to do.
Thanks.
Some advice:
You really shouldn't suppress errors using #, instead you should be instantiating all of your variables and writing proper code.
Don't hard-code pagination into each page. Instead, wrap it in a reusable function.
Example:
// draws a menu for navigating multiple pages of content
function paginate($page, $display, $total) {
if(isset($_SERVER['QUERY_STRING']) && trim($_SERVER['QUERY_STRING']) != '') {
if(stristr($_SERVER['QUERY_STRING'], 'page=')) {
$query = '?' . preg_replace('/page=\d+/', 'page=', $_SERVER['QUERY_STRING']);
} else {
$query = '?' . $_SERVER['QUERY_STRING'] . '&page=';
}
} else {
$query = '?page=';
}
$pages = $total <= $display ? 1 : ceil($total / $display);
$self = htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'utf-8');
$first = 'first';
$prev = 'prev';
$next = 'next';
$last = 'last';
echo '<p>';
echo ($page > 1) ? "$first | $prev |" : 'first | prev |';
echo '(page ' . $page . ' of ' . $pages . ')';
echo ($page < $pages) ? "| $next | $last" : '| next | last';
echo '</p>';
}
// output example
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$display = 100;
$start = $display * $page - $display;
$total = countRecords($start, $display);
paginate($page, $display, $total);
I agree with #mister martin, but if you use this code fundamentally, try to change
$start = ($page < $adjacents ? 1 : $page - $adjacents);
To:
$start = ($page < $adjacents ? $page : $page - $adjacents);
Edited: maybe problem in undefined $left_rec, check demo. You can change $s like $_GET['page'] and as I see all works correctly.
Demo.
<?php
$rec_limit = 100;
$targetpage = "dispatch.php";
$total_records = countRecords();
// Nav part
$page = intval($_GET['page'])? $_GET['page']: 1;
$offset = $rec_limit * ($page - 1);
$total_pages = ceil($total_records / $rec_limit); // $rec_limit is 100
$adjacents = 2;
$previousPage = $page - 1;
$nextPage = $page + 1;
$start = ($page < $adjacents ? $page : $page - $adjacents); // <-- I think the issue is this line
$beginning = 1;
$end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);
$uri = $_GET;
unset($uri['page']);
$querystring = http_build_query($uri);
echo '<div class="row-fluid"><div class="span2"><ul class="pager"><li>First</li>';
if($left_rec < $rec_limit && ($page > 1))
{
echo "<li>Previous</li>";
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
} else if ($page > 1)
{
echo "<li>Previous</li> ";
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
} else {
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
}
echo "<li>Last</li>";
echo '</ul></div></div>';
?>

Href format using master page php

I saw a similar post on stackoverflow but it failed to anwser my question.
I am using pagination to display results and the link is coming up broken when I add the master links. Most likely as I am incorrectly using the correct format.
Have tried a few formats to try to get it working based on other posts
<a href='../admin/admin.master.php?page=list_products.php&page=' .$j. ' id='page_a_link'>Next</a></span>
or
../admin/admin.master.php?page=list_products.php&page=$j
but neither work.
PHP PAGINATION SHOWING PAGE NUMBERS
<?php
if(isset($page))
{
$result = mysql_query("SELECT COUNT(*) As Total FROM products");
$rows = mysql_num_rows($result);
if($rows)
{
$rs = mysql_fetch_array($result);
$total = $rs["Total"];
}
$totalPages = ceil($total / $perpage);
if($page <=1 )
{
echo "<span id='page_links' style='font-weight:bold;'>Pre</span>";
}
else
{
$j = $page - 1;
echo "<span><a id='page_a_link' href='../admin/admin.master.php?page=list_products.php&page=$j'>< Pre</a></span>";
}
for($i=1; $i <= $totalPages; $i++)
{
if($i<>$page)
{
echo "<span><a href='../admin/admin.master.php?page=list_products.php&page=' .$i. ' id='page_a_link'>$i</a></span>";
}
else
{
echo "<span id='page_links' style='font-weight:bold;'>$i</span>";
}
}
if($page == $totalPages )
{
echo "<span id='page_links' style='font-weight:bold;'>Next ></span>";
}
else
{
$j = $page + 1;
echo "<span><a href='../admin/admin.master.php?page=list_products.php?page=' .$j. ' id='page_a_link'>Next</a></span>";
}
}
?>
You are mixing double quotes with single quotes, that's why it breaks. Try this:
<?php
if(isset($page))
{
$result = mysql_query("SELECT COUNT(*) As Total FROM products");
$rows = mysql_num_rows($result);
if($rows)
{
$rs = mysql_fetch_array($result);
$total = $rs["Total"];
}
$totalPages = ceil($total / $perpage);
if($page <=1 )
{
echo '<span id="page_links" style="font-weight:bold;">Pre</span>';
}
else
{
$j = $page - 1;
echo '<span><a id="page_a_link" href="../admin/admin.master.php?page=list_products.php&page=' . $j . '">< Pre</a></span>';
}
for($i=1; $i <= $totalPages; $i++)
{
if($i<>$page)
{
echo '<span>' . $i . '</span>';
}
else
{
echo '<span id="page_links" style="font-weight:bold;">' . $i . '</span>';
}
}
if($page == $totalPages )
{
echo '<span id="page_links" style="font-weight:bold;">Next ></span>';
}
else
{
$j = $page + 1;
echo '<span>Next</span>';
}
}
?>
Also, the HTML code you generate isn't valid, as you assign the same ID to multiple elements. Consider changing id="page_links" and id="page_a_link" to class="page_link" and class="page_a_link" respectively, then change #page_links and #page_a_link in your CSS to .page_links and .page_a_link
You are assigning two values to page in the URL '../admin/admin.master.php?**page**=list_products.php&**page**=' .$j. ' try replacing page = $j with page_num = $j or something of your choice and replace it in the and in the code.

PHP & MySQL pagination display problem

I asked a similar question like this yesterday but after waiting for ever I figured out part of the problem but now I'm stuck again I'm trying to display ... when the search results are to long because my pagination links will keep on displaying and will not stop until every link is displayed on the page.
For example I'm trying to achieve the following in the example below. Can some one help me fix my code so I can update my site. Thanks
This is what I want to be able to do.
First Previous 1 2 ... 5 6 7 8 9 10 11 12 13 ... 199 200 Next Last
Here is my pagination code that displays the links.
$display = 20;
if (isset($_GET['p']) && is_numeric($_GET['p'])) {
$pages = $_GET['p'];
} else {
$q = "SELECT COUNT(id) FROM comments WHERE user_id=3";
$r = mysqli_query ($mysqli, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($mysqli));
$row = mysqli_fetch_array ($r, MYSQLI_NUM);
$records = $row[0];
if ($records > $display) {
$pages = ceil ($records/$display);
} else {
$pages = 1;
}
}
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
//content goes here
if ($pages > 1) {
echo '<br /><p>';
$current_page = ($start/$display) + 1;
if ($current_page != 1) {
echo 'First';
}
if ($current_page != 1) {
echo 'Previous ';
}
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
echo '' . $i . ' ';
} else {
echo '<span>' . $i . '</span> ';
}
}
if ($current_page != $pages) {
echo 'Next';
}
if ($current_page != $pages) {
echo 'Last';
}
echo '</p>';
}
Instead of the loop just use something like this:
if($current_page > 8 && $pages > 11) {
echo '1 ';
echo '2 ';
echo '...';
}
for ($i = max(1, $current_page - 4); $i < min($current_page + 4, $pages); $i ++) {
echo '' . $i . ' ';
}
if ($current_page < $pages - 8 && $pages > 11) {
echo '...';
echo '' . ($pages - 1) . ' ';
echo '' . $pages . ' ';
}

PHP question pagination

How would I do so that the page I'm on won't be clickable?
$page = (isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1);
$limit = ($page - 1) * 15;
$sql = mysql_query("SELECT * FROM log LIMIT $limit, 15");
$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM log"),0);
$totalpages = ceil($totalres / 15);
for ($i = 1; $i <= $totalpages; $i++) {
$pagination .= "$i ";
}
<p><?php echo $pagination ?></p>
if ($i == $page)
$pagination .= "$i ";
else
$pagination .= "$i ";
Just need to modify the for loop to check what the current page is.
for ($i = 1; $i <= $totalpages; $i++)
{
if ($i != $page)
$pagination .= "$i ";
else
$pagination .= " " . $i . " ";
}
With an if inside the for,
if ( $i != $page) {
//Your code.
}
else
{
//Same page.
$pagination .= " " . $page . " "
}
#RedBlueThing Has soon has I saw your post I got it to work, although I got the idea from a different stackoverflow question with a little modifying of the code.
<?php
echo "<section>";
$link = mysqli_connect("localhost", "root", "root", "user_db");
$page = (isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1);
$offset = 4;
$x = 5;
$limit = ($page - 1) * $offset;
$sql = mysqli_query($link,"SELECT * FROM cfp_blogs;");
$totalres = mysqli_num_rows($sql);
$totalpages = ceil($totalres / $offset);
$stmt = mysqli_query($link, "SELECT * FROM blogs LIMIT $limit, $offset;");
if($stmt->num_rows > 0){
while($obj = mysqli_fetch_object($stmt)) {
echo "<h1>$obj->title</h1>";
echo "<p>$obj->content</p>";
$stmt2 = $link->query("SELECT COUNT(id) AS Tot FROM blogs");
if($stmt2->num_rows > 0){
while ($obj2 = $stmt2->fetch_object()){
echo "<p>($obj2->Tot)</p>";
}
}
else
echo "<p>(0)</p>";
$stmt2->close();
}
}
else {
echo "<section class='error'><h1>There was a problem with the database.</h1></section>";
}
mysqli_free_result($stmt);
for ($i = $page - $x; $i < $page; $i++)
{
if ($i >= 1) {
$pagination .= "$i ";
}
}
$pagination .= " " . $i . " ";
for ($i = $page + 1; $i <= $page + $x; $i++)
{
if ($i <= $totalpages) {
$pagination .= "$i ";
}
}
/*for ($i = 1; $i <= $totalpages; $i++)
{
if ($i != $page){
$pagination .= "$i ";
}
else{
$pagination .= " " . $i . " ";
}
}*/
mysqli_close($link);
echo $pagination;
echo "</section>";
?>
The only thing I couldn't get was having a "next" or "previous" button. If anyone is up to the task of adding on those buttons, give it a shot. Other wise..... Enjoy..... :) This is my way of giving back to the stackoverflow community for helping me.

Categories