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 . ' ';
}
Related
I am having difficulties with my MYSQL / PHP dashboard. - Currently i am having 50 pages, but currently they are all showing on the same page.
http://imgur.com/wDfTWUa - as you can see in the attached file. - I only want 10 pages to be shown, and be able to click through the rest of the pages without seeing 4 rows of pages.
Exsampel <- 2 3 4 5 6 7 8 9 10 -> when you are on ?page=1, if you are on page ?page=10 <- 11 12 13 14 15 16 17 18 19->
Hope you can help me.
Code:
<?php
include 'config.php';
$sidenr = $_GET['page'];
$sidenr2 = ($sidenr -1) * 10;
echo $sidenr2;
echo "<br><br>";
$query100 = mysqli_query($conn, "SELECT * FROM `test` LIMIT $sidenr2,10") or die(mysqli_error($conn));
while($row = mysqli_fetch_array($query100))
{
echo $row['id']."<br>";
}
$result = mysqli_query($conn, "SELECT * FROM test");
$num_rows = mysqli_num_rows($result);
$sideantal = $num_rows / 10;
echo "Der skal være antal sider: ". $sideantal;
echo "<br><br>antal rækker ". $num_rows . "<br><br>";
?>
<br><br>
<?php
for ($number = 1; $number <= $sideantal; $number++) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
?>
function getPageRange($current, $max, $total_pages = 10) {
$desired_pages = $max < $total_pages ? $max : $total_pages;
$middle = ceil($desired_pages/2);
if ($current <= $middle){
return [1, $desired_pages];
}
if ($current > $middle && $current <= ($max - $middle)) {
return [
$current - $middle,
$current + $middle
];
}
if ($current <= $max ) {
return [
$current - ($desired_pages - 1),
$max
];
}
}
list($min,$max) = getPageRange($sidenr, $sideantal);
foreach (range($min, $max) as $number) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
try to change this:
for ($number = 1; $number <= $sideantal; $number++) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
</li>";
}
to this:
for ($number = 1; $number <= $sideantal; $number++) {
if (($number > $_GET['page']) && ($number <= $_GET['page'] + 10)) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
</li>";
}
}
You can try the following code:
for ($number = 1; $number <= $sideantal; $number++) {
/** If the loop count is greater than the current page but less than current page plus 10 */
if ( ($number > $_GET['page'] && ($number < ($_GET['page'] + 10)))) $is_valid = true;
/** If the loop count is less than the current page but greater than current page -10 and the current page is the last page */
if ($number < $_GET['page'] && $_GET['page'] == $sideantal && $number > ($_GET['page'] - 10)) $is_valid = true;
else $is_valid = false;
if ($is_valid) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
}
I have page with a URL like this:
http://***.com/profile/username#profile_favs
With my pagination it looks like this:
http://***.com/profile/username?s=0&p=1#profile_favs
The last example doesn't work.
Basically my pagination-function looks like this:
function Pagination($pages, $start, $display, $link_url="", $anchor="") {
echo '<div id="pagination">';
$current_page = ($start / $display) + 1;
$paginator_num = 5;
$pages_display = 10;
if ($current_page > $pages - $paginator_num) {
$paginator_num = $pages_display - ($pages - $current_page);
} elseif ($current_page < $paginator_num + 1) {
$paginator_num = $pages_display - $current_page;
} else {
$paginator_num = 5;
}
$min = max($current_page - $paginator_num, 1);
$max = min($current_page + $paginator_num, $pages);
for ($i = $min; $i <= $max; $i++) {
if ($i != $current_page) {
echo '<div class="pagination_link">';
echo '' . $i . '';
echo '</div>';
} else {
echo '<div id="pagination_active" class="pagination_link">';
echo $i . ' ';
echo '</div>';
}
}
echo '</div>';
}
And this is the part that calculates the pages (this part gets included right before my pagination-function):
<?php
$display = $display_num;
if (isset($_GET['p']) && is_numeric ($_GET['p'])) {
$pages = $_GET['p'];
} else {
$total_results = $qr_num;
if ($total_results > $display) {
$pages = ceil ($total_results / $display);
} else {
$pages = 1;
}
}
if (isset($_GET['s']) && is_numeric ($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
?>
Now my question is: Is there any workaround possible to get that pagination working with a URL mentioned above:
http://***.com/profile/username?s=0&p=1#profile_favs
Actually I'd prefer a solution without GET-parameters.
You could use an AJAX call with the XMPHttpRequest Object to fetch it without the get parameters.
Another thing you could do (if you are using Apache) is to look at mod_rewrite for urls.
Hope that helps :)
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.
I have two tiny little problems;
1 . I want to add a previous / next button into this code
2 . I want it to only show like max 10 links between previous and next. So if i have 50 numbers/links it will only show 10 of them and not 50 links on the page.
I have searched on the clo
The code works, only need that two options in it.
Can someone help me out? Thank you !
<?php
include 'includes/connection.php';
$per_page = 8;
$pages_query = mysql_query("SELECT COUNT(`id`) FROM `products`");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query = mysql_query("SELECT `name` FROM `products` LIMIT $start, $per_page");
while ($query_row = mysql_fetch_assoc($query)) {
echo '<p>', $query_row['name'] ,'</p>';
}
if ($pages >= 1 && $page <= $pages) {
for ($x=1; $x<=$pages; $x++) {
//echo $x, ' ';
echo ($x == $page) ? '<strong>'.$x.'</strong> ' : ''.$x.' ';
}
}else{
header("location:index.php?page=1");
}
?>
First of all, you should, just for good practice, put an "exit;" after the header() call at the end. It doesn't make a difference in this particular script, but keep in mind that any code following a header("Location: ...") call WILL be executed before redirection.
Now to your question, try this (UPDATE: This code has been tested and works.)
<?php
include 'includes/connection.php';
$per_page = 8;
$pages_query = mysql_query("SELECT COUNT(`id`) FROM `products`");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query = mysql_query("SELECT `name` FROM `products` LIMIT $start, $per_page");
while ($query_row = mysql_fetch_assoc($query))
{
echo '<p>' . $query_row['name'] . '</p>';
}
// If the requested page is less than 1 or more than the total number of pages
// redirect to the first page
if($pages < 1 || $page > $pages)
{
header('Location: ?page=1');
// end execution of the rest of this script
// it will restart execution after redirection
exit;
}
// If more than one page, show pagination links
if($pages > 1)
{
$html = array();
$html[] = '<strong>';
// if you're on a page greater than 1, show a previous link
$html[] = (($page > 1) ? 'Previous ' : '');
// First page link
$pageFirst = '1';
$html[] = (($page == 1) ? "</strong>{$pageFirst}<strong>" : $pageFirst);
if ($pages > 6)
{
$start_cnt = min(max(1, $page - (6 - 1)), $pages - 6);
$end_cnt = max(min($pages, $page + 4), 8);
$html[] = ($start_cnt > 1) ? '...' : ' ';
for ($i = $start_cnt + 1; $i < $end_cnt; $i++)
{
$html[] = ($i == $page) ? '</strong>' . $i . '<strong>' : '' . $i . '';
if ($i < $end_cnt - 1)
{
$html[] = ' ';
}
}
$html []= ($end_cnt < $pages) ? '...' : ' ';
}
else
{
$html[] = ' ';
for ($i = 2; $i < $pages; $i++)
{
$html[] = ($i == $page) ? '</strong>' . $i . '<strong>' : '' . $i . '';
if ($i < $pages)
{
$html[] = ' ';
}
}
}
// last page link
$pageLast = '' . $pages . '';
$html[] = (($page == $pages) ? "</strong>{$pageLast}<strong>" : $pageLast);
// Show next page link if you're on a page less than the total number of pages
$html[] = ($page < $pages) ? ' Next' : '';
// If you're not on the last page, show a next link
$html[] = '</strong>';
}
else
{
// show page number 1, no link.
$html[] = '<strong>1</strong>';
}
echo implode('', $html);
Also note that the final ?> is not required in PHP files that do not have HTML code following the PHP code, so I left it off.
Buddy refer this URL
http://www.codediesel.com/php/simple-pagination-in-php/
OR
http://www.phpfreaks.com/tutorial/basic-pagination
Surely will help you.
Thanks
I'm working on a pagination algorithm in PHP. I can guess that it needs room for improvement, so I'd like some thoughts on how to improve it, be it cleaning up the code itself, from a UI/UX standpoint, or anything else you can think of.
The algorithm should output pagination that looks like this:
1 2 3 ... 6 7 8 ... 97 98 99
or this:
1 2 3 4 5 ... 6 7 8 9 10
or this:
1 2 3
Here's my code:
<?php
if(!is_null($_GET['page'])) {
$currentPage = $_GET['page'];
} else {
$currentPage = 1;
}
if($pages != null) {
echo 'Page: ';
}
// Less than 10 pages
if($pages <= 10) {
for($page = 1; $page <= $pages; $page++) {
echo '' . $page . ' ';
}
// Greater than 10 pages, and we're somewhere in the middle of them
} elseif(($pages > 10) && ($currentPage > 4) && ($currentPage < $pages - 3)) {
for($page = 1; $page <= 3; $page++) {
echo '' . $page . ' ';
}
echo '... ';
for($page = $currentPage - 1; $page <= $currentPage + 1; $page++) {
echo '' . $page . ' ';
}
echo '... ';
for($page = $pages - 2; $page <= $pages; $page++) {
echo '' . $page . ' ';
}
// Greater than 10 pages, and we're towards the end of the pages
} else {
for($page = 1; $page <= 5; $page++) {
echo '' . $page . ' ';
}
echo '... ';
for($page = $pages - 5; $page <= $pages; $page++) {
echo '' . $page . ' ';
}
}
I'm not sure if my code is any better than yours, but here is how I solved a similar problem.
It takes a parameter for the amount of pages to generate and creates a div with the class pages containing anchors, the current page has a class of current. This solution seems a little cleaner because there is less repetition.
function generate_pages($total,$current)
{ //Will generate pages and link to ?page=x when passed total pages to output
if($total > 1)
{
$total=intval($total);
$output='<div class="pages">';
$current_page= (false == isset($current)) ? 1 : $current;
for($page=1;$page<$total+1;$page++)
{
$lower=$current_page-3;
$upper=$current_page+3;
$special = ($page==$current_page) ? " class=\"current\"" : "";
if(($page > $lower && $page < $upper) || $page < 2 || $page > ($total-1))
{
if($last_done_page+1 != $page) $output.= '... ';
$output.='<a'.$special.' href="?page='.$page.'">'.$page.'</a>';
$last_done_page=$page;
}
}
$output.='</div>';
return $output;
}
}