This question already has an answer here:
PHP Pagination removes search term and reverts back to displaying all results, fix?
(1 answer)
Closed 6 years ago.
Creating a new project using php: retrieving data from mysql database using a search function (search.php) - added pagination to the search result on results page (find.php). Question is, when the first search is displayed, it shows the correct data; however, when i click on 2nd page (pagination button), it populates all the data again - not the term i searched for: Please help!
search.php
<form action="find.php" method="post">
<input name="search" type="search" autofocus><input type="submit" name="button">
</form>
find.php
<?php
include_once("config.php");
if(isset($_POST['button'])){
$search=$_POST['search'];
}
$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit ");
$row = mysql_num_rows($result);
$page_rows = 3;
$last = ceil($row/$page_rows);
if($last < 1){
$last = 1;
}
$pagenum = 1;
if(isset($_GET['page'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['page']);
}
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit ");
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
$paginationCtrls = '';
$self = $_SERVER['PHP_SELF'];
if($last != 1){
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= 'Previous';
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= ''.$i.'';
}
}
}
$paginationCtrls .= '<span class="current">'.$pagenum.'</span>';
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= ''.$i.'';
if($i >= $pagenum+4){
break;
}
}
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= 'Next ';
}
}
?>
<table border='1' cellpadding='10' style="border-collapse:collapse;">
<tr>
<th>id</th>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>email</th>
<th>website</th>
<th>locations</th>
<th>info</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['website']; ?></td>
<td><?php echo $row['locations']; ?></td>
<td><?php echo $row['info']; ?></td>
</tr>
<?php
}
?>
You must have to set search term in pagination links. Try this.
<form action="find.php" method="post">
<input name="search" type="search" autofocus><input type="submit" name="button">
</form>
<?php
include_once("config.php");
if(isset($_POST['button'])){
$search=$_POST['search'];
}
$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit ");
$row = mysql_num_rows($result);
$page_rows = 3;
$last = ceil($row/$page_rows);
if($last < 1){
$last = 1;
}
$pagenum = 1;
if(isset($_GET['page'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['page']);
}
// get search tearm from url
if(isset($_GET['search'])){
$search=$_GET['search'];
}
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit ");
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
$paginationCtrls = '';
$self = $_SERVER['PHP_SELF'];
// you must have to set search tearm in pagination links
if($last != 1){
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= 'Previous';
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= ''.$i.'';
}
}
}
$paginationCtrls .= '<span class="current">'.$pagenum.'</span>';
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= ''.$i.'';
if($i >= $pagenum+4){
break;
}
}
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= 'Next ';
}
}
?>
<table border='1' cellpadding='10' style="border-collapse:collapse;">
<tr>
<th>id</th>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>email</th>
<th>website</th>
<th>locations</th>
<th>info</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['website']; ?></td>
<td><?php echo $row['locations']; ?></td>
<td><?php echo $row['info']; ?></td>
</tr>
<?php
}
?>
Related
How could I go about inserting a div after every fifth row on result page?
I have a script that displays 15 rows from a database in every pagination.
This my entire script
<?php
$sql = "SELECT COUNT(id) FROM table";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
// Here we have the total row count
$rows = $row[0];
$page_rows = 15;
$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;
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$sql = "SELECT id, fname, image_text, address, contact, price, image FROM table WHERE fname LIKE '%".$search."%' $limit";
$query = mysqli_query($db_conx, $sql);
$textline1 = "table (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<td><a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> </td> ';
for($i = $pagenum-3; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= '<td><a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> </td> ';
}
}
}
$paginationCtrls .= ''.$pagenum.' ';
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= '<td><a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a></td> ';
if($i >= $pagenum+3){
break;
}
}
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' <td> <a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a></td> ';
}
}
$list = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
?>
<div class="light-purple column">
<table style="height:100px; border-bottom:#F88 solid 2px;" width="100%" border="0">
<tr>
<td>
<img src="img/<?php echo $row['image']; ?>" alt="No Image">
</td>
<td width="70%">
<div class="detail_text" style="font-weight:bold;"><input type="hidden" value="<?php echo $row['id']; ?>"/>
<?php echo $row['fname']; ?>
</div>
<div class="detail_text " ><?php echo $row['image_text']; ?></div>
<div style="color: #999; font-weight: bold;"><?php echo $row['address']; ?></div>
<div class="detail_text" style="font-weight:bold; color: #606;">
<table width="95%" border="0">
<tr>
<td width="51%"><?php echo $row['contact']; ?></td>
<td width="49%"><?php echo $row['price']; ?></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
<?php }
?>
I want to able to put an ad div in every 5 rows something like this "
<div class="ad"></div>
<row> content </row>
<row> content </row>
<row> content </row>
<row> content </row>
<row> content </row>
<div class="ad"></div>
<row> content </row>
<row> content </row>
<row> content </row>
<row> content </row>
<row> content </row>
<div class="ad"></div>
Can anyone assist me as to how could I set up the PHP script that will allow me to count the rows returned and after every certain number of rows , insert an ad script? Or is there a way to do this within rows itself?
You can use modulus operator,
$count = 1;
while{
if($count % 5 == 0)
echo '<div class="add"></div>';
$count++;
}
Just add a count variable to count till 5 and then reset to zero inside your loop.
$count=0;
while{
echo "<row> content </row>";
$count++;
if($count>=5)
{
echo "<div class="ad"></div>";
$count=0;
}
}
code:
<table>
<tr style="background: white url(bg.gif) repeat-x left top;color: #fff;">
<th>College Id</th>
<th>College Name</th>
<th>Website</th>
<th>Field</th>
<th>City</th>
<th>Action</th>
</tr>
<?php
$per_page=10;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else {
$page=1;
}
$start_from = ($page-1) * $per_page;
$sql="select * from all_colleges LIMIT $start_from, $per_page";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['college_id']; ?></td>
<td><?php echo $row['college_name']; ?></td>
<td><?php echo $row['website']; ?></td>
<td><?php echo $row['field']; ?></td>
<td><?php echo $row['city']; ?></td>
<td>
<a class='view' title='view' href='view.php?id=<?php echo $row['college_id']; ?>'>
<img src='gridview/view.png' alt='view' />
</a>
<a class='update' title='Update' href='update.php?id=<?php echo $row['college_id']; ?>'>
<img src='gridview/update.png' alt='Update' />
</a>
<a class='delete' title='delete' href='delete.php?ad_id=<?php echo $row['college_id']; ?>'>
<img src='gridview/delete.png' alt='delete' />
</a>
</td>
</tr>
<?php
}
?>
</table>
<div style="padding: 10px;">
<?php
$query = "select * from all_colleges";
$result = mysqli_query($link, $query);
$total_records = mysqli_num_rows($result);
$total_pages = ceil($total_records / $per_page);
echo "<center><a href='admin.php?page=1' style='padding:5px;'>".'previous'."</a>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='admin.php?page=".$i."'>".$i."</a>";
}
echo "<a href='admin.php?page=$total_pages'>".'next'."</a></center>";
?>
</div>
In this code I want to create a pagination with next and previous button here this code is working properly but it show diffrent pagination i.e. if the number of results is 200 then it will display like
previous 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 next
but I want like
previous 1 2 3 4 5 ....8 9 next
How can I do this please help ?
Thank You
You can check each iteration of your loop if you want to display the number.
In the following code, only:
the first two pages
the last two pages
the current page
each two pages to the left and to the right of the current page
are displayed.
$skipped = false;
for ($i = 1; $i <= $total_pages; $i++) {
if ($i < 3 || $total_pages- $i < 3 || abs($page - $i) < 3) {
if ($skipped)
echo '<span> ... </span>';
$skipped = false;
echo "<a href='admin.php?page=" . $i . "'>" . $i . "</a>";
} else {
$skipped = true;
}
}
If you have a lot of pages, the big loop is unnecessary.
Instead, you could use three different loops:
$done = [];
for ($i = 1; $i < 3; $i++) {
$done[$i] = true;
//echo
}
if ($page > 3)
echo '<span> ... </span>';
for ($i = $page - 2; $i < $page + 3; $i++) {
if (isset($done[$i])
continue;
$done[$i] = true;
//echo
}
if ($page < $total_pages - 3)
echo '<span> ... </span>';
for ($i = $total_pages- 2; $i < $total_pages; $i++) {
if (isset($done[$i])
continue;
$done[$i] = true;
//echo
}
To fix your two other buttons (previous and next), link to $page - 1 and$page + 1 instead of 1 and $total_pages.
For the past 2 days I've been looking for a pagination script of sorts, and I found this one which I'm starting to implement on my website.
The code is the following:
<?php
if (!(isset($pagenum)))
{
$pagenum = 1;
}
$data = mysqli_query($lig,"SELECT * FROM requisicao") or die("data");
$rows = mysqli_num_rows($data);
$page_rows = 5;
$last = ceil($rows/$page_rows);
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$data_p = mysqli_query($lig,"SELECT * FROM requisicao $max") or die("data_p");
while($info = mysqli_fetch_assoc( $data_p ))
{
?>
<tr>
<td><?php print $info['id_requisicao']; ?> </td>
<td><?php print $info['username']; ?> </td>
<td><?php print $info['nome_servico']; ?> </td>
<td><?php print $info['data_requisicao']; ?> </td>
<td><?php print $info['estado_requisicao']; ?> </td>
<td><?php print $info['prioridade']; ?> </td>
<td><?php print $info['total']; ?> </td>
<td align="center">+</td>
<tr>
<?php
}
?>
</tbody>
</table>
<?php
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-Início</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Anterior</a> ";
}
echo " ---- ";
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Seguinte -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Última ->></a> ";
}
?>
I'm getting the first 5 rows out of it, but when I press Seguinte (which is Next) it changes the link (TO PAGE 2) but it doesn't change the values that get outputed. Basically, it always shows page 1.
I've also looked into several similar questions but I haven't found solutions that fix my problem.
Thanks in advance, and my apologies if this is a simple problem and I'm just overlooking things :s
The problem is that $pagenum will be initialized as 1, anyway.
Instead of:
if (!(isset($pagenum)))
{
$pagenum = 1;
}
Try writing this:
if (!isset($_REQUEST['pagenum']) || !is_int($_REQUEST['pagenum']))
{
$pagenum = 1;
} else {
$pagenum = (int)$_REQUEST['pagenum'];
}
You may find some more info handling HTTP Request variables here
How would I go about making a pagination script for this MySQL & PHP query.
if (isset($_GET['c'])) {
$c = $_GET['c'];
}
$query = mysql_query("SELECT * FROM Categories WHERE category = '$c' ");
WHILE($datarows = mysql_fetch_array($query)):
$id = $datarows['id'];
$category = $datarows['category'];
$code = $datarows['code'];
endwhile;
$query2 = mysql_query("SELECT * FROM Games WHERE category = '$code' ");
WHILE($datarows_cat = mysql_fetch_array($query2)):
$title = $datarows_cat['title'];
$description = $datarows_cat['description'];
$imgurl = $datarows_cat['image_name'];
$category = $datarows_cat['category'];
$views = $datarows_cat['view_count'];
$pagename = $datarows_cat['pagename'];
$featured = $datarows_cat['featured'];
if ($featured =="1") {$f = "<img src='http://my-site.com/images/star.png' width='13px' title='Featured Game' /> Featured"; } else {$f = "";}
if(is_int($views/2)) {
$views = $views / 2;
} else { $views = $views / 2 + .5; }
if (strlen($description) > 95) {
$desc= substr($description,0,95);
$desmod = "$desc...<br/>- Read More";
}
else {$desmod = "$description";}
echo "$f - $title - $desmod<br/>";
endwhile;
And when I visit http://my-site.com/categories/Action for instance, The code looks up that category in my category table, then once it gets the unique code for that category, It runs another query to find all games in another table with that category code. Currently, however, I have 200+ games loading for a single category which causes a great amount of loading time.
Thanks for your help!
First of all find out how many games are there for a specific category
change the line
$query2 = mysql_query("SELECT * FROM Games WHERE category = '$code' ");
to
$sql="SELECT * FROM Games WHERE category = '$code' ";
$query_count=mysql_query($sql);
Add following after it
$per_page =30;//define how many games for a page
$count = mysql_num_rows($query_count);
$pages = ceil($count/$per_page);
if($_GET['page']==""){
$page="1";
}else{
$page=$_GET['page'];
}
$start = ($page - 1) * $per_page;
$sql = $sql." LIMIT $start,$per_page";
$query2=mysql_query($sql);
Then display the numbers of pages where you want
<ul id="pagination">
<?php
//Show page links
for ($i = 1; $i <= $pages; $i++)
{?>
<li id="<?php echo $i;?>"><?php echo $i;?></li>
<?php
}
?>
</ul>
Use CSS for pagination this will do the trick
//database connation
<?php
$conn = new mysqli("localhost", "root", "","database_name");
?>
<!DOCTYPE html>
<html>enter code here
<head>
<title>View Student Details</title>
<h1 align="center"> Student Details </h1>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="bootstrap/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<div class="row">
<div class="col-sm-2"></div>
<div class="col-sm-8">
<form>
<table class="table table-striped">
<tr>
<th>Sr.No.</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Class</th>
<th>Gender</th>
<th>Birth of Date</th>
<th>Contact No.</th>
<th>Action</th>
</tr>
<?php
$count=0;
if(isset($_GET['page_count']))
{
$count=1;
$page_count=$_GET['page_count']-1;
$count=$page_count*10;
}
$q="SELECT * from student_detail LIMIT $count,10";
$result=$conn->query($q);
$j=0;
while($data=$result->fetch_array())
{ $j=$j+1;
?>
<tr>
<td><?php echo $j ?></td>
<td><?php echo $data['std_id'] ?></td>
<td><?php echo $data['std_name'] ?></td>
<td><?php echo $data['std_class'] ?></td>
<td><?php echo $data['gender'] ?></td>
<td><?php echo $data['bod'] ?></td>
<td><?php echo $data['contact'] ?></td>
<td>
<div class="row">
<div class="col-sm-12">
Delete
Update
</div>
</div>
</td>
</tr>
<?php } ?>
</table>
<ul class="pagination">
<?php
$q="SELECT count(std_id) from student_detail";
$result=$conn->query($q);
$data=$result->fetch_array();
$total=$data[0];
$total_page=ceil($total/10);
if($total_page>1)
{
for($i=1;$i<=$total_page;$i++)
{
?>
<li class="active"><?php echo $i; ?></li>
<?php
}
}
?>
</ul>
</form>
<div class="col-sm-2"></div>
</div>
</div>
</body>
</html>
Pagiantion, it is working simple and easy
<?php
$sql = "SELECT COUNT(id) FROM contact_info";
$rs_result = $conn->query($sql);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
echo $total_records;
$previous = 1;
$total_pages = ceil($total_records / $limit);
$next = $_GET["page"] + 1;
$previous = $_GET["page"] - 1;
$pagLink = "<span>";
if($previous ==0)
{
$prev = "<a href='javascript:void(0);' >Previous</a>";
}
else
{
$prev = "<a href='http://homeacresfinefurniture.com/all-queries.php?page=".$previous."' style='color:black;'>Previous</a>";
};
echo $prev;
"</span><div class='pagination'>";
for ($i=1; $i<=$total_pages; $i++)
{
$pagLink .= "<a href='http://homeacresfinefurniture.com/all-queries.php?page=".$i."'>".$i."</a>";
};
echo $pagLink;
$nex = "<span><a href='http://homeacresfinefurniture.com/all-queries.php?page=".$next."' style='color:black;'>Next</a></span>";
echo $nex;
";
</div>";
?>
Get all data from database.
$limit = 1;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * $limit;
$sql = "SELECT * FROM contact_info ORDER BY id desc LIMIT $start_from , $limit";
$page = 1;
$limit = 10;
$offset = ($limit * $page) - $limit;
$query = mysqli_query(
$connect,
"SELECT * FROM Games WHERE category = '$code' limit $limit offset $offset"
);
<?php
include "includes/connection.php";
//$id=$_REQUEST['category'];
//$catid=mysql_escape_string($id);
$catid = isset($_GET['category']) ? (int)$_GET['category'] : 0;
$recordsPerPage =4;
# 0
// //default startup page
$pageNum = 1;
if(isset($_GET['p']))
{
$pageNum = $_GET['p'];
settype($pageNum, 'integer');
}
$offset = ($pageNum - 1) * $recordsPerPage;
//set the number of columns
$columns = 1;
//set the number of columns
$columns = 1;
$query = "SELECT temp_id, temp_img, temp_header, temp_resize, temp_small, temp_name, temp_type, cat_id, col_id, artist_id FROM `templates` where cat_id = '{$catid}' ORDER BY `temp_id` DESC LIMIT $offset, $recordsPerPage";
$result = mysql_query($query);
//we add this line because we need to know the number of rows
$num_rows = mysql_num_rows($result);
echo "<div>";
//changed this to a for loop so we can use the number of rows
for($i = 0; $i < $num_rows; $i++) {
while($row = mysql_fetch_array($result)){
if($i % $columns == 0) {
//if there is no remainder, we want to start a new row
echo "<div class='template'>";
}
echo ...........my data(s).
if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) {
echo "</div>";
}
}
}
echo "</div>";
//}
?>
<div class="pagination">
<?
$query = "SELECT COUNT( temp_id ) AS `temp_date` FROM `templates` where cat_id ='{$catid}'";
$result = mysql_query($query) or die('Mysql Err. 2');
$row = mysql_fetch_assoc($result);
$numrows = $row['temp_date'];
//$numrows = mysql_num_rows($result);
$self = $_SERVER['PHP_SELF'];
$maxPage = ceil($numrows/$recordsPerPage);
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{ if ($page == $pageNum)
{
$nav .= "<span class=\"pegination-selected\">$page</span>";
}
else
{
$nav .= "<aa class=\"pegination\" hreeef=\"javascript:htmlData('$self','p=$page')\">$page</a>";
}
}
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = "<aa class=\"pagination\" hreeef=\"javascript:htmlData('$self','p=$page')\"><strong><imgee src=\"images/previous.gif\" alt=\"previous\" width=\"5\" height=\"10\" border=\"0\"/></strong></a>";
$first = "<aa class=\"pagination\" hreeef=\"javascript:htmlData('$self','p=1')\"><strong><imgee src=\"images/previous1.gif\" alt=\"first\" width=\"7\" height=\"10\" border=\"0\" /></strong></a>";
}
else
{
$prev = '<strong> </strong>';
$first = '<strong> </strong>';
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = "<aa hreeef=\"javascript:htmlData('$self','p=$page')\"> <strong> <imgee src=\"images/next.gif\" alt=\"next\ width=\"5\" height=\"10\" border=\"0\" /></strong></a>";
$last = "<a class=\"pagination\" hreeef=\"javascript:htmlData('$self','p=$maxPage')\"> <strong> <imgee src=\"images/next1.gif\" alt=\"next\" border=\"0\" width=\"7\" height=\"10\" /></strong></a>";
}
else
{
$next = '<strong> </strong>';
$last = '<strong> </strong>';
}
echo "<div class=\"pagination\"> $first $prev <span class=\"pagination-selected\">$nav </span> $next $last </div>";
?>
Here my ajax code:
function GetXmlHttpObject(handler)
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtResult").innerHTML=xmlHttp.responseText
}
else
{
//alert(xmlHttp.status);
}
}
function htmlData(url, qStr)
{
if (url.length==0)
{
document.getElementById("txtResult").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
url=url+"?"+qStr;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true) ;
xmlHttp.send(null);
}
How can I get the selected category id after 1st page in pagination?
Do you pass though category in the request? You haven't given us that information (what is the value of qstr in the javascript?), but I'd guess not.
You're also passing it straight into an SQL query, which leaves you open to injection.
You should use mysql_escape_string() to fix that.
Post it with the AJAX call and return it
Store it in a local JS variable
Add it to the URL
...
You seem to be aware that $_GET['p'] gets the value of the 'p' parameter passed in the querystring. Well $_REQUEST['category'] is doing the same thing. (Technically $_REQUEST checked everything in $_POST, $_GET and $_COOKIE).
So if you haven't set the 'category' in the querystring then it wont contain anything in your code.
You should add ?category=XXX&sid=RAND... to your url
Better use $category = isset($_GET['category']) ? (int)$_GET['category'] : 0;
<?php
include ('database connection file ');
?>
**and this is my index.php**
<?php
if(isset($_GET['page']))
{
$page=$_GET['page'];
$offset=$limit * ($page - 1);
}
else{
$page=1;
$offset=0;
}
if($page==0 || $page > $page_rec){
header('location:index.php?page=1');
}
?>
<body>
<div>
<div class="headingSec"> <h1 align="center">Welcome AdminLogout</h1>
<p align="center">Manage your student database here.</p></div>
<table class="trBgColr">
<thead class="bgColorSec">
<th>Registration Id</th>
<th>Image</th>
<th>Signature</th>
<th>Name</th>
<th>Father's Name</th>
<th>City</th>
<th>Registration Category</th>
<th>Phone Number</th>
<th>Mobile Number</th>
<th>Status</th>
<th>
</thead>
<?php
//$getstudentdetails = "select * from student_details";
$result=mysqli_query($conn,"select * from `student_details` LIMIT $offset,$limit");
/* fetch associative array */
while($row = mysqli_fetch_array($qry)) {
?>
<tr>
<td><?php echo $row["registration_number"]; ?>
<td><?php echo $row["Name"]; ?></td>
<td><?php echo $row["Father_Name"]; ?></td>
<td><?php echo $row["City"]; ?></td>
<td><?php echo $row["Registration_Category"]; ?></td>
<td><?php echo $row["Phone_Number"]; ?></td>
<td><?php echo $row["Mobile_Number"]; ?></td>
<?php if($row['payment_status']==1)
{?>
<td><?php echo "Sucsess"; ?></td>
<?php
}
else{
?>
<td><?php echo "Failed"; ?></td>
<?php
}?>
<?php
}
$pre=$page - 1;
$next=$page + 1;
?>
</tr>
</table>
</div>
<br />
<br />
<div class="pagination">
<?php
for($i=1;$i<=$page_rec;$i++){
continue;
?>
<?php $i;?>
<?php } ?>
Previous«
Next»