PDA

View Full Version : [How To] Paginate Admin CP Results


Iain M
02 Jan 2009, 13:20
Had just finished developing a hack, and realized over time that the pages in the admin cp would become quite long if they weren't paginated, so I ventured into the modcp/banning.php file to help me out...

This article is for hack authors, and presumes you are familiar with vBulletin and PHP in general.

First block of code...

$vbulletin->input->clean_array_gpc('r', array(
'pagenumber' => TYPE_UINT,
));

$perpage = 25;//number of results per page
if(!$vbulletin->GPC['pagenumber']){
$vbulletin->GPC['pagenumber'] = 1;
}
$start = ($vbulletin->GPC['pagenumber'] - 1) * $perpage;

//count the number of rows
$rewardscount = $db->query_first("
SELECT COUNT(*) AS count
FROM " . TABLE_PREFIX . "table_name
");

$pagecount = ceil($rewardscount['count'] / $perpage);

Things to change are the $perpage variable, the table_name.

Then the output of the pagination...

if($pagecount > 1){
$pagenav = "<strong>$vbphrase[go_to_page]</strong>";
for ($thispage = 1; $thispage <= $pagecount; $thispage++){
if($thispage == $vbulletin->GPC['pagenumber']){
$pagenav .= " <strong>[$thispage]</strong> ";
} else {
$pagenav .= " <a href=\"incent_admin.php?$session[sessionurl]do=rewards&amp;page=$thispage\" class=\"normal\">$thispage</a> "; //your admin cp page and do=
}
}

print_description_row($pagenav, false, 4, '', 'right'); //change 4 to your colspan
}

Here you have to change incent_admin.php to your filename and the do=, and the 4 in the print_description_row.

Then there's your query:

$getrewards = $db->query_read("
SELECT rewardid, name, points, cost
FROM " . TABLE_PREFIX . "table_name
ORDER BY rewardid ASC
LIMIT $start, $perpage
");

All you just need to add to your query is the LIMIT $start, $perpage.

If you've done it correctly you should have page navigation on your admin cp page.


Here's the code in full from one of my pages, if you don't understand anything above:

$vbulletin->input->clean_array_gpc('r', array(
'pagenumber' => TYPE_UINT,
));

$perpage = 25;
if(!$vbulletin->GPC['pagenumber']){
$vbulletin->GPC['pagenumber'] = 1;
}
$start = ($vbulletin->GPC['pagenumber'] - 1) * $perpage;

$rewardscount = $db->query_first("
SELECT COUNT(rewardid) AS count
FROM " . TABLE_PREFIX . "incent_rewards
");

$pagecount = ceil($rewardscount['count'] / $perpage);

print_table_start();
print_table_header("Incent Rewards", 4, 0, '', 'center', 0);
echo '<tr><td align="center" class="thead">Reward</td>';
echo '<td align="center" class="thead">Points Required</td>';
echo '<td align="center" class="thead">Cost</td>';
echo '<td align="center" class="thead">Info</td></tr>';
if($pagecount > 1){
$pagenav = "<strong>$vbphrase[go_to_page]</strong>";
for ($thispage = 1; $thispage <= $pagecount; $thispage++){
if($thispage == $vbulletin->GPC['pagenumber']){
$pagenav .= " <strong>[$thispage]</strong> ";
} else {
$pagenav .= " <a href=\"incent_admin.php?$session[sessionurl]do=rewards&amp;page=$thispage\" class=\"normal\">$thispage</a> ";
}
}

print_description_row($pagenav, false, 4, '', 'right');
}
$getrewards = $db->query_read("
SELECT rewardid, name, points, cost
FROM " . TABLE_PREFIX . "incent_rewards
ORDER BY rewardid ASC
LIMIT $start, $perpage
");
while($reward = $db->fetch_array($getrewards)){
$cell[1] .= "<a href=\"incent_admin.php?do=edit_reward&rewardid=$reward[rewardid]\">$reward[name]</a>";
$cell[2] .= "$reward[points]";
$cell[3] .= "$reward[cost]";
$cell[4] .= "<a href=\"incent_admin.php?do=reward_stats&rewardid=$reward[rewardid]\">Stats</a> / <a href=\"incent_admin.php?do=edit_reward&rewardid=$reward[rewardid]\">Edit</a> / <a onclick=\"return confirm('Are you sure you want to delete this reward?');\" href=\"incent_admin.php?do=delete_reward&rewardid=$reward[rewardid]\">Delete</a>";

print_cells_row($cell);
unset($cell);
}

echo '<tr><td align="center" class="alt1" colspan="4"><a style="font-weight: bold;" href="incent_admin.php?do=add_reward">Add Reward</a></td></tr>';
print_table_footer(4, '', '', 0);

(Phrases are hard coded coz the hack is just for my own use, for now:))


Also, thanks to Revan for his [How-To] Paginate your results (http://www.vbulletin.org/forum/showthread.php?t=120540) for user pages.

Vaupell
07 Feb 2009, 23:56
Awsome TX....

5x stars


EDIT :
Im haveing problems with linking to pages..

The problem is, linking from pagenavs
and the one that lists my query is DO Find
and when i change the pagenav to fit the links to the other pages fail


$pagenav .= " <a href=\"sh2.php?$session[sessionurl]do=Find&amp;page=$thispage\" class=\"normal\">$thispage</a> ";



and find is the one printing results, and works fine, only shows the ammount i
set it for, howewer when clicking additional pages, it goes blank just as if it cant
located the requested destination..

any suggestions ? Im confused where the link goes, i can see it goes to do=find but the rest page=X
heh nope.

Iain M
24 Feb 2009, 17:41
Sorry for the late reply... don't come on here often.

Would you mind PM'ing me your file, and I'll take a look at it?