I recently had an interview for a programming job and after the interview I was then asked to create a function with the following brief:
The Task:
Relating to pagination links: given a page number, a total number of pages, and an amount of 'context' pages, generate the appropriate pagination links. For example, with 9 pages in total and a context of 1 page, you would get the following results for pages 1 to 9:
For page 1: 1 2 ... 8 9
For page 2: 1 2 3 ... 8 9
For page 3: 1 2 3 4 ... 8 9
For page 4: 1 2 3 4 5 ... 8 9
For page 5: 1 2 ... 4 5 6 ... 8 9
For page 6: 1 2 ... 5 6 7 8 9
For page 7: 1 2 ... 6 7 8 9
For page 8: 1 2 ... 7 8 9
For page 9: 1 2 ... 8 9
I would like you to write a PHP function with the following signature to solve this task:
function outputLinks($page, $numberOfPages, $context)
The code below is what I submitted and I've now been asked to complete another task which is good news in my eyes, so I can only assume that my code for this task was at least good enough. But I was just wondering how good my submission really was! Is there an easier (or simpler) way to achieve the same results? Could I shorten my code at all?
<?php
// Global Variables
$p = $_GET['p']; // Current Page
$t = 9; // Total Pages
$c = 1; // Context Pages
// Pagination Function
function outputLinks($page, $numberOfPages, $context) {
// Correct page variable
if (!$page || $p < 1) { $page = 1; }
if ($page > $numberOfPages) { $page = $numberOfPages; }
// Set array variables
$leftArray = $midArray = $rightArray = array();
// Left Array
for ($x = 1; $x <= (1 + $context) && $x <= $numberOfPages; $x++) {
array_push($leftArray, $x);
}
// Right Array
for ($x = $numberOfPages - $context; $x <= $numberOfPages && $x > 0; $x++) {
if (!in_array($x, $leftArray)) {
array_push($rightArray, $x);
}
}
// Left/Right Array First/Last values to variables
$firstInLeftArray = current($leftArray);
$lastInLeftArray = end($leftArray);
$firstInRightArray = current($rightArray);
$lastInRightArray = end($rightArray);
// Middle Array
if ($page == $firstInLeftArray || $page == $lastInRightArray) {
if (($firstInRightArray - $lastInLeftArray) > 1) {
array_push($midArray, ' ... ');
}
} else {
if (in_array($page, $leftArray)) {
for ($x = $page; $x <= ($page + $context) && $x <= $numberOfPages; $x++) {
if (!in_array($x, $leftArray) && !in_array($x, $rightArray)) {
array_push($midArray, $x);
}
}
} else if (in_array($page, $rightArray)) {
for ($x = ($page - $context); $x < $firstInRightArray && $x > $lastInLeftArray; $x++) {
array_push($midArray, $x);
}
} else {
for ($x = ($page - $context); $x <= ($page + $context) && $x > 0; $x++) {
if (!in_array($x, $leftArray) && !in_array($x, $rightArray)) {
array_push($midArray, $x);
}
}
}
// Middle Array First/Last values to variables
$firstInmidArray = current($midArray);
$lastInmidArray = end($midArray);
// Ellipses for omitted numbers
if (($firstInmidArray - $lastInLeftArray) > 1) {
array_push($leftArray, ' ... ');
}
if (!empty($midArray) && ($firstInRightArray - $lastInmidArray) > 1) {
array_push($midArray, ' ... ');
}
}
// Display Arrays
echo 'Page '.$page.' of '.$numberOfPages.': ';
foreach ($leftArray as $value) {
if ($value == ' ... ') { echo $value; } else { echo ' <a href="?p='.$value.'" target="_self">'.$value.'</a> '; }
}
foreach ($midArray as $value) {
if ($value == ' ... ') { echo $value; } else { echo ' <a href="?p='.$value.'" target="_self">'.$value.'</a> '; }
}
foreach ($rightArray as $value) {
echo ' <a href="?p='.$value.'" target="_self">'.$value.'</a> ';
}
}
// Initialize document
print('<!DOCTYPE HTML><html><head><title>Pagination Function</title></head><body>');
// Generate Pagination through the outputLinks function
outputLinks($p, $t, $c);
// Close the document
print('</body></html>');
?>