im doing pagination for my table where there are around 1500 rows.
$numperpage = 50;
$countsql = $connect->prepare("select COUNT(id) from prana");
$countsql->execute();
$row = $countsql->fetch();
$numrecords = $row[0];
$numlinks = ceil($numrecords/$numperpage);
$page = $_GET['start'];
if (!$page) $page = 0;
$start = $page * $numperpage;
echo "start is ".$start.'<br>';
if(isset($_POST["action"]))
{
$query = "
SELECT * FROM prana WHERE product_status = '1' limit $start,$numperpage";
echo $output;
for ($i=0;$i<$numlinks;$i++)
{
$y = $i+1;
echo ' '.$y.' ';
}
from <a href="index.php?start='.$i.'"> im getting value of start in my url but for $page = $_GET['start'] it is showing Undefined array key start . and start variable stays at 0.
THIS IS MY REFERENCE :- https://www.youtube.com/watch?v=TI78ax23qZg
please help.
I hope this is the thing you are looking for, here is an working example and in details explanation of this example,
I have attached the code and Link for better understanding, have a look
have a nice day
https://www.myprogrammingtutorials.com/create-pagination-with-php-and-mysql.html
<html>
<head>
<title>Pagination</title>
<!-- Bootstrap CDN -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<?php
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
$conn=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$total_pages_sql = "SELECT COUNT(*) FROM table";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
$sql = "SELECT * FROM table LIMIT $offset, $no_of_records_per_page";
$res_data = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($res_data)){
//here goes the data
}
mysqli_close($conn);
?>
<ul class="pagination">
<li>First</li>
<li class="<?php if($pageno <= 1){ echo 'disabled'; } ?>">
Prev
</li>
<li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
Next
</li>
<li>Last</li>
</ul>
</body>
</html>
To get rid of the warning, change your code from:
$page = $_GET['start'];
if (!$page) $page = 0;
Into:
$page = isset($_REQUEST['start']) ? $_REQUEST['start'] + 0 : 0;
Where $_REQUEST['start'] is used instead of $_GET['start'] to ensure logic works, even if method was not GET.
And the "+ 0" is to ensure it's numeric.
Also, start param is not set until one of links is clicked.
Related
so I'm having some issues with a pagination script I have been building The pagination works fine however when i load a new page using the next button, my search term $search_term = "%" . $_POST['searchBar'] . "%"; is lost. is there any way to avoid this? or set the search term as a set value?
the url for the next page is like so - http://examplewebsite.com/user/Courses/SearchResultsPage.php?pn=2
Any help with this would be greatly appreciated.
The rest of the pagination script is below;
<?php
$mysqli = new mysqli('localhost', 'user', 'password','db');
if ($mysqli->connect_errno)
{
die('Database connection failed');
}
//$m->set_charset('utf8');
//here are my main changes
//turn errors on to develop, back off when you go live
error_reporting(E_ALL);
ini_set('display_errors', 1);
$search_term = "%" . $_POST['searchBar'] . "%";
$search_param = $_SESSION['$search_term']=$search_term;
$stmt =$mysqli->prepare("SELECT title, summary, id FROM course WHERE title
LIKE ?");
$stmt->bind_param("s", $search_param); //learn this
$stmt->execute();
$result = $stmt->get_result();
//This gets the number of rows in a query result
$rows = $result->num_rows;
//number of results per page
$rows_per_page = 10;
//shows last page
$last_page = ceil($rows/$rows_per_page);
if($last_page < 1){
$last_page = 1;
}
if(isset($_GET['pn'])){
$page_number = preg_replace('#[^0-9]#', '', $_GET['pn']);
} else {
$page_number = 1;
}
//makes sure page number is between limits of $page_number
if($page_number < 1){
$page_number = 1;
} else if($page_number > $last_page){
$page_number = $last_page;
}
// sets the value of items to view
$limit = 'LIMIT ' .($page_number -1) * $rows_per_page .',' .$rows_per_page;
//displays to the user the total number of results and the page numbers
$total_number_of_results = "Search Results (<b>$rows</b>)";
$page_user_is_on = "Page <b>$page_number</b> of <b>$last_page</b>";
//query again only grabbing the set number of rows depending on page number
$stmt = $mysqli->prepare("SELECT title, summary, id FROM course WHERE title LIKE ? ".$limit);
$stmt->bind_param("s", $search_param);
$stmt->execute();
$result = $stmt->get_result();
$list = '';
while($row = $result->fetch_assoc()){
$title = $row['title'];
$id = $row['id'];
//I'm assuming you want each link to be different here...
$list.='<p>' . $title . '</p>';
}
mysqli_close($mysqli);
//set up pagination
$pagination_controls = '';
if($last_page != 1){
if($page_number > 1){
$previous = $page_number - 1;
$pagination_controls .='previous ';
for($i = $page_number - 4; $i < $page_number; $i++)
{
if($i > 0){
$pagination_controls .= ''.$i.' ';
}
}
}
$pagination_controls.=''.$page_number.' ';
//clickable links to the left
for($i = $page_number+1; $i <= $last_page; $i++)
{
$pagination_controls .= ''.$i.' ';
if($i >= $page_number+4){
break;
}
}
if($page_number != $last_page){
$next = $page_number + 1;
$pagination_controls.=' Next';
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel='stylesheet' href='courses.css'>
</head>
<body>
<div class="header">
<h1>Search Results for - <?= $search_param ?></h1>
</div>
<div>
<h3> <?php echo $page_user_is_on ?> </h3>
<p><?php echo $list; ?></p>
<p><?php /* echo $search_result['summary']; //Where was this coming from? */?> </p>
</div>
<div id="pagination_controls"><?php echo $pagination_controls; ?></div>
</body>
</html>
Every time you click next, the page refreshes and since you dont use
<?php
session_start();
....
Replace these 2 lines
$search_term = "%" . $_POST['searchBar'] . "%";
$search_param = $_SESSION['$search_term']=$search_term;
with:
if(isset( $_POST['searchBar'])){
$search_term = "%" . $_POST['searchBar'] . "%";
$search_param = $_SESSION['search'] = $search_term ;
}
else {
$search_param = $_SESSION['search'];
}
at the top,$_SESSION['$search_term'] is lost
Also add an isset check for your POST
EDIT
I think your problem is that $search_term is set only on load then on the next page it is NULL and session is also set to NULL.
You guys are helping me greatly. Please I have created a members page which I want to use pagination but it shows only one member's id in all ids.
Here is an image:
Here is my code:
<html>
<head>
<title>MEMBERS</title>
<link rel="icon" href="guyt.gif" type="image/x-icon">
<link rel="stylesheet" type="text/css" media="all" href="style.css">
</head>
<body>
<?php include 'connect.php'; ?>
<?php include 'functions.php'; ?>
<?php include 'header.php'; ?>
<div id="sidebar">
<br>
<?php include 'lists.php'; ?>
</div>
</body>
</html>
here is the lists.php code which contains the pagination:
<?php
$per_page = 5;
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;
$mem_query = mysql_query("SELECT id FROM register");
while($run_mem = mysql_fetch_array($mem_query)){
$id = $run_mem['id'];
$first = getuser($id, 'first');
$last = getuser($id, 'last');
}
$sql = "SELECT * FROM register";
$num_rows = mysql_num_rows(mysql_query($sql));
$num_pages = ceil($num_rows / $per_page);
$sql .= " LIMIT $start, $per_page";
$result = mysql_query($sql);
While($row = mysql_fetch_array($result)){
echo "<div class='header'><table width='98%'><tr><td><div align='left'>$first $last</div></td><td><div align='right'><a href='profile.php?user=$id'>VISIT PROFILE</a></div></td></tr></table></div><br>";
}
$prev = $page - 1;
$next = $page + 1;
echo "<hr>";
if($prev > 0)
echo "<a href='?page=$prev' class='box'>Previous</a></font> ";
if($page < ceil($num_rows/$per_page))
echo " <a href='?page=$next' class='box'>Next</a></font>";
?>
if I change this:
$sql = "SELECT * FROM register";
to this:
$sql = "SELECT * FROM register WHERE id='$id'";
it will display like that same user but just once. please help me
You should output the property of each row you are looping in :
While($row = mysql_fetch_array($result)){
echo "<div class='header'><table width='98%'><tr><td><div align='left'>" . $row['name'] . "</div></td><td><div align='right'><a href='profile.php?user=$id'>VISIT PROFILE</a></div></td></tr></table></div><br>";
}
That's assuming the name is stored in a "name" key. Adapt it to your db structure.
please i need your help greatly. I am creating a page that will have all the lists of the registered members using pagination, but when i click on any user it takes me to only the 'profile.php?user=3' instead of the user's id profile if you understand what i mean, please show me what i did wrong in my coding:
<html>
<head>
<title>MEMBERS</title>
<link rel="icon" href="guyt.gif" type="image/x-icon">
<link rel="stylesheet" type="text/css" media="all" href="style.css">
</head>
<body>
<?php include 'connect.php'; ?>
<?php include 'functions.php'; ?>
<?php include 'header.php'; ?>
<div id="sidebar">
<br>
<?php include 'lists.php'; ?>
</div>
</body>
</html>
here is the lists.php which contains the pagination:
<?php
$per_page = 5;
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;
$mem_query = mysql_query("SELECT id FROM register");
while($run_mem = mysql_fetch_array($mem_query)){
$id = $run_mem['id'];
$first = getuser($id, 'first');
$last = getuser($id, 'last');
}
$sql = "SELECT * FROM register";
$num_rows = mysql_num_rows(mysql_query($sql));
$num_pages = ceil($num_rows / $per_page);
$sql .= " LIMIT $start, $per_page";
$result = mysql_query($sql);
While($row = mysql_fetch_array($result)){
echo "<div class='header'><table width='98%'><tr><td><div align='left'>" . $row['first'] . " " . $row['last'] . "</div></td><td><div align='right'><a href='profile.php?user=$id'>VISIT PROFILE</a></div></td></tr></table></div><br>";
}
$prev = $page - 1;
$next = $page + 1;
echo "<hr>";
if($prev > 0)
echo "<a href='?page=$prev' class='box'>Previous</a></font> ";
if($page < ceil($num_rows/$per_page))
echo " <a href='?page=$next' class='box'>Next</a></font>";
?>
Please look at your code critically,you are generating the id within the first loop and assigning the value in the second loop. That is your problem,because after the execution of first loop the variable id will be assigned to the last member id which is basically 3. It will also be nice to show the profile.php.
Solution
change your url to this
<a href='profile.php?user='.$row['id']>VISIT PROFILE</a>
Im trying to make a pagination to my article page.
the problem is i want it to count the articles WHERE category = 1
ive worked with the script for sometime, but it still just shows a blank page no errors.
any suggestions why it wont work?
<?php
error_reporting(E_ALL); ini_set("display_errors", 1);
$db = mysql_connect("localhost", "root", "");
mysql_select_db("dirts_mysql", $db);
echo "<h1>articles</h1>";
$pr_page = 2;
$number = mysql_result(mysql_query("SELECT COUNT(*) FROM article WHERE category = 1"), 0) or die(mysql_error());
$show_from = (isset($_GET["visfra"]) && is_numeric($_GET["visfra"]) && $_GET["visfra"] < $number) ? $_GET["visfra"] : 0;
$query = mysql_query("SELECT * FROM article ORDER BY id DESC limit $show_from, $pr_page") or die(mysql_error());
while ($row = mysql_fetch_array($query)) {
?>
<link rel="stylesheet" type="text/css" href="style.css">
<div id="news">
<h2><u><? echo $row['name']; ?></u></h2>
<p>
<?php echo nl2br($row['description']); ?>...
</p>
<br/><br/>
[...]
</div>
<br>
<?php
}
if ($show_from > 0) {
$back = $show_from - $pr_page;
echo "<a href='?showfrom=$back'>Forrige</a> ";
}
$page = 1;
for ($start = 0; $number > $start; $start = $start + $pr_page) {
if ($show_from != $page * $pr_page - $pr_page) {
echo "<a href='?showfrom=$start'>$page</a> ";
} else {
echo $page . " ";
}
$page++;
}
if ($show_from < $number - $pr_page) {
$next = $show_from + $pr_page;
echo " <a href='?&showfrom=$next'>Næste</a>";
}
?>
add the following on the top of your code:
error_reporting(E_ALL);
ini_set("display_errors", 1);
On the otherside, I will suggest you to start using function to structure your code. :)
I've searched through the web and can't really find what's the problem with my code. The next and prev links are not working. it only gets the first entry.
Hope you guys can help with this! Thanks.
<?php
// ROWS DISPLAYED PER PAGE
$rows_per_page = 1;
// GET PAGE NUMBER
$page = $_GET['page'];
$offset = (!empty($page)) ? $page : $page = 1;
// URL CLEAN UP
$self = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
$self = str_replace("page={$offset}", "", $self);
// GET LIST OF STATES
$offset = ($page) ? ($page - 1) * $rows_per_page : 0;
$id = $_GET['id'];
$sql = "SELECT * FROM updates WHERE update_categoryID = '$id' ORDER BY update_date DESC LIMIT {$offset},{$rows_per_page}";
$result = mysql_query($sql)or die('Error, query failed');
// GET NUMBER OF PAGES
$query1 = "SELECT * FROM updates WHERE update_categoryID = '$id'";
$result1 = mysql_query($query1)or die('Error, query failed');
$total = mysql_num_rows($result1);
$NumPgs = ceil($total/$rows_per_page);
while($row = mysql_fetch_assoc($result))
{
?>
<h2><?php echo $row['update_title']; ?></h2>
<p class="datetime"><?php echo $row['update_date'];?></p>
<br>
<p class="post"><?php echo $row['update_content'];?></p>
Post a Comment
<?php
}
?>
<span style="float:right">
<? if ($NumPgs > 0 && $page!=1) {
echo "<<Prev "; } ?>
[Page <?php echo $page; ?>]
<? if ($page < $NumPgs) {
echo " Next>>"; } ?>
<b><? echo $offset+1;?> to <? echo $offset+$rows_per_page;?>, of <? echo $total; ?> Entries</b>
</span>
</div>
Uhm maybe your page param is appended to the query string, try this :
$self = $_SERVER['PHP_SELF']."?".
preg_replace( $_SERVER[ 'QUERY_STRING' ], 'page=[0-9]+', '');
instead of :
// URL CLEAN UP
$self = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
$self = str_replace("page={$offset}", "", $self);
Short open tags are not working in your case.
http://us3.php.net/echo
http://us3.php.net/manual/en/ini.core.php#ini.short-open-tag
Try the following code.
<?php
// ROWS DISPLAYED PER PAGE
$rows_per_page = 1;
// GET PAGE NUMBER
$page = $_GET['page'];
$offset = (!empty($page)) ? $page : $page = 1;
// URL CLEAN UP
$self = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
$self = str_replace("page={$offset}", "", $self);
// GET LIST OF STATES
$offset = ($page) ? ($page - 1) * $rows_per_page : 0;
$id = $_GET['id'];
$sql = "SELECT * FROM updates WHERE update_categoryID = '$id' ORDER BY update_date DESC LIMIT {$offset},{$rows_per_page}";
$result = mysql_query($sql)or die('Error, query failed');
// GET NUMBER OF PAGES
$query1 = "SELECT * FROM updates WHERE update_categoryID = '$id'";
$result1 = mysql_query($query1)or die('Error, query failed');
$total = mysql_num_rows($result1);
$NumPgs = ceil($total/$rows_per_page);
while($row = mysql_fetch_assoc($result))
{
?>
<h2><?php echo $row['update_title']; ?></h2>
<p class="datetime"><?php echo $row['update_date'];?></p>
<br>
<p class="post"><?php echo $row['update_content'];?></p>
Post a Comment
<?php
}
?>
<span style="float:right">
<? echo ($prev = ($NumPgs > 0 && $page!=1) ? "Prev ":
"Prev "); ?>
[Page <?php echo $page; ?>]
<? echo ($next = ($page < $NumPgs) ? "Next":
"Next");?>
<b> <? echo $offset+1?> to <?=$offset+$rows_per_page?>, of <? echo $total?> Entries</b>
</span>
</div>