paging query problem - php

here is my paging code:
function getPagingQuery($sql, $itemPerPage = 10)
{
if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
$page = (int)$_GET['page'];
} else {
$page = 1;
}
// start fetching from this row number
$offset = ($page - 1) * $itemPerPage;
return $sql . " LIMIT $offset, $itemPerPage";
}
function getPagingLink($sql, $itemPerPage = 10, $strGet ="")
{
$result = dbQuery($sql);
$pagingLink = '';
$totalResults = dbNumRows($result);
$totalPages = ceil($totalResults / $itemPerPage);
// how many link pages to show
$numLinks = 10;
// create the paging links only if we have more than one page of results
if ($totalPages > 1) {
$self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;
if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
$pageNumber = (int)$_GET['page'];
} else {
$pageNumber = 1;
}
// print 'previous' link only if we're not
// on page one
if ($pageNumber > 1) {
$page = $pageNumber - 1;
if ($page > 1) {
$prev = " [Prev] ";
} else {
$prev = " [Prev] ";
}
$first = " [First] ";
} else {
$prev = ''; // we're on page one, don't show 'previous' link
$first = ''; // nor 'first page' link
}
// print 'next' link only if we're not
// on the last page
if ($pageNumber < $totalPages) {
$page = $pageNumber + 1;
$next = " [Next] ";
$last = " [Last] ";
} else {
$next = ''; // we're on the last page, don't show 'next' link
$last = ''; // nor 'last page' link
}
$start = $pageNumber - ($pageNumber % $numLinks) + 1;
$end = $start + $numLinks - 1;
$end = min($totalPages, $end);
$pagingLink = array();
for($page = $start; $page <= $end; $page++) {
if ($page == $pageNumber) {
$pagingLink[] = " $page "; // no need to create a link to current page
} else {
if ($page == 1) {
$pagingLink[] = " $page ";
} else {
$pagingLink[] = " $page ";
}
}
}
$pagingLink = implode(' | ', $pagingLink);
// return the page navigation link
$pagingLink = $first . $prev . $pagingLink . $next . $last;
}
return $pagingLink;
}
im calling it like:
$sql = "something";
$result = mysql_query(getPagingQuery($sql, $rowsPerPage));
$pagingLink = getPagingLink($sql, $rowsPerPage, $url);
now if my url is like
http://xyz/abc/list.php its working fine.
but when my url is like
http://xyz/abc/list.php?action=def
after i click on page 2 the url changes like http://xyz/abc/list.php?page2&
'action=def' part is gone so the result changes.
if i use to pass the value in $strGet as $_SERVER['QUERY_STRING']
the 1st time it is ok like http://xyz/abc/list.php?page2&action=def
but from the 2nd time onwards it gives like http://xyz/abc/list.php?page3&page2&action=def
so not getting the desired result.
whereas i want it to be like http://xyz/abc/list.php?page3&action=def
plz help.. thanxx in advance

So I consider you use $_GET['page'] ^^
$_GET['page']=$page;
$url = 'http://xyz/abc/list.php?';
foreach($_GET as $key=>$param) {
$url.=$key.'='.$param.'&';
}

use http_build_query() instead of $_SERVER['QUERY_STRING']

What i would do is before setting $page = url code. i would first echo $page before and after setting it. so that i can see exactly what the values are. And i would print echo statements before and after everytime i set $page with the url to make sure it is correct. and if in any place you can see that its not the desired output because you can see that from the echo statements then you can make the right changes to make sure the $page variable is set properly.
what i would do then to set it properly is clear the $page variable and make sure that the $page variable is then set freshly with the url.
also when setting the url make sure that the $strGet variable is also echoed out first to make sure that it is the right value that you want to set. and then set the url to the $page variable.
By doing this simple debugging you are making sure all the values are correct first and you know it is before setting them.
give it a go
let me know what happens
PK

Related

Next/Previous Page bug

I need some help, I want to make an next / previous page but I have some problems...
This is the PHP Code, how I try to make it. The problem is that it doesn't show 3 data on the first page and on the next page it puts the same data again an not the other one.
Code:
$statement = $connect->prepare("SELECT COUNT(*) AS anzahl FROM `accounts`");
$statement->execute();
$row = $statement->fetch();
$max_data = $row['anzahl'];
$page = 1;
if(isset($_GET['page'])) {
$page = intval($_GET['page']);
}
$max_info = 3;
$start = ($page - 1) * $max_info;
$number_page = ceil($max_data / floatval($max_info));
for($a = 1; $a <= $anzahl_seiten; $a++) {
if($page == $a){
echo " <b>$a</b> ";
}
else {
echo " <a href='acp.php?page=list_all_player&seite=$a'>$a</a> ";
}
}

PHP pagination Adjacents

I was able to create a working pagination system for my application. But the problem I am having is when a user does a search, it can/will display over 100 pages in the pagination.
I am trying to figure out how to show only like 5 on each side of the current page. I would like to create a FIRST page button, and a LAST page button, but I'll deal with that later.
So here is the first part of code that counts the records in the database table:
<?php
function countRecords()
{
// The application takes a lot of user input, which then builds a query here.
// The user input goes into a session variable called $_SESSION['where']
// I'll start with the actual query code
$sql = "SELECT COUNT(DISTINCT CONTAINER_NUMBER) AS TOTAL
FROM 'contTable'" . " WHERE (" . $_SESSION['where'] . ");";
$sqlres = #mysql_query($sql) or die();
$row = mysql_fetch_row($sqlres);
$return $row[0];
}
So code above gets the count from the table depending on the search criteria.
Here is the next piece of code that prints the grid. I'll keep it as short as possible:
function displayrecords()
{
$rec_limit = 100;
$targetpage = "containers.php";
if(isset($_GET['page']))
{
$page = $_GET['page'];
$offset = $rec_limit * ($page - 1);
}
else
{
$page = 1;
$offset = $rec_limit * ($page - 1);
}
$left_rec = countRecords() - ($page * $rec_limit);
$select = "";
$_SESSION['where'] = "";
// user input variables are here that build a variable called $_SESSION['where']
// I'll skip the code down to the query
if ($_SESSION['where'] != "") $select = "SELECT * FROM 'contTable'" . " WHERE
(" . $_SESSION['where'] . ") GROUP BY container, bol LIMIT " . $offset . ",
" . $rec_limit . ";";
}
Please excuse any typos or missing brackets and whatnot. Just know the above code works.
Now here is the code for the rest of the pagination:
$total_records = countRecords();
$total_pages = ceil($total_records / $rec_limit);
$adjacents = 5; // I just added this variable. I don't know what to do with it
$previousPage = $page - 1;
$nextPage = $page + 1;
$querystring = "";
foreach ($_GET as $key => $value)
{
if ($key != "page") $querystring .= "$key=$value&";
}
echo '<ul style="border: 0px solid red; margin: 3px;" class="pager">';
if ($left_rec < $rec_limit)
{
$last = $page - 2;
echo #"<li><a href=\"$targetpage?page=$previousPage&$querystring\">
Previous</a></li>";
for($i = 1; $i <= $total_pages; $i++)
{
echo #"<li " . ((($page+1)==$i)? "class=\"active\"" : "") . ">
$i</li>";
}
}
else if ($page == 0)
{
for($i = 1; $i <= $total_pages; $i++)
{
echo #"<li " . ((($page+1)==$i)? "class=\"active\"" : "") . ">
$i</li>";
}
echo #"<li>Next</li>";
}
else if ($page > 0)
{
$last = $page - 2;
echo #"<li><a href=\"$targetpage?page=$previousPage&$querystring\">
Previous</a></li> ";
for($i = 1; $i <= $total_pages; $i++)
{
echo #"<li " . ((($page+1)==$i)? "class=\"active\"" : "") . ">
$i</li>";
}
echo #"<li>Next</li>";
}
echo '</ul>';
}
So, with all of the code above, I can display a grid, and display the pages at the bottom of the application. But I don't want to show all 100 pages, only 5 on each side of the current page. I know I need to utilize the variable called $adjacents and plug it in to the paging portion of the code. But I'm not exactly sure how to do it.
I hope I am being clear on my request.
Please help.
Instead of looping through all of the pages:
for($i = 1; $i <= $total_pages; $i++)
Try doing something like this:
$start = ($page < $adjacents ? 1 : $page - $adjacents);
$end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);
for($i= $start; $i <= $end; $i++)
//Here you can loop through the numbers within adjacents of the current page

PHP Pagination of MySQL Query not incrementing/decrementing properly

I'm having trouble with the follow PHP which paginates the results of a MySQL query.
When I go to webpagename.php with the first page of the results and click Previous, the browser changes to webpagename.php?page=-1 and shows the first page of results again. If I click Previous again, it changes to webpagename.php?page=-2 and shows Page 1 of the results again, etc.
When I go to webpagename.php with the first page of the results and click Next, the browser changes to webpagename.php?page=1 and shows the first page of results again. I then have to hit Next a second time to move to Page 2.
When I go to the last page of the results - Page 8 - and click Next, the browser changes to webpagename.php?page=9 and shows Page 1 of the results. If I click Next again, it shows webpagename.php?page=10 and shows Page 1 of the results again, etc.
Expected Results:
When on Page 1 and a user hits Previous, I would like the code to do nothing/not decrement. When on Page 8 - the last page of results, I would like the code to do nothing/not increment. Of course, I would also expect that if you hit Next from Page 1 that it doesn't display Page 1 a second time but rather goes to Page 2.
Your exact changes to this code to make it work properly are very much appreciated. Thank you for time.
<?php
mysql_connect("localhost","username","password") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
// number of results to show per page
$per_page = 10;
// figure out the total pages in the database
$result = mysql_query("SELECT * FROM uc_users LEFT JOIN ent_dancers ON uc_users.id = ent_dancers.id WHERE ent_dancers.DancerYesNo = '1' AND ent_dancers.DancerEnabledYesNo = '1' ORDER BY uc_users.display_name ASC");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: webpagename.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
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 = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
// display data in table
echo "<div class='dancerbio'>";
echo "<div class='uts-1'>";
// loop through results of database query, displaying them in the table
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; }
// echo out the contents of each row into a table
$rowid = mysql_result($result, $i, 'id');
echo "<div class='uts-1-1'><a class='bodytxt5' href='webpagename-details.php?userid=$rowid'>" . mysql_result($result, $i, 'display_name') . "</a></div>";
}
// close table>
echo "<div class='ugen-1'></div>";
echo "</div>";
$prev = $_GET['page'] - 1;
echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";
echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a class='bodytxt5' href='webpagename.php?page=$i'>$i</a> ";
}
$next = $_GET['page'] + 1;
echo " <a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a> ";
echo "</div>";
// pagination
?>
replace this:
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
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 = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
by this:
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
elseif ($show_page > $total_pages)
{
$show_page=$total_pages;
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else {
$show_page=1;
$start = 0;
$end = $per_page;
}
}
else {
$show_page=1;
$start = 0;
$end = $per_page;
}
then :
$prev=$show_page-1;
$next=$show_page+1;
if($show_page>1){//this way previsous won't appear if you are at page 1 already
//show previous div
}
if($show_page<$total_pages){ //this way next won't appear unless you are not at the last page
//show next div
}
Make a variable $page set it equal to $_GET['page'].
if(isset($_GET['page'])){
$page = $_GET['page'];
}
else{
$page = 1;
}
you need to put a condition before echoing previous link to check whether $_GET['page'] is set or not and is greater than 1.
Like this:
if($page!=($start+1)){
$prev = $page - 1;
echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";
echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a> ";
}
Add another condition for next
if($page!=$total_pages)
{
$next = $page+1
echo " <a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a> ";
echo "</div>";
}
I hope your issue is solved.
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
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 = 0;
$end = $per_page;
$show_page=1
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
$show_page=1;
}
// display pagination
// display data in table
echo "<div class='dancerbio'>";
echo "<div class='uts-1'>";
// loop through results of database query, displaying them in the table
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; }
// echo out the contents of each row into a table
$rowid = mysql_result($result, $i, 'id');
echo "<div class='uts-1-1'><a class='bodytxt5' href='webpagename-details.php?userid=$rowid'>" . mysql_result($result, $i, 'display_name') . "</a></div>";
}
// close table>
echo "<div class='ugen-1'></div>";
echo "</div>";
if($show_page!=($start+1)){
$prev = $page - 1;
echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";
echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a> ";
}
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a class='bodytxt5' href='webpagename.php?page=$i'>$i</a> ";
}
if($show_page!=$total_pages)
{
$next = $page+1
echo " <a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a> ";
echo "</div>";
}
// pagination
?>

issue in selecting value in pagination

I think i am very near to my correct answer. I found one code on this forum like i wrote below.but it didn't work for me.Whats wrong with this code???
<?php
include("config.php");
$start = 0;
$per_page = 10;
$targetpage = "manual.php?id=$id"; // This is my target page
if(!isset($_GET['page'])){
$page = 1;
} else{
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;
........................
.............................
.................?>
The answer which i saw contains id=42 in $targetpage = "manual.php?id=$id";But i don't understand why he used 42 there???
Then linking target page in next previous code as follows
<?php
if($page > 1){
$prev = $page - 1;
$prev = " <a href=\'$targetpage&page=$prev'>prev</a> ";
} else {
$prev = "";
}
if($page < $num_pages){
$next = $page + 1;
$next = " <a href=\'$targetpage&page=$next'>next</a> ";
}
else
{
$next = "";
}
echo $prev;
echo $next;
?>
But it gives me error like \'manual.php is not found..But i have this file.
just a guess, try to use:
$targetpage = "./manual.php?id=$id";
if it doesn't work then it means you are specifying the wrong URL to your manual.php page, try to find the correct URL
and id=42 it's an application specific parameter so it's related to that app so it's okay if you don't use it in your app.

How can I "limit" the page numbers of a forum/blog in PHP

I'm trying to fix something with which I'm not familiar with and don't know how to proceed. The forum on which I'm working is suppose to show under "TOP 50" only the most commented topics (2 pages by 25 topics) but it shows all topics (by 25) without any limitation of the pages. I need only the first 2 pages - but don't know how to get rid of the others?
I'm even not sure that the below code is the responsible one but please have a look and give me a hint if you see any solution.
This is the code:
{
public function __construct()
{
parent::__construct();
}
public function get_forum()
{
if ($_GET['l'] && ($_GET['l'] == 'leng' || $_GET['l'] == 'lrus' || $_GET['l'] == 'lde' || $_GET['l'] == 'ltr'))
$l = substr($_GET['l'], 1);
else
$l = 'eng';
(isset($_GET['num'])) ? $page = intval($_GET['num']) : $page = 1;
$id_user = intval($_SESSION['user_id']);
$lang = language::getLang();
if ($_GET['el']) {
switch ($_GET['el']) {
case 'categories':
return $this->getCategories($l);
break;
case 'top':
$top_lang = $_GET['ln'];
$c = $this->db->selectAssoc($this->db->Select('*', 'forum_categories ,forum_thems', "`forum_categories`.`lang` = '" . $l
. "' AND `forum_thems`.`id_categories` = `forum_categories`.`id`"));
$total_pages = count($c) / 25;
$p = "<div class=\"pageCounter_box\">Pages:";
if (empty($_GET['p'])) {
$_GET['p'] = 1;
}
for($i=1; $i<$total_pages+1; $i++){
if ($i == $_GET['p']) {
$class = 'class="active_page"';
}
$p .= "<a href=\"$top_lang/smoke/{$_GET['l']}/top?p=$i\" $class>$i</a>";
}
$p .= "</div>";
return $this->getTop($l) . $p;
break;
I think you could do a check in there of If ($total_pages > 2) { $total_pages = 2};
$c = $this->db->selectAssoc(
$this->db->Select('*', 'forum_categories ,forum_thems', "`forum_categories`.
`lang` = '" . $l. "' AND `forum_thems`.
`id_categories` = `forum_categories`.`id`"));
$total_pages = count($c) / 25;
if ($total_pages >2) { //limit to two pages
$total_pages = 2;
}
$p = "<div class=\"pageCounter_box\">Pages:";
if (empty($_GET['p'])) {
$_GET['p'] = 1;
}
"thanks a lot - great help! Do you further see why both pages might show active (page counter shows both active) when showing page 1? Page 2 is fine, there only Page 2 shows active..."
The $class variable is staying set, you need to have an else that sets the class to an empty string
for($i=1; $i<$total_pages+1; $i++){
if ($i == $_GET['p']) {
$class = 'class="active_page"';
} else {
$class = '';
}
$p .= "<a href=\"$top_lang/smoke/{$_GET['l']}/top?p=$i\" $class>$i</a>";
}

Categories