I want to use pagination on sql fetched row. I am using this code:
<?php
$rec_limit = 4;
$sql = "SELECT count(id) FROM wp_wct8 ";
$retval = mysql_query( $sql);
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}else {
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT id, name, insurance ".
"FROM wp_wct8 ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql);
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while ($b = mysql_fetch_array($retval)){
echo $b['name'];
echo $b['insurance'];
}
if( $page > 0 ) {
$last = $page - 2;
echo "Last 4 Records |";
echo "Next 4 Records";
}else if( $page == 0 ) {
echo "Next 4 Records";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "Last 4 Records";
}
?>
but pagination not working... when i click on show next 4 records it is showing same records not next..please helpme... thanks in advance
I don't recommend you to put this code online as it's vulnerable to SQL injection. (Use prepared statements with PDO instead or at least, escape your input in the SQL query.)
I assume the error are the whitespaces in your links. So try removing the whitespaces, e.g. echo "Last 4 Records |";
Related
I want to display five record per page through pagination (mysql,php,html,css) until all the records are displayed, navigation to pages must be like, Page: 1 2 3 4 5 6 7 7 8... Last.
HERE IS MY CODE TO VIEW ALL THE RECORDS FROM emp_master table.
I am new to PHP so please write an easily understandable code for pagination. I have seen few examples but they are not working.
<?php
$con=mysqli_connect("localhost","user","password","dataplus");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM emp_master");
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
I want to display five record per page through pagination untill all the records are displayed, navigation to pages must be like, Page: 1 2 3 4 5 6 7 7 8... Last.
This code below is not working:
$dbhost="localhost";
$dbuser="10053";
$dbpass="n6867242";
$database="0368";
$rec_limit = 10;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('1005368');
/* Get total number of records */
$sql = "SELECT count(emp_id) FROM emp_master ";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}else {
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT emp_id, emp_name, e_mail ".
"FROM emp_master ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "EMP ID :{$row['emp_id']} <br> ".
"EMP NAME : {$row['emp_name']} <br> ".
"EMP MAIL : {$row['e_mail']} <br> ".
"--------------------------------<br>";
}
if( $page > 0 ) {
$last = $page - 2;
echo "Last 10 Records |";
echo "Next 10 Records";
}else if( $page == 0 ) {
echo "Next 10 Records";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "Last 10 Records";
}
mysql_close($conn);
If you want just PHP MySQL code, I usually use something like the following.
$page=max(intval($_GET['page']),1); // assuming there is a parameter 'page'
$itemsperpage = 5;
$total=100; // total results if you know it already otherwise use another query
$totalpages = max(ceil($total/$itemsperpage),1);
$query = "SELECT * FROM emp_master LIMIT ".(($page-1)*$itemsperpage).",".$itemsperpage; // this will return 5 items based on the page
You can use a library called dataTables that will auto paginate the data and you can easily customize it to suit your needs with the look and how many records to show per page. It also has a search field which enables users to search your data without any extra code.
All you have to do is include a couple of cdn's and the following code and everything works fine
$(document).ready(function(){
$('#myTable').DataTable();
});
Some examples here
you just should remove spaces in href and also make $page = $page + 1 when page is 0
if( $page > 0 ) {
$last = $page - 2;
echo "Last 10 Records |";
echo "Next 10 Records";
}else if( $page == 0 ) {
$page = $page + 1;
echo "Next 10 Records";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "Last 10 Records";
}
Link is here for demo, click here to experience the result
Here is the code working for me, just change your db name, username and password
and get pagination done. You can change $rec_limit value to your desired no. of records per page.
<?php
$host="localhost";
$username="68";
$password="67242";
$database="68";
$rec_limit = 5;
$conn = mysql_connect($localhost,$username,$password);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('10');
/* Get total number of records */
$sql = "SELECT count(emp_id) FROM emp_master ";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}else {
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT eid,ename, email, quali, gender, contactno, birthdate, joiningdate,CURDATE(), TIMESTAMPDIFF( YEAR, birthdate, CURDATE( ) ) AS age ".
"FROM emp_master ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
echo "<table border='1'>";
$i = 0;
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
if( $page > 0 ) {
$last = $page - 2;
echo "Last 5 Records | ";
echo "Next 5 Records";
}else if( $page == 0 ) {
$page = $page + 0;
echo "Next 5 Records";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "Last 5 Records";
}
mysql_close($conn);
php?>
IF your are using mysqli the code is below
$conn=mysqli_connect("localhost","root","","ui");
$start=0;
$limit=5;
$t=mysqli_query($conn,"select * from form_table");
$total=mysqli_num_rows($t);
if(isset($_GET['id']))
{
$id=$_GET['id'] ;
$start=($id-1)*$limit;
}
else
{
$id=1;
}
$page=ceil($total/$limit);
$query=mysqli_query($conn,"select * from form_table limit $start, $limit");
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script s src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> </script>
</head>
<body>
<div class="container">
<h2>Table</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Gender</th>
<th>Hobbies</th>
<th>Course</th>
</tr>
</thead>
<tbody>
<?php
while($ft=mysqli_fetch_array($query))
{?>
<tr>
<td><?= $ft['0']?></td>
<td><?= $ft['1']?></td>
<td><?= $ft['2']?></td>
<td><?= $ft['3']?></td>
<td><?= $ft['4']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<ul class="pagination">
<?php if($id > 1) {?> <li>Previous</li><?php }?>
<?php
for($i=1;$i <= $page;$i++){
?>
<li><?php echo $i;?></li>
<?php
}
?>
<?php if($id!=$page)
{?>
i Give you an example of my project of pagination. Just you have to put your values in the code
$sql="SELECT * FROM tblname LIMIT $next,5";
$results = mysqli_query($conn,$sql);
}
else if (isset($_POST['prev']))
{
$prev1=$_POST['prev'];
$next=$_POST['prev'];
$prev=$prev1;
$sql="SELECT * FROM tablename LIMIT $prev,5";
$prev=$prev1;
$results = mysqli_query($conn,"SELECT * FROM tablename LIMIT $prev,10");
if($prev==0)
{
$prev = 0;
}
else
{
$prev=$next-10;
}
}
else
{
$next=0;
$prev=0;
$results = mysqli_query($conn,"SELECT * FROM tablename LIMIT $next,10");
}
I am trying to implement pagination using PHP and MySQL by using the following code .
I have taken help from internet and changed the code but not able to get the value of page variable so code is not working effectively
Can you suggest me where is the error in the program or how can i get the page variable value. The program is just simple to implement the pagination using PHP and MYSQL db.
code is as following.
<html>
<head>
<title>Paging Using PHP</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root123';
$rec_limit = 10;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('test_db');
/* Get total number of records */
$sql = "SELECT count(cinno) FROM register_data ";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}else {
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT cinno ".
"FROM register_data ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "Cin No :{$row['cinno']} <br> ";
}
echo "$page";
if( $page > 0 ) {
$last = $page - 2;
echo "Last 10 Records |";
echo "Next 10 Records";
}else if( $page == 0 ) {
echo "Next 10 Records";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "Last 10 Records";
}
mysql_close($conn);
?>
</body>
</html>
I have cheked last page condition by finding $total_page and then comparing $page == $total_page. $left_rec method is not fit all the time.
// $left_rec = $rec_count - ($page * $rec_limit); not always give correct result
$total_page = ceil($rec_count/$rec_limit);
ceil() is a php function which returns the next highest integer value by rounding up. for more info read http://php.net/manual/en/function.ceil.php
Also you have spaces in your query string thats why you can't get page value and you need not to use $_PHP_SELF you can simply write
echo "<a href = '?page=$page'>Next 10 Records</a>"; or echo "<a href = 'index.php?page=$page'>Next 10 Records</a>"; if the code is in index.php
// use full code this
<html>
<head>
<title>Paging Using PHP</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root123';
$rec_limit = 10;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('test_db');
/* Get total number of records */
$sql = "SELECT count(cinno) FROM register_data";
$retval = mysql_query( $sql, $conn );
if(!$retval ) {
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
if(isset($_GET['page'] ) ) {
$page = $_GET['page'];
$offset = $rec_limit * ($page - 1);
}else {
$page = 1;
$offset = 0;
}
// $left_rec = $rec_count - ($page * $rec_limit); not always give correct result
$total_page = ceil($rec_count/$rec_limit);
$sql = "select cinno from register_data LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data2: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "Cin No :{$row['cinno']} <br> ";
}
if( $page >1 && $page != $total_page) {
$last = $page - 1;
$page++;
echo "<a href = '?page=$last'>Last 10 Records</a> |";
echo "<a href = '?page=$page'>Next 10 Records</a>";
}else if( $page == 1 ) {
$page++;
echo "<a href = '?page=$page'>Next 10 Records</a>";
}elseif($page == $total_page){
$last = $page - 1;
echo "<a href = '?page=$last'>Last 10 Records</a>";
}
mysql_close($conn);
?>
</body>
</html>
Also please use mysqli or PDO(more secure);
$_PHP_SELF sounds incorrect; you should use $_SERVER['PHP_SELF'] or in in strings ${_SERVER['PHP_SELF']}
The first page should be 1 but its -1 and the next page should be 2 but its 0.
How can I make this work? I have tried to edit the $page but when I do, the if/else get wierd.
I added the whole code...
...
$resultt= "SELECT * FROM users WHERE AND country='$okc' AND state='$oks'";
if(!empty($a1) && empty($a2)){
$resultt .= " AND aar>=";
...
}
There are more than 1 if statement.
$result = mysqli_query($con,"$resultt");
$rec_limit = 19;
$row = mysqli_fetch_array($result);
$rec_count = $row[0];
if( isset($_GET['page'] ) )
{
$page = $_GET['page'] + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$resultt .= "LIMIT $offset, $rec_limit";
$sql = $resultt;
$retval = mysqli_query( $con, "$resultt" );
if( $page > 0 )
{
$last = $page - 1;
echo "Last 10 Records |";
echo "Next 10 Records";
}
else if( $page == 0 )
{
echo "Next 10 Records";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "Last 10 Records";
}
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
}
You should change parts of code to get it to work. $_GET['page'] should give you the queried page number itself, so don't try to manipulate it.
$sql = "SELECT * FROM users WHERE country='$okc' AND state='$oks'"
$rec_limit = 10;
$result = mysqli_query( $con, $sql );
$row = mysqli_fetch_array( $result );
$rec_count = $row[0];
$left_rec = $rec_count - ( $rec_limit * $page );
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$offset = $rec_limit * ( $page - 1 ) ;
$sql.= " LIMIT $rec_limit OFFSET $offset";
$result = mysqli_query( $con, $sql );
$row = mysqli_fetch_array( $result );
if( $page > 1 ){
$last = $page - 1;
echo "Last 10 Records |";
echo "Next 10 Records";
}else if( $page == 1 ){
echo "Next 10 Records";
}else if( $left_rec < $rec_limit ){
$last = $page - 2;
echo "Last 10 Records";
}
while( $row = mysqli_fetch_array( $result, MYSQL_ASSOC ) ){
// use $row to echo row contents ...
}
$_GET is an array like your $row.
to access array keys you always use [].
change your get into $_GET['page']
even better:
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
is page set then convert value to int, because you dont know whats incomming.
if not set take page 1.
btw. constructs like this:
$resultt = "LIMIT $offset, $rec_limit";
can/will cause sql injections bugs within you homepage!
never build a sql with variables!
Use parameters instead!
I've the following Pagination code :
$rec_limit = 3;
/* Get total number of records */
$query = "SELECT count(id) FROM news";
$result = mysql_query($query);
if(!$result)
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($result, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$query2 = "SELECT * ".
"FROM news ".
"LIMIT $offset, $rec_limit";
$result2 = mysql_query( $query2 );
if(! $result2 )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($result2, MYSQL_ASSOC))
{
echo " ID :{$row['id']} <br> ".
" News : {$row['title']} <br> ".
" Date : {$row['date']} <br> ".
"--------------------------------<br>";
}
if( $page > 0 )
{
$last = $page - 2;
echo "Last 3 Records |";
echo "Next 3 Records";
}
else if( $page == 0 )
{
echo "Next 3 Records";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "Last 3 Records";
}
The code works perfectly as needed, except that the a href part where it shows the Next 3 records link even when there is no news left. By that i mean, it keeps opening up pages with empty records ! How can i remove Next 3 Records link when it reaches the last news ID.
Thank You
Just bring the last condition above the other two and change < to <=:
if($left_rec <= $rec_limit)
{
$last = $page - 2;
echo "Last 3 Records";
} else if ( $page > 0 )
{
$last = $page - 2;
echo "Last 3 Records |";
echo "Next 3 Records";
}
else if( $page == 0 )
{
echo "Next 3 Records";
}
Im looking to add a simple pagination, but after changing from msql to mysqli i cant seem to get it right. I cant get my head around it yet, im just starting with it.
I followed the tutorial but still im getting errors.
sorry for my lack of knowledge but what am i missing?
<?php
$db = new mysqli("host", "username", "password", "mydatabase");
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$rec_limit = 10;
$sql = "SELECT COUNT(photo)FROM employees";
$retval = mysql_query( $sql, $db );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT photo, link".
"FROM employees ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $db );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
echo "<br /><br />";
echo '','<img src="/upload/' . $row->photo . '" border=0>';
echo '', $row->link;
if( $page > 0 )
{
$last = $page - 2;
echo "Last 10 Records |";
echo "Next 10 Records";
}
else if( $page == 0 )
{
echo "Next 10 Records";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "Last 10 Records";
}
mysql_close($db);
?>
You are mixing mysqli_* and mysql_* functions.
Please choose which style you are going to use: the procedural style (mysqli_connect, mysqli_query) or the object style (new MySQLi, ect.)
You can't mix those 2 and you can't also mix 2 different libraries (mysql and mysqli)
Read more about the different styles on PHP.net (the examples also show how to use mysqli)
$sql = "SELECT COUNT(photo)FROM employees";
^--- missing a space here
and
$sql = "SELECT photo, link".
^--- missing space here
"FROM employees ".
if this code is from a tutorial, I suggest you ditch it. Your code has absolutely not error handling, and simply assumes everything succeeded. A properly written tutorial would have proper error detection/handling. And also suggest NOT musing the mysql_*() functions, as they're deprecated/obsolete.