Like the title says, my pagination only occurs on the first page.
This query selects a total number of rows from user input, and then selects the type records they want.
What I'm wanting it to do is grab the records, and then display them on each page but it seems that every time next is hit it runs the query again and gets a new value. Then the pagination buttons ("previous/ Next") disappear.
I run it locally once and it's fine. When I hit next it goes goes to pagination.php?pn=2, but then I get errors
PHP Notice: Undefined index: count on line 5 PHP Notice: Undefined index: topic on line 6 PHP Warning: join (): invaldd arguments (Im guessing because of the above notices) PHP Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolen given (again probably because of lines 5 and 6 and it's getting a false value)
Here's my code.
<?php
include ("link.php");
$limitValue = $_POST['count'];
$type = $_POST['topic'];
$qType = join("','", $type);
$query = mysqli_query($link,"SELECT count(question_id)
FROM questions
WHERE questionType IN ('$qType')
LIMIT $limitValue" );
$row = mysqli_fetch_row($query);
// Assign the value of $row[0] (the results of our query, we need the square brackets for this to work) to $rows
$rows = $row[0];
$page_rows = 1;
$last = ceil($rows/$page_rows);
if($last <1){
$last = 1;
}
$pagenum = 1;
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
if ($pagenum < 1) {
$pagenum = 1;
}
else if ($pagenum > $last) {
$pagenum = $last;
}
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$sql = "SELECT question_id, questionType, question, answerA, answerB, answerC, answerD
FROM questions
WHERE questionType IN ('$qType')
ORDER BY RAND()
$limit";
$result = mysqli_query($link, $sql);
$paginationCtrls = '';
if($last != 1){
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'" class="btn btn-default">Previous</a> ';
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'" class="btn btn-default">'.$i.'</a> ';
}
}
}
$paginationCtrls .= ''.$pagenum.' ';
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'" class="btn btn-default">'.$i.'</a> ';
if($i >= $pagenum+4){
break;
}
}
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'" class="btn btn-default">Next</a> ';
}
}
while ($row = mysqli_fetch_array($result)) {
echo "QID: " . $row["question_id"]. "<br>";
"Question Type: " . $row["questionType"]. "<br><br>" ;
"Question: " . $row["question"]. "<br><br><br>";
echo "<input type = 'Radio' Name = 'Answer[]' value ='A' > A. <br>";
echo "<input type = 'Radio' Name = 'Answer[]' value ='B' > B. <br>";
echo "<input type = 'Radio' Name = 'Answer[]' value ='C' > C. <br>";
echo "<input type = 'Radio' Name = 'Answer[]' value ='D' > D. <br><br>";
echo "<input name='submit' type='submit' value='Submit'><br><br><hr>";
echo $paginationCtrls;
}
?>