Implementing Pagination with Discuz

Implementing Pagination with Discuz

In the realm of web development, pagination is a crucial feature that enables users to navigate through large datasets efficiently. In this article, we will delve into the implementation of pagination using the Discuz framework, a popular open-source PHP-based forum software.

The Paging Function

The paging function is defined in the ./include/global.func.php file and is responsible for generating the pagination links. The function prototype is as follows:

function multi($num, $perpage, $curpage, $mpurl, $maxpages = 0, $page = 10, $autogoto = TRUE, $simple = FALSE)

Here’s a breakdown of the function parameters:

  • $num: The total number of records
  • $perpage: The number of records per page
  • $curpage: The current page number
  • $mpurl: The base URL for the pagination links
  • $maxpages: The maximum number of pages (optional)
  • $page: The number of pages to display (optional)
  • $autogoto: A flag indicating whether to automatically redirect to the next page (optional)
  • $simple: A flag indicating whether to display a simple pagination layout (optional)

The Function Implementation

The multi function is implemented as follows:

function multi($num, $perpage, $curpage, $mpurl, $maxpages = 0, $page = 10, $autogoto = TRUE, $simple = FALSE)
{
    global $maxpage;

    $ajaxtarget = empty($_GET['ajaxtarget']) ? 'ajaxtarget=" "' : 'dhtmlspecialchars($_GET['ajaxtarget'])" "';

    $multipage = '';
    $mpurl = strpos($mpurl, '?') ? '&' : '?';
    $realpages = 1;

    if ($num > $perpage) {
        $offset = 2;
        $realpages = @ceil($num / $perpage);
        $pages = $maxpages && $maxpages < $realpages ? $maxpages : $realpages;
    }

    if ($page > $pages) {
        $from = 1;
        $to = $pages;
    } else {
        $from = $curpage - $offset;
        $to = $from + $page - 1;

        if ($from < 1) {
            $to = $curpage + 1 - $from;
            $from = 1;

            if ($to - $from < $page) {
                $to = $page;
            }
        } elseif ($to > $pages) {
            $from = $pages - $page + 1;
            $to = $pages;
        }
    }

    $multipage = ($curpage - $offset > 1 && $pages > $page) ? '<a href="' . $mpurl . 'page=1" ' . $ajaxtarget . '>1...</a>' : '';

    for ($i = $from; $i <= $to; $i++) {
        $multipage = $i == $curpage ? '<strong>' . $i . '</strong>' : '<a href="' . $mpurl . 'page=' . $i . ($ajaxtarget && $i == $pages && $autogoto ? '#' : '') . '"' . $ajaxtarget . '>' . $i . '</a>';
    }

    $multipage = ($curpage < $pages && $simple) ? '<a href="' . $mpurl . 'page=' . ($curpage + 1) . '"' . $ajaxtarget . '>&rsaquo;</a>' : '';

    $multipage = ($to < $pages) ? '<a href="' . $mpurl . 'page=' . $pages . '"' . $ajaxtarget . '>&hellip;</a>' : '';

    $multipage = (! $simple && $pages > $page && ! $ajaxtarget) ? '<kbd><input type="text" name="custompage" size="3" onkeydown="if (event.keyCode == 13) {window.location.href = \'' . $mpurl . 'page=\' + this.value; return false;}"/></kbd>' : '';

    $multipage = '<div>' . ($simple ? '<em>&nbsp;' . $num . '&nbsp;</em>' : '') . '</div>' . $multipage;

    $maxpage = $realpages;
    return $multipage;
}

Using the Paging Function

To use the multi function, you need to call it with the required parameters. Here’s an example:

$pagesize = 20; // number of records per page
$query = $db->query("SELECT COUNT(*) FROM table");
$amount = $db->result($query, 0); // query the total number of records
$pagecount = ($amount < $pagesize) ? 1 : (($amount % $pagesize) ? ((int)($amount / $pagesize) + 1) : (int)($amount / $pagesize)); // calculate the total number of pages
$page = empty($_GET['page']) ? max(1, intval($_GET['page'])) : 1; // get the current value of the page
$page = $page > $pagecount ? 1 : $page; // get the current value of the page
$startlimit = ($page - 1) * $pagesize; // offset initial query
$query = $db->query("SELECT * FROM table LIMIT {$startlimit}, {$pagesize}"); // set query log
$multipage = multi($amount, $pagesize, $page, '?page.php=get=string', $pagecount); // display pagination

This code snippet demonstrates how to use the multi function to generate pagination links for a database query. The multi function takes care of calculating the total number of pages, generating the pagination links, and displaying the current page number.

By following this implementation, you can easily integrate pagination into your web application using the Discuz framework.