PDA

View Full Version : pagination displaying same results for all links


reddyink
12-26-2007, 12:22 AM
I have pagination code working properly for count and no of pages and limit. When I click on 2nd 3rd and 4rth pages same results are displayed. Can anybody please explain what am I missing
I really appreciate any help.
Code starts here.....
$limit = 2 ;
$count = $db->query_first("select count(*) count query");

$count['count'] = vb_number_format($count['count']);
$totalrows = $count['count'];

if(empty($page))
{
$page = 1;
}
$limitvalue = $page * $limit - ($limit);
$query = "My query";
$getusercats = $db->query_read($query);
while($cats = mysql_fetch_array($getcats)){

eval('$cats .= "' . fetch_template('mainforum_navpage') . '";');
}

//Start pagination code here
// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}

if(!isset($_GET['page'])){

$page = 1;

} else {

$page = $_GET['page'];

}
$pagenum = intval($page);
if (!isset($pagenum))
{
$pagenum = 1;
}

// counting the offset
$offset = ($pageNum - 1) * $limit;

$numrows = $count['count'];

// how many pages we have when using paging?
$maxPage = ceil($numrows/$limit);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';

for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";

$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = '&nbsp;'; // we're on page one, don't print previous link
$first = '&nbsp;'; // nor the first page link
}

if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";

$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = '&nbsp;'; // we're on the last page, don't print next link
$last = '&nbsp;'; // nor the last page link
}

MoT3rror
12-26-2007, 05:21 AM
Try to replace your first query
$count = $db->query_first("select count(*) count query");
with
$count = $db->query_first("select count(*) AS 'count' FROM table");

reddyink
12-26-2007, 01:42 PM
Thanks for reply but count is not a problem. It is displaying correct number of pages and when we set limit to 10 Query is displaying all results correctly. If I have limit 2, records are displaying for 1st page and same records for all links. I am just wondering it is not going back to php page for next page results. The url is saying page=2,page=3... but the contents are 1st page results only.
Where am i doing wrong? Can any body please help me?
Thanks

MoT3rror
12-26-2007, 07:22 PM
Is your query that is displaying the data when the user define page contain at the end LIMIT $first_record, $perpage. It should look something like this.

$query = $db->query_read("SELECT * FROM table LIMIT $limitlower, $perpage"); // $limitlower is the first record that is showed.

while($data = $db->fetch_array($query))
{
//Output data here
}

reddyink
12-26-2007, 09:00 PM
Yes, I have Limit $limitvalue limit in my query. The count is displaying correctly and also query because when I set the limit =20, all the results are displaying with just 1 page with no link to page. When I set Limit to 5 count of pages are displaying correct with all links,but the records are first 5 records for all pages when I click on 2nd,3rd,4th. The url link says page=2,3 correctly but results are only first page results.

Can you please tell me where am i doing wron. In my template I use $nav for pagination

Here is my code with query also.

$limit = 2 ;
$count = $db->query_first("select count(*) as 'count' FROM forum AS forum
LEFT JOIN user AS user ON (forum.userid = user.userid) where user.usergroupid='6'");
$count['count'] = vb_number_format($count['count']);
$totalrows = $count['count'];

if(empty($page))
{
$page = 1;
}
$limitvalue = $page * $limit - ($limit);

$query = "select forum.*
,user.* FROM forum AS forum
LEFT JOIN user AS user ON (forum.userid = user.userid) where user.usergroupid='6' order by forum.forumid DESC LIMIT $limitvalue,$limit";
$getusercats = $db->query_read($query);

while($usercats = mysql_fetch_array($getusercats)){
eval('$usercategories .= "' . fetch_template('mainforum_usercats_navpage') . '";');
}
//Start pagination code here
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $limit;
$numrows = $count['count'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$limit);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = '&nbsp;'; // we're on page one, don't print previous link
$first = '&nbsp;'; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = '&nbsp;'; // we're on the last page, don't print next link
$last = '&nbsp;'; // nor the last page link
}

$db->free_result($usercats);
eval('$home[$mods[\'modid\']][\'content\'] = "' . fetch_template('mainforum_usercats') . '";');

I appreciate your reply.

MoT3rror
12-26-2007, 09:44 PM
Replace
if(empty($page))
{
$page = 1;
}
$limitvalue = $page * $limit - ($limit);

with


if (isset($page)
{
$page = 1;
}

//Default lower and upper limit variables
$limitlower = ($page - 1) * $limit + 1;
$limitupper = $page * $limit;

if ($limitupper > $count['count'])
{
//Too many for upper limit
$limitupper = $count['count'];
if ($limitlower > $count['count'])
{
//Too many for lower limit
$limitlower = $count['count'] - $limit;
}
}
if ($limitlower <= 0)
{
//Can't have negative or null lower limit
$limitlower = 1;
}

Or you can read this article which will should a way to use vBulletin functions to create a pagination. https://vborg.vbsupport.ru/showthread.php?t=120540&highlight=page+results

reddyink
12-26-2007, 10:23 PM
MOT3rror I tried with code that you suggested. No affect on my page. The results are same for all page links. I am thinking we need to change something in this below line to get next page results from php page in addition to page=$page in pages links
$nav .= " <a href=\"$self?page=$page\">$page</a> ";

Any suggestions.Please help me.
Thanks for all your replies.

MoT3rror
12-26-2007, 10:31 PM
Sorry I see my error. It was checking to see if $page had a value and when it did it would make $page = 1 so change isset at the top to empty. Hopefully that makes it works. The code should be the following.
if (empty($page)
{
$page = 1;
}

//Default lower and upper limit variables
$limitlower = ($page - 1) * $limit + 1;
$limitupper = $page * $limit;

if ($limitupper > $count['count'])
{
//Too many for upper limit
$limitupper = $count['count'];
if ($limitlower > $count['count'])
{
//Too many for lower limit
$limitlower = $count['count'] - $limit;
}
}
if ($limitlower <= 0)
{
//Can't have negative or null lower limit
$limitlower = 1;
}

reddyink
12-26-2007, 10:53 PM
Thanks for your effort. I changed code to empty. But no change. Still displaying same results for all pages.
I changed in query also to
$query = "select forum.*
,user.* FROM forum AS forum
LEFT JOIN user AS user ON (forum.userid = user.userid) where user.usergroupid='6' order by forum.forumid DESC LIMIT " . ($limitlower - 1) . ", $limit";

Any other suggestions
Thanks

MoT3rror
12-26-2007, 10:56 PM
This article can show you a way to do pagination with vBulletin functions.
https://vborg.vbsupport.ru/showthread.php?t=120540&highlight=page+results

Marco van Herwaarden
12-27-2007, 09:11 AM
Where is $page loaded? All i see is that you are testing the contents of $page, but it is not loaded anywhere in your example.

reddyink
12-27-2007, 01:45 PM
Marco,
$page is loaded in the link with page=$page. In url of page when I click on on page it is showing proper link like page.php?page=2, but the results are same as page 1. Do I need to get $page values before the link. I have like this
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}

In template I just call $nav. $nav is displaying correctly with links and page numbers. but results are same
<tr>
<div border="1" align="$stylevar[right]" style="padding-bottom:2px">$nav </div>

$usercategories - This variable has results
</tr>

Any suggestions are appreciated.
Thanks

Marco van Herwaarden
12-28-2007, 07:47 AM
Having ...php?page=x in the url, does not set a variable named $page in your script. ;)

reddyink
12-29-2007, 01:10 AM
Marco,
Where should I pass that $pagevariable inaddition to url page. CAn you please explain me in detail.

Thanks for reply

WhaLberg
12-29-2007, 07:00 AM
You can use

$pagenum = $_GET['page'];

// or..
$vbulletin->input->clean_array_gpc('p', array(
'pageid' => TYPE_INT,
));

$pagenum = $vbulletin->GPC['pageid'];

reddyink
12-30-2007, 12:13 PM
It did not work wiith above code. thanks for reply. Any suggestions....