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.
Related
I have OpenCart 2.1 and the website has such pagination (see screenshot)
default pagination
Here is the question: How to make like a product diapason pagination in OpenCart?
When I say the diapason pagination, I mean this (see screenshot please)
diapason pagination screenshot
Here is some code that is responsible for the formation of pagination.
<?php
class Pagination {
public $total = 0;
public $page = 1;
public $limit = 20;
public $num_links = 8;
public $url = '';
public $text_first = '|<';
public $text_last = '>|';
public $text_next = '>';
public $text_prev = '<';
public function render() {
$total = $this->total;
if ($this->page < 1) {
$page = 1;
} else {
$page = $this->page;
}
if (!(int)$this->limit) {
$limit = 10;
} else {
$limit = $this->limit;
}
$num_links = $this->num_links;
$num_pages = ceil($total / $limit);
$this->url = str_replace('%7Bpage%7D', '{page}', $this->url);
$output = '<ul class="pagination">';
if ($page > 1) {
$output .= '<li>' . $this->text_first . '</li>';
$output .= '<li>' . $this->text_prev . '</li>';
}
if ($num_pages > 1) {
if ($num_pages <= $num_links) {
$start = 1;
$end = $num_pages;
} else {
$start = $page - floor($num_links / 2);
$end = $page + floor($num_links / 2);
if ($start < 1) {
$end += abs($start) + 1;
$start = 1;
}
if ($end > $num_pages) {
$start -= ($end - $num_pages);
$end = $num_pages;
}
}
for ($i = $start; $i <= $end; $i++) {
if ($page == $i) {
$output .= '<li class="active"><span>' . $i . '</span></li>';
} else {
$output .= '<li>' . $i . '</li>';
}
}
}
if ($page < $num_pages) {
$output .= '<li>' . $this->text_next . '</li>';
$output .= '<li>' . $this->text_last . '</li>';
}
$output .= '</ul>';
if ($num_pages > 1) {
return $output;
} else {
return '';
}
}
}
I have this code:
<?php
function paginate($reload, $page, $tpages) {
$adjacents = 2;
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "";
if ($page == 1) {
$out.= "<span style=\"display:none;\">" . $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>" . $i . "</li>\n";
} elseif ($i == 1) {
$out.= "<li>" . $i . "\n</li>";
} else {
$out.= "<li>" . $i . "\n</li>";
}
}
if ($page < $tpages) {
$out.= "<li>" . $nextlabel . "\n</li>";
} else {
$out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
}
$out.= "";
return $out;
}
?>
How to when the first page , appears 5 points. For E.g, When I was at page 1 , then I would like this 1 2 3 4 5.. but if I change the "$ adjacents=2;" to 4. Indeed, when the 1 page there is a 1 2 3 4 5.. But when I moved to the page=7 , then , The page displayed 1 2 3 4 5 6 7 8 9 a lot of numbers, or when I moved to page=12. The page displayed 8 9 10 11 12 13 14 15 16.. Whereas , I Want to display 5 digits only.
How so that when the first and second page only, is made to appear 5 digit page ?And when I turn the page , eg page 12 like this. 10 11 12 13 14.
or when in page 1 to be like this 1 2 3 4 5
or when in page 2 to be like this 1 2 3 4 5
or when in page 3 to be like this 1 2 3 4 5.. etc
You can copy paste this script
<?php
function paginate($reload, $page, $tpages) {
$adjacents = 2;
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "";
if ($page == 1) {
$out.= "<span style=\"display:none;\">" . $prevlabel . "</span>\n"; $pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
$adjacents = 4;
$pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out.= "<li>" . $i . "</li>\n";
} elseif ($i == 1) {
$out.= "<li>" . $i . "\n</li>";
} else {
$out.= "<li>" . $i . "\n</li>";
}
}
if ($page < $tpages) {
$out.= "<li>" . $nextlabel . "\n</li>";
} else {
$out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
}
$out.= "";
return $out;
} elseif ($page == 2) {
$out.= "<li>" . $prevlabel . "\n</li>";
$adjacents = 3;
$pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
$pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out.= "<li>" . $i . "</li>\n";
} elseif ($i == 1) {
$out.= "<li>" . $i . "\n</li>";
} else {
$out.= "<li>" . $i . "\n</li>";
}
}
if ($page < $tpages) {
$out.= "<li>" . $nextlabel . "\n</li>";
} else {
$out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
}
$out.= "";
return $out;
} 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>" . $i . "</li>\n";
} elseif ($i == 1) {
$out.= "<li>" . $i . "\n</li>";
} else {
$out.= "<li>" . $i . "\n</li>";
}
}
if ($page < $tpages) {
$out.= "<li>" . $nextlabel . "\n</li>";
} else {
$out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
}
$out.= "";
return $out;
}
}
?>
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>';
?>
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>';
}
?>
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.