common::paginateArray PHP Method

paginateArray() public method

Pagination.
public paginateArray ( $inArray, $page, $itemsPerPage )
    function paginateArray($inArray, $page, $itemsPerPage)
    {
        $page = $page < 1 ? 1 : $page;
        $start = ($page - 1) * ($itemsPerPage + 1);
        $offset = $itemsPerPage + 1;
        return array_slice($inArray, $start, $offset);
    }

Usage Example

Exemplo n.º 1
0
session_start();
// Load the common PHP classes.
require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "common.class.php";
require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "settings.class.php";
require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "template.class.php";
$common = new common();
$settings = new settings();
$template = new template();
$pageData = array();
// The title of this page.
$pageData['title'] = "Flights Seen";
// Add blog post data to the $pageData array.
$dbh = $common->pdoOpen();
$sql = "SELECT * FROM " . $settings::db_prefix . "flights WHERE flight LIKE ? ORDER BY lastSeen DESC, flight";
$sth = $dbh->prepare($sql);
$sth->bindValue(1, "%" . $_POST['flight'] . "%", PDO::PARAM_STR);
$sth->execute();
$flights = $sth->fetchAll();
$sth = NULL;
$dbh = NULL;
//$pageData['flights'] = $flights;
// Pagination.
$itemsPerPage = 25;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$pageData['flights'] = $common->paginateArray($flights, $page, $itemsPerPage - 1);
// Calculate the number of pagination links to show.
$pageData['pageLinks'] = count($flights) / $itemsPerPage;
$template->display($pageData);
?>

All Usage Examples Of common::paginateArray