how to create pagination with next and previous button? - php

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.

Related

MySQL PHP Search result with pagination [duplicate]

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
}
?>

PHP - MySQL query with Pagination

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"
);

Display 5 array items per row in a HTML table

I have an array of products and I need to display them 5 items per row. How can I make it, because now if i do
<?php foreach($data as $entries) : ?>
<td><?php echo $entries->name; ?>
<?php endforeach; ?>
it doesn't work. Should I make a counter?
Something like:
$data = range(1, 17);
for($count = 0; $count < count($data);)
{
echo "<tr>\n";
for($i = 0; $count < count($data) && $i < 5; $count++, $i++) {
echo "\t<td>$data[$count]</td>\n";
}
for(; $i < 5; $i++) {
echo "\t<td>-</td>\n";
}
echo "</tr>\n";
}
Output
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
<tr>
<td>11</td>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
</tr>
<tr>
<td>16</td>
<td>17</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
Perhaps a bit more readable?
<table>
<tr>
<?php
$data = range(1, 17);
$counter = 1;
foreach($data as $value){
if (!(($counter++ ) % 5)){
echo "<td>$value</td></tr><tr>";
}else{
echo "<td>$value</td>";
}
}
?>
</table>
Produces :
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17
Hope this helps
echo"<table>";
echo"<tr>";
for($i = 0; $i < count($productsArray); $i++){
if($i % 4 ==0){
echo"</tr><tr>"
}
echo "<td>$products[$i]; </td>";
}
Here's a simple counter that's also easy to modify if layout requirements change
<?php
//initialize a counter
$count = 0;
foreach($data as $entries){
//if it's divisible by 5 then echo a new row
if($count > 0 && $count % 5 == 0){
echo("</tr><tr>\n");
}
$count++;
?>
<td>
<?php
// We're also creating a new cell for each item
echo $entries->name; ?>
</td>
<?php
}
?>
<div class="container-fluid">
<?php
$count = 1;
foreach ($home_products as $pro_img) {
$parent_cat_link = base_url('category/view/'.$pro_img['category_id']);
if ($count % 6 == 1) {
$class="cap1";
$class1="cap";
?>
<div class="row">
<?php }
if($i %2 == 0){?>
<div class="col-md-2 col-sm-4 <?php if($count >=6){ echo $class; } else{ echo 'cap'; } ?> ">
<div class="">
<img src="<?php echo base_url() . 'uploads/category/' . $pro_img['image_name'] ?>" class="img-responsive" alt="logo">
</div>
</div><?php }else{ ?>
<div class="col-md-2 col-sm-4 <?php if($count >=6){ echo $class1; } else{ echo 'cap1'; } ?>">
<div class="">
<img src="<?php echo base_url() . 'uploads/category/' . $pro_img['image_name'] ?>" class="img-responsive" alt="logo">
</div>
</div><?php } $i++;
if ($count % 6 == 0) {
?> </div> <?php } $count++;
} if ($count % 6 != 1) echo "</div>"; ?>
</div>

showing 2nd page in a paginated table in a primary content div in php

Good Evening/morning,
I have a table which is paginated. The first page shows up in a primarycontent div, which is where i want it..however when clicking on page 2, 3..etch it opens up in a different link, I want it to open up in the primary content div like the first page, but can't figure out how to do it. Also when clicking the previous button it goes to page 1, instead of the page before the current page..ex on page 50, goes to page 1. Here is my code.
<?php
require_once ('mysqli_connect.php');
$display = 30;
if (isset($_GET['p']) && is_numeric ($_GET['p']))
{
$pages = $_GET['p'];
} else {
$q = "SELECT COUNT(NewCustomerID) FROM customer";
$r = #mysqli_query($dbc, $q);
$row = #mysqli_fetch_array ($r, MYSQLI_NUM);
$records = $row[0];
if ($records > $display){//more than 1 page.
$pages = ceil($records/$display);
} else {
$pages = 1;
}
} // end of p IF
if (isset($_GET['s']) && is_numeric ($_GET['s']))
{
$start = $_GET['s'];
} else {
$start = 0;
}
$q = "SELECT(NewCustomerID) AS customerid,
(OldCustomerID) AS oldcustomerid,
(FirstName) AS FirstName,
(MiddleName) AS MiddleName,
(LastName) AS LastName,
(UserName) AS UserName,
(CarID) AS CarID,
(CarColorID) AS CarColorID,
(ComputerID) AS ComputerID,
(IsLaptop) AS LaptopID,
(RaceID) AS RaceID,
(ResidenceID) AS ResidenceID,
(BirthMonthID) AS BirthMonthID
FROM customer ORDER BY LastName ASC LIMIT $start, $display";
$r = #mysqli_query($dbc, $q); if(!$r){die(mysqli_error($dbc));}
Echo '<table>
<tr>
<td><b>NewCustomerID </b></td>
<td><b>OldCustomerID </b></td>
<td><b>FirstName </b></td>
<td><b>MiddleName </b></td>
<td><b>LastName </b></td>
<td><b>UserName </b></td>
<td><b>CarID </b></td>
<td><b>CarColorID </b></td>
<td><b>ComputerID </b></td>
<td><b>IsLaptop </b></td>
<td><b>RaceID </b></td>
<td><b>ResidenceID </b></td>
<td><b>BirthMonthID </b></td>
</tr>';
$bg = '#eeeeee'; // set initial back ground color
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$bg = ($bg =='#eeeeee' ? '#ffffff' : '#eeeeee'); // switch the background color.
echo '<tr bgcolor="' . $bg . '">
<td>' . $row['customerid'] . '</td>
<td>' . $row['oldcustomerid']. '</td>
<td>' . $row['FirstName']. '</td>
<td>' . $row['MiddleName']. '</td>
<td>' . $row['LastName']. '</td>
<td>' . $row['UserName'].'</td>
<td>' . $row['CarID'].'</td>
<td>' . $row['CarColorID'].'</td>
<td>' . $row['BirthMonthID'].'</td>
<td>' . $row['ComputerID'].'</td>
<td>' . $row['LaptopID'].'</td>
<td>' . $row['RaceID'].'</td>
<td>' . $row['ResidenceID'].'</td>
</tr>';
} // end of while loop
echo '</table>';
mysqli_free_result($r);
mysqli_close($dbc);
// make the links to the other pages if necessary
if ($pages >1) {
// add some spaces and start a paragraph
echo '<br /> <p>';
// determine what page the script is on:
$current_page = ($start/$display)+1;
// if it's not the first page, make a previous link;
if ($current_page !=1) {
echo 'Previous ';
}
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
echo '<a href="#" ' . (($display * ($i - 1))) . '&p=' . $pages . '">'
. $i . '</a> ';
}else{
echo $i . ' ';
}
}// end of FOR loop
// if it is not the last page, make a next button
if ($current_page != $pages) {
echo 'Next';
}
echo '</p>'; // close the paragraph
} // endo of links section
?>
Save yourself a ton of time and use a pre-engineered grid. My grid of choice is DataTables It's got all the bugs and browser inconsistencies taken care of already, and paging is as simple as one line of code. If you want to get really simple, you could output your entire table and let datatables sort out the paging, as well as sorting, filtering, etc. If you're like me and have to deal with a lot of results, you can also load data via ajax and have datatables actually assemble the dom. Either way, it's quicker than writing your own.
First of all, you should pass in GET/POST parameter which will carry the page to show. After you grab it from there, fro example in variable $page, you can use it SQL query with keyword OFFSET and LIMIT, like LIMIT 10 OFFSET 2, that gives you exactly rows from 11 to 20 items numbers.
Hope that's exactly what you need.
EDITED
<?php
$route = '/index.php?p='; // where to go on click page
$total = ($records % $display) != 0 ? floor($records / $display) + 1 : $records / $display; // total pages count
$current = isset($_GET['p']) ? $_GET['p'] : 1; // current page number
$last = 1; // init value of the last page
?>
<p class="pager">
<?php if ($total <= 9): ?>
<?php for ($i = 1; $i <= $total; $i++): ?>
<a href="<?php echo $route, $i; ?>" <?php if ($current == $i) echo 'class="acitve"'; ?>><?php echo $i; ?></a>
<?php endfor; ?>
<?php else: ?>
<?php
$rbound = $current + 1;
$lbound = $current - 1;
if ($lbound < 1)
$lbound = 1;
if ($rbound > $total)
$rbound = $total;
for ($i = 1; $i <= 3; $i++):
?>
<a href="<?php echo $route, $i; ?>" <?php if ($current == $i) echo 'class="acitve"'; ?>><?php echo $i; ?></a>
<?php $last = $i; ?>
<?php endfor; ?>
<?php if ($lbound <= $last): ?>
<?php for ($i; $i <= $rbound; $i++): ?>
<a href="<?php echo $route, $i; ?>" <?php if ($current == $i) echo 'class="acitve"'; ?>><?php echo $i; ?></a>
<?php $last = $i; ?>
<?php endfor; ?>
<?php else: ?>
<?php echo $lbound == $last + 1 ? '' : '...'; ?>
<?php endif; ?>
<?php if ($lbound > 3 && $rbound < $total - 2): ?>
<?php for ($i = $lbound; $i <= $rbound; $i++): ?>
<a href="<?php echo $route, $i; ?>" <?php if ($current == $i) echo 'class="acitve"'; ?>><?php echo $i; ?></a>
<?php $last = $i; ?>
<?php endfor; ?>
<?php endif; ?>
<?php if ($rbound >= $total - 2): ?>
<?php for ($i = $lbound; $i < $total - 2; $i++): ?>
<a href="<?php echo $route, $i; ?>" <?php if ($current == $i) echo 'class="acitve"'; ?>><?php echo $i; ?></a>
<?php $last = $i; ?>
<?php endfor; ?>
<?php $last = $last > $total - 4 ? $last : $total - 2; ?>
<?php else: ?>
<?php echo $rbound == $total - 3 ? '' : '...'; ?>
<?php $last = $total - 2; ?>
<?php endif; ?>
<?php for ($i = $total - 2; $i <= $total; $i++): ?>
<a href="<?php echo $route, $i; ?>" <?php if ($current == $i) echo 'class="acitve"'; ?>><?php echo $i; ?></a>
<?php endfor; ?>
<?php endif; ?>
</p>
That's all in above gives you pager rendered as: 1 2 3 ... 12 13 14 ... 18 19 20, here current page is 13, so it shows first 3 pages number, current page +/- 1 page and last 3 page numbers. So for the first page it would be: 1 2 3 ... 18 19 20, same for the last. Also adds CSS class active to the current page link.

How can I get the selected category id after 1st page in pagination?

<?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»

Categories