commit a178a7e1e1197fde28abe713bc1f463243beaefb
parent 9f91b7d63bb0f67d0241e803804ace9b04d8e772
Author: John Kubach <johnkubach@gmail.com>
Date: Mon, 17 Dec 2018 22:12:55 -0500
paginate directory
directory page now has pages. currently each page serves 5 surveys. we can play around with this value.
Diffstat:
1 file changed, 43 insertions(+), 2 deletions(-)
diff --git a/website/directory.php b/website/directory.php
@@ -8,10 +8,24 @@ include 'res/navbar.php';
echo '<link rel="stylesheet" href="res/style.css">';
echo '<div class="survey-directory">';
-$sql = "SELECT * FROM survey;";
+$sql = "SELECT COUNT(*) FROM survey;";
$result = mysqli_query($connect, $sql);
+$r = mysqli_fetch_row($result);
+$number_rows = $r[0];
+$rows_page = 5;
+$total_pages = ceil($number_rows / $rows_page);
-while ($row = mysqli_fetch_array($result)) {
+if (isset($_GET['page']) && is_numeric($_GET['page'])) {
+ $page = (int) $_GET['page'];
+} else {
+ $page = 1;
+}
+
+$offset = ($page - 1) * $rows_page;
+$sql = "SELECT * FROM survey LIMIT $offset, $rows_page;";
+$result = mysqli_query($connect, $sql);
+
+while ($row = mysqli_fetch_assoc($result)) {
$sid = $row['survey_id'];
$title = $row['title'];
$url = "takesurvey.php?sid=" . $sid;
@@ -23,6 +37,33 @@ while ($row = mysqli_fetch_array($result)) {
echo "Access code: ", $row['access_code'], "<br>";
echo "<a href='$qr'> QR Code </a> <br><br>";
}
+
+/* generate page links */
+
+$range = 3;
+
+if ($page > 1) {
+ echo " <a href='{$_SERVER['PHP_SELF']}?page=1'><<</a> ";
+ $prevpage = $page - 1;
+ echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage'><</a> ";
+}
+
+for ($x = ($page - $range); $x < (($page + $range) + 1); $x++) {
+ if (($x > 0) && ($x <= $total_pages)) {
+ if ($x == $page) {
+ echo " [<b>$x</b>] ";
+ } else {
+ echo " <a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> ";
+ }
+ }
+}
+
+if ($page != $total_pages) {
+ $nextpage = $page + 1;
+ echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>></a> ";
+ echo " <a href='{$_SERVER['PHP_SELF']}?page=$total_pages'>>></a> ";
+}
+
exit();
?>
</div>