issue "checked value hold in pagination" - php

I have a script with the code below and a pagination where the user can check checkboxes to narrow doen the results shown everything seems to work great the only problem is that when a user clicks the next page in the pagination the results are lost and the default results are shown. I think I may need to use sessions to store the query or statement but I am unsure how to use them. If anyone can help me or point me in the right direction I would be very greatful.
<?php
include("config.php");
$start = 0;
$per_page = 10;
if(!isset($_GET['page'])){
$page = 1;
} else
{
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;
$sql="select id,question,correctAnswer,category from math order by id";
$num_rows = mysql_num_rows(mysql_query($sql));
$num_pages = $num_rows / $per_page;
$sql .= " LIMIT $start, $per_page";
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result))
{ ?>
..............
<?php
if($page > 1){
$prev = $page - 1;
$prev = " <a href='?page=$prev'>prev</a> ";
} else {
$prev = "";
}
if($page < $num_pages){
$next = $page + 1;
$next = " <a href='?page=$next'>next</a> ";
}
else
{
$next = "";
}
echo $prev;
echo $next;
?>
and the following code is about selection of checkbox value one by one and all
function selectall()
{
var selectAll = document.forms[0].length;
if(document.forms[0].topcheckbox.checked==true)
{
for(i=1;i<selectAll;i++)
{
document.forms[0].elements[i].checked=true;
}
}
else
{
for(i=1;i<selectAll;i++)
document.forms[0].elements[i].checked=false;
}
}
function goSelect()
{
var select = document.forms[0].length;
var checkboxes=""
for(i=1;i<select;i++)
{
if(document.forms[0].elements[i].checked==true)
checkboxes+= " " + document.forms[0].elements[i].name
}
if(checkboxes.length>0)
{
var con=confirm("You have selected ...........");
if(con)
{
window.location.assign("submit.php?recsno="+checkboxes)
// document.forms[0].submit()
}
}
else
{
alert("No record is selected.")
}
}

You could probably use the PHP Get method? and then receive it in your javascript.
For example; searchurl.com/results.php?box=1
Then retrieve it on the next page.
I believe this should help you, be sure to let me know if you need more detail.

This is my all code.
include("config.php");
$start = 0;
$per_page = 10;
if(!isset($_GET['page'])){
$page = 1;
} else{
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;
$sql="select id,question,correctAnswer,category from math order by id";
$num_rows = mysql_num_rows(mysql_query($sql));
$num_pages = $num_rows / $per_page;
$sql .= " LIMIT $start, $per_page";
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result))
{ ?>
<tr>
<td><input name="<?php echo $row['id']; ?>" type="checkbox" class="check"></td>
<td><center><?php echo htmlspecialchars_decode($row['id'], ENT_QUOTES); ?></center></td>
<td style="padding-left:25px;margin:15px"><?php echo $_SESSION['question']= htmlspecialchars_decode($row['question'], ENT_QUOTES); ?></center></td>
<td><center><?php echo htmlspecialchars_decode($row['correctAnswer'], ENT_QUOTES); ?></center></td>
<td><center><?php echo htmlspecialchars_decode($row['category'], ENT_QUOTES); ?></center></td>
<td>View</td>
</tr>
<?php } ?>
<tr>
<td><strong>Select</strong></td>
<td><strong>Sr No </strong></td>
<td><strong>Question </strong></td>
<td><strong>CorrectAnswer </strong></td>
<td><strong>Category </strong></td>
<td><strong>View Details</strong></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<?php
if($page > 1){
$prev = $page - 1;
$prev = " <a href='?page=$prev'>prev</a> ";
} else {
$prev = "";
}
if($page < $num_pages){
$next = $page + 1;
$next = " <a href='?page=$next'>next</a> ";
}
else
{
$next = "";
}
echo $prev;
echo $next;
?>

Related

Detect Previous Next ID

I have a little script that have a Previous & Next button.
My problem is I want to detect & place the ID in those buttons
this is the code from the pagination
<body>
<?php include_once 'data.php'; ?>
<center>
<ul class="pagination">
<?php
if($page_counter == 0){
echo "<li><a href=?start='0' class='active'>0</a></li>";
for($j=1; $j < $paginations; $j++) {
echo "";
}
}else{
echo "<a href=?start=$previous><button>Previous</button></a>";
for($j=0; $j < $paginations; $j++) {
if($j == $page_counter) {
echo " ";
}else{
echo " ";
}
}if($j != $page_counter+1)
echo "<a href=?start=$next><button>Next</button></a>";
}
?>
</ul>
</center>
In this part I have the ID but the problem is I can`t get it with this example to place it into the pagination.
<?php
foreach($result as $row) {
echo '
<div class="card"><button1>
'. $row['notice_id'] .'
<div class="time-left">
<div class="dropdown1">
' ;
}
}
else {
echo '';
}
$conn->close();
?>
</div></div>
This is the code from data.php I think I need to place some code into for detection from the ID
<?php
//include configuration file
require 'configuration.php';
$start = 0; $per_page = 1;
$page_counter = 0;
$next = $page_counter + 1;
$previous = $page_counter - 1;
if(isset($_GET['start'])){
$start = $_GET['start'];
$page_counter = $_GET['start'];
$start = $start * $per_page;
$next = $page_counter + 1;
$previous = $page_counter - 1;
}
// query to get messages from messages table
$q = "SELECT * FROM group_notice LIMIT $start, $per_page";
$query = $db->prepare($q);
$query->execute();
if($query->rowCount() > 0){
$result = $query->fetchAll(PDO::FETCH_ASSOC);
}
// count total number of rows in students table
$count_query = "SELECT * FROM group_notice";
$query = $db->prepare($count_query);
$query->execute();
$count = $query->rowCount();
// calculate the pagination number by dividing total number of rows with per page.
$paginations = ceil($count / $per_page);
?>
You can use some formulas for calculate limit and offset of results.
For example you have 100 records, and you want to paginate it into 10 records per page, and the formula is like below.
// Get Page from Query String
$page = 1;
if(!empty($_GET["page"])){
$page = $_GET["page"];
}
// Get record count
$count_query = "SELECT * FROM group_notice";
$query = $db->prepare($count_query);
$query->execute();
$count = $query->rowCount();
$per_page = 10; // records per page
$pages = ceil($count/$per_page); // get total page
$start = $page * $per_page - $per_page; // get offset
// Do something with your records
$query = db->prepare("SELECT * FROM group_notice LIMIT ?, ?");
$query->bind_param("ii", $start, $per_page); // you should use bind param if you use prepared statement
$query->execute();
if($query->rowCount() > 0){
$result = $query->fetchAll(PDO::FETCH_ASSOC);
}
if($page > 1){
echo 'Prev'; // Print Prev Page
}
for($i = 1; $i <= $pages){
echo ''.$i.''; // Print Page Number
}
if($page < $pages){
echo 'Next'; // Print Next Page
}
Hope it helps.

PHP pagination isn't working and I'm not sure why

I feel like I ask a lot of questions.
Anyways, I'm writing pagination for some database entries, and it looks sound to me but it's only displaying the first 10 posts and nothing else when I click the links.
The whole shebang is right here:
<?php
$post_limit = 10;
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("failed to connect: " . $conn->connect_error);
}
$sql = "SELECT count(id) FROM $tablename";
$result = mysqli_query($conn, $sql);
if (!$result) {
echo "you fucked up";
} else {
echo $row["id"];
}
$row = mysqli_fetch_array($result, MYSQL_NUM);
$post_count = $row[0];
if(isset($_GET['page'])) {
$page = $_GET['page'] + 1;
$offset = $post_limit * $page;
} else {
$page = 0;
$offset = 0;
}
$post_left = $post_count - ($page * $post_limit);
$sql = "SELECT id, upvotes, downvotes, name, title, message, date, time FROM posts ORDER BY date DESC, time DESC LIMIT $offset, $post_limit";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<br><div id='messageBar'>";
echo " ❖ </b>";
echo "Posted by <b>";
echo htmlspecialchars($row["name"]);
echo "</b> on ";
echo $row["date"];
echo " at ";
echo $row["time"];
if (!empty($row['title'])) {
echo " - <b>";
echo htmlspecialchars($row["title"]);
echo "</b>";
}
echo "<span style='float: right'>#";
echo $row["id"];
echo "</span>";
echo "</div><div id='messageContent'>";
echo htmlspecialchars($row["message"]);
echo "<br><br><span id='commentLink'><a class='commentLink' href='thread.php?id=$row[id]'>view thread </a></span>";
echo "<br></div><br><hr>";
}
} else {
echo "<br>";
echo "<center><i>it's dusty in here</i></center>";
echo "<br>";
}
if ($page > 0) {
$last = $page - 2;
echo "<a href='$_PHP_SELF?page = $last'>previous page</a> | ";
echo "<a href='$_PHP_SELF?page = $page'>next page</a>";
} else if ($page == 0) {
echo "<a href='$_PHP_SELF?page = $page'>next page</a>";
} else if ($post_left < $post_limit) {
$last = $page - 2;
echo "<a href='$_PHP_SELF?page = $last'>previous page</a>";
}
$conn->close();
?>
The link for the next page appears at the bottom, but clicking it takes you to the page you're already on with the same 10 most recent posts.
I am trying to learn PHP as I go, so I appreciate any help. Thank you!
Instead of using $_GET please use $_SESSION to manage a counter.
Every time you go in this page you increment the counter.
So, you have to do this on the top of the page:
if(isset($_SESSION['counter'])) {
$page = $_SESSION['counter'] + 1;
$offset = $post_limit * $page;
} else {
$page = 0;
$offset = 0;
$_SESSION['counter']=0;
}
Try this.
By the way there is Datatables which do what you're looking for.
P.S. If you don't start sessions in some other points in your code, please put session_start(); in the first page (e.g., login.php) otherwise put on this page.
Fixed. Problem was here:
if ($page > 0) {
$last = $page - 2;
echo "<a href='$_PHP_SELF?page = $last'>previous page</a> | ";
echo "<a href='$_PHP_SELF?page = $page'>next page</a>";
} else if ($page == 0) {
echo "<a href='$_PHP_SELF?page = $page'>next page</a>";
} else if ($post_left < $post_limit) {
$last = $page - 2;
echo "<a href='$_PHP_SELF?page = $last'>previous page</a>";
}
The URL I was linking to wasn't supposed to have spaces around the = sign. It's still buggy, but it works.

how to load pagnation with ajax

i have this code pagination and this code for quiz after click to next saves session and header(location) to next page for quiz.
now i want to load next page without refresh page after click to input next load next page ...
<?php
$cid = (int)$_GET['id'];
if(!isset($_GET['page'])){
$page = 1;
}else{
$page = (int)$_GET['page'];
}
$records_at_page = 1;
$q = $db->query("SELECT * FROM `questions` WHERE `cat`='".$cid."'");
$records_count = $q->num_rows;
$pages_count = (int)ceil($records_count / $records_at_page);
if(($page > $pages_count) or ($page <= 0)){
die('No More Pages');
}
$start = ($page - 1) * $records_at_page;
$end = $records_at_page;
$next = $page + 1;
$prev = $page - 1;
$qui = isset($_POST['val']);
if(isset($_POST['next'])){
$tst = $_SESSION['val'] = $qui;
$_SESSION['value'] = ($_SESSION['value'] + $tst);
if($next <= $pages_count){
header('Location: quiz.php?id='.$cid.'&page=' .$next. '');
}
if($next > $pages_count){
echo 'Result';
exit;}
}
if($records_count != 0){
$ar = $db->query("SELECT * FROM `questions` WHERE `cat`='".$cid."' order by `id` desc LIMIT $start,$end");
while($rowar = $ar->fetch_object()){
echo"
<form ation='' method='POST'>
<div>".$rowar->question."</div>";
$ans = $db->query("select * from `answer` where `question_id`='".$rowar->id."' order by `id` desc");
$rowans = $ans->fetch_object();
echo"
<div ><input class='rgt' type='radio' name='val' value='".$rowans->val1."'/><p>".$rowans->answ1."</p></div>
<div><input type='submit' name='next'value='Next'/></div>
</form>
";
}
}
?>

php next page blank for search result

my default load page is ok with next and prev results.. but for search results i can get only first page then blank for the next page. let say total search results row is 9, it display 3 pages with next link. but when i click the next link, it queries no result on the page 2. here is my code:
echo '<table><tr><td>';
echo '<form method="post" name="frmSearch" action="mypage.php>
Search by name
<input type="text" name="txtSearch">
<input type="submit" name="submit" value="Search">
</form>';
$per_page = 5;
$page = 1;
if (isset($_GET['page']))
{
$page = intval($_GET['page']);
if($page < 1) $page = 1;
}
$start_from = ($page - 1) * $per_page;
$Prev_Page = $page - 1;
$Next_Page = $page + 1;
if (!$_POST){
//$sql = "SELECT * FROM mytable LIMIT $start_from, $per_page";
//$totalr = mysql_query("SELECT COUNT(*) FROM mytable");
//$totalr = mysql_fetch_row($totalr);
//$totalr = $totalr[0];
//$total_pages = $totalr / $per_page;
//$total_pages = ceil($total_pages);
//$listresult = mysql_query($sql);
//$total = mysql_num_rows($listresult);
}
else {
if ($_POST['txtSearch']!="") {$cond1 = " name LIKE
'%".$_POST['txtSearch']."%' ";} else {$cond1 = 1; }
$sql = "SELECT * FROM mytable WHERE ".$cond1." LIMIT $start_from, $per_page";
$totalr = mysql_query("SELECT COUNT(*) FROM mytable WHERE name LIKE
'%".$_POST['txtSearch']."%' ");
$totalr = mysql_fetch_row($totalr);
$totalr = $totalr[0];
$total_pages = $totalr / $per_page;
$total_pages = ceil($total_pages);
$listresult = mysql_query($sql);
$total = mysql_num_rows($listresult);
}
if($total == 0){
echo "<center>No records found.</center>";
}
echo "<span style='float:right;margin-top:8px;'>[ <a href='new.php'>Add New</a>
]</span></td></tr>";
echo "<table>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
";
while ($_POST = mysql_fetch_assoc($listresult)){
echo "<tr>";
echo "<td>" . $_POST['id'] . "</td>";
echo "<td>" . $_POST['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</table>";
echo $totalr." result";
echo "<br>";
if ($Prev_Page) {
echo " <a href ='{$_SERVER['PHP_SELF']}?page=$Prev_Page'><< Prev</a> ";
}
if ($totalr > $per_page) {
for($i = 1; $i <= $total_pages; ++$i)
{
echo "<a href='{$_SERVER['PHP_SELF']}?page=$i'>$i</a> | ";
}
}
if ($page!=$total_pages) {
echo " <a href='{$_SERVER['PHP_SELF']}?page=$Next_Page'>Next >></a> ";
}
maybe you need to use SELECT COUNT(*) FROM mytable WHERE name LIKE '%$search%' ... remember to escape the $_POST['txtSearch']

Previous/Next Navigation issues with loading navigation links

I have used this code before with no issues but... now I can't seem to load the page request when pressing Previous/Next Navigation if you can see where I went wrong be a help.
<?php
include 'config.php';
$db = mysql_connect($host, $username, $password);
mysql_select_db($db_name,$db);
$sql = "SELECT u_id FROM users";
$query = mysql_query($sql,$db);
$total_results = mysql_num_rows($query);
$limit = "5"; //limit of archived results per page.
$total_pages = ceil($total_results / $limit); //total number of pages
if (empty($page))
{
$page = "1"; //default page if none is selected
}
$offset = ($page - 1) * $limit; //starting number for displaying results out of DB
$query = "SELECT * FROM users ORDER BY u_id LIMIT $offset, $limit";
$result = mysql_query($query);
//This is the start of the normal results...
while ($row = mysql_fetch_array($result))
{
?>
<HR>
Username : <B><?echo $row['uname'] ?></B><BR>
user Message: <?echo $row['umess'] ?>
<BR>
<?
}
mysql_close();
// This is the Previous/Next Navigation stuff having issue with
echo "<font face=Verdana size=1>";
echo "Pages:($total_pages) "; // total pages
if ($page != 1)
{
echo "<a href=?&page=1><< First</a> "; // First Page Link
$prevpage = $page - 1;
echo " <a href=?page=$prevpage><<</a> "; // Previous Page Link
}
if ($page == $total_pages)
{
$to = $total_pages;
}
elseif ($page == $total_pages-1)
{
$to = $page+1;
}
elseif ($page == $total_pages-2)
{
$to = $page+2;
}
else
{
$to = $page+3;
}
if ($page == 1 || $page == 2 || $page == 3)
{
$from = 1;
}
else
{
$from = $page-3;
}
for ($i = $from; $i <= $to; $i++)
{
if ($i == $total_results) $to=$total_results;
if ($i != $page)
{
echo "<a href=?showold=yes&page=$i>$i</a>";
}
else
{
echo "<b><font face=Verdana size=2>[$i]</font></b>";
}
if ($i != $total_pages)
echo " ";
}
if ($page != $total_pages)
{
$nextpage = $page + 1;
echo " <a href=?page=$nextpage>>></a> "; // Next Page Link
echo " <a href=?page=$total_pages>Last >></a>"; // Last Page Link
}
echo "</font>";
?>
After a test run you can see 1st page request is fine untill you click next or a page number it keeps going back to 1st page.
Instead of
if (empty($page))
{
$page = "1"; //default page if none is selected
}
Try :
if (isset($_GET['page']))
{
$page = $_GET['page'];
}
else
{
$page = 1;
}

Categories