[How To] Paginate Admin CP Results
by
02 Jan 2009
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...
PHP 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...
PHP Code:
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&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:
PHP Code:
$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:
PHP Code:
$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&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 for user pages.
|