PHP MySQL Pagination Issue -- additional pages display all rows - php

I am currently searching my db and displaying results 5 per page and then I am intending to provide the user with links to the additional pages.
When i run this code it limits my initial query to the 5 records but then when I click on the links for the additional pages it is ignoring my sql query parameters and retuning all rows in the database.
Does anyone know why this may be happening?
Here is my code:
<html>
<head>
<link rel="stylesheet" type="text/css" href="results.css">
</head>
<img src="http://meritscholarshiplist.com/wordpress/wp- content/uploads/2015/12/searchhead.png" alt="" width="100%" style="padding-top:0px;"/>
<-Search Again
</html>
<?php
require('./wp-blog-header.php');
$current_user = wp_get_current_user();
$school = $_POST['school'];
$num_rec_per_page=5;
mysql_connect('', '', '');
mysql_select_db('');
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
}
;
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM `data` WHERE `school` LIKE '%$school%' LIMIT $start_from, $num_rec_per_page";
$rs_result = mysql_query ($sql);
//run the query
while ($array = mysql_fetch_row($rs_result)) {
echo "<div class='list' style='width:1750px; margin:0 auto;'>";
echo "<form action='singleview.php' method='post' target='_new$array[0]'><table id='scholarship' style='float:left;' align='center'><thead><th class='head' colspan='3' style='text-align:center;'>$array[2]<br><br>$array[5]<br><br>$array[3]<br><br><input type='hidden' name='id' value='$array[0]'><input type='submit' value='View More Details'><br>(Will Open in a New Tab)</th></thead>";
echo "</form></table></div> ";
}
;
?>
<?php
$school = $_POST['school'];
$sql = "SELECT * FROM `data` WHERE `school` LIKE '%$school%'";
$rs_result = mysql_query($sql);
//run the query
$total_records = mysql_num_rows($rs_result);
//count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='search.php?page=1'>".'|<'."</a> ";
// Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='search.php?page=".$i."'>".$i."</a> ";
}
;
echo "<a href='search.php?page=$total_pages'>".'>|'."</a> ";
// Goto last page
echo "<br>Your Search Returned $total_records Results";
?>

When you are clicking on the page number, your browser is making a GET request without the value of school. You can first add school to the request like
echo "<a href='search.php?page=1&school=".urlencode($school)."'>".'|<'."</a> ";
// Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='search.php?school=".urlencode($school)."&page=".$i."'>".$i."</a> ";
}
and make sure you can get the value of school from both GET and POST:
$school = $_REQUEST['school']; // instead of $_POST['school']

Related

How to do pagination in PHP?

I'm trying to do pagination in PHP, everything seems to be working, except that next and previous links don't work, it's only when I manually insert the page number in the URL that it displays data from the database on the next page.
Here is my code:
This is where I initialised the perpage and page. These are at the beginning of the page.
<?php
$per_page=4;
if (isset($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page=1;
}
$page;
echo $start_from = ($page-1) * $per_page;
//$search = $_POST['search'];
?>
And this is for the next and previous links, those ones that display the results depending on what the user wants to see.
<?php
$query = "select * from services";
$result = mysqli_query($link, $query);
$total_records = mysqli_num_rows($result);
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
$prev = $page - 1;
if($page == 1){
echo "<span align='right' class='inactive'>← Prev</span>";
}else{
echo "<a href='livebands.php?page=$prev'><span class='paging-prev'>".'← Prev'."</span></a>";
}
for ($i=1; $i<=2; $i++) {
for ($i=1; $i<=$page; $i++) {
echo "<a href='livebands.php?page=$i'><span class='paging'>" .$i. "</span></a>";
}
}
$page++;
if ($page>$total_pages){
echo "<span align='right' class='inactive'>→ Next</span>";
}else{
echo "<a href='livebands.php?page=$page&per_page=$per_page'><span align='right' class='paging-next'>".'Next →'."</span></a>";
}
?>
If I use you code and hardcode the $total_records variable to 5 for example, the links seem to work.
// $query = "select * from services";
// $result = mysqli_query($link, $query);
// $total_records = mysqli_num_rows($result);
$total_records = 5;
Are you sure that your $total_records is more than 4?

Passing query to next page (pagination)

Hi what I'm trying to do is make a pagination for the whole and gives also a chance the user to search for a name and returns a paginated result.
Here is my code
<form method = "POST">
<td>
Search:<input name="search_name" type="text" id="t_searchkey" style="width:35%;" placeholder = "Name">
<input type="submit" name="b_find" id="b_find" title="Find" value = "Find">
</td>
PHP code:
<?php
if(!isset($_POST['b_find']))
{
$query = "SELECT * FROM reginformation
WHERE deleted = 0";
$search_name = "";
}
if(isset($_POST['b_find']))
{
$search_name = trim($_POST['search_name']);
$query = "SELECT * FROM reginformation WHERE name = '$search_name' AND deleted = 0 ";
}
?>
<?php
$result = mysql_query($query) or die(mysql_error());
?>
<?php
$num_rec_per_page=5;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
if (isset($_GET["search"]))
{
$search_name = $_GET['search'];
}
$start_from = ($page-1) * $num_rec_per_page;
$query2 =$query . " LIMIT $start_from, $num_rec_per_page";
echo "query2 $query2";
$rs_result = mysql_query ($query2); //run the query
while ($row = mysql_fetch_assoc($rs_result)) {
echo "<tr onClick =window.location='infodetailsresp.php?id=$row[regID]'><td>$row[name]</td><td>$row[emailadd]</td><td>$row[contactno]</td><td>$row[event]</td><td>$row[date_register]</td></tr>";
};
//$sql = "SELECT * FROM reginformation";
$rs_result = mysql_query($query); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
?>
<?php
echo "<a href='reglistresp.php?page=1&search=$search_name'>".'|< '."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='reglistresp.php?page=".$i."&search=$search_name'>". $i ."</a> ";
};
echo "<a href='reglistresp.php?page=$total_pages&search=$search_name'>".' >| '."</a> "; // Goto last page
?>
What happens when I click the next page it gives me the query in the
if(!isset($_POST['b_find']))
So what should be changed so I can get my desired query to the next page?

Can anyone help me out to try pagination for the below code...!

Well below i have attached the table structure kindly go through it.
I would like to get serial no as my 1st column followed by id and name. serial no should continue for pagination rather then starting from 1st.
Thanks in advance..
Table structure is
create table departments (
id INT(20) AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL);
<html>
<head>
<?php
$db_connection = new mysqli("localhost","root","","emp_app");
if($db_connection->connect_error)
die("connection failed".$db_connection->connect_error);
?>
</head>
<body>
<table>
<tr>
<th>Serial no </th>
<th>id</th>
<th>name</th>
</tr>
<?php
$sql_query = "select * from departments";
$result = $db_connection->query($sql_query);
if($result->num_rows > 0){
while($rows = $result->fetch_assoc()){
echo "<tr>";
echo "<td>".$rows["id"]."<td>";
echo "<td>".$rows["name"]."<td>";
echo "<tr>";
}
}
?>
</table>
</body>
here is a good example how to add pagination in php pagination in php
Refrence link is using depricated mysql query you need to replace with mysqli query
$num_rec_per_page=10;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM departments LIMIT $start_from, $num_rec_per_page";
$rs_result = $db_connection->query ($sql); //run the query
$serial=1;
while ($row = $result->fetch_assoc) {
//code here
echo $serial;
$serial++;
};
$sql = "SELECT * FROM departments"; //select query for total records
$rs_result = $db_connection->query($sql); //run the query
$total_records =$rs_result->num_rows; //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='pagination.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='pagination.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
?>
You could append the URL to add a get variable that will serve as your pagination number i.e. youurl.com/?pagination=1 or 2 or 3
Then in your code add
$pagenumber = $_GET["pagination"];
$offset = $pagenumber * 5; //The number 5 is how many results per page if you wanted 10 results per page this would be 10
Where you have
$sql_query = "select * from departments";
You can change to
$sql_query = "select * from departments" LIMIT 5 OFFSET '$offset';
Which is basically saying; only give me 5 results starting from the pagination row (which is 5 results x page 2 or 3
<html>
<head>
<?php
$db_connection = new mysqli("localhost","root","","emp_app");
if($db_connection->connect_error)
die("connection failed".$db_connection->connect_error);
$num_rec_per_page=5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
};
$start_from = ($page-1) * $num_rec_per_page;
?>
</head>
<body>
<table>
<tr>
<th>Serial no </th>
<th>id</th>
<th>name</th>
</tr>
<?php
$sql_query = "SELECT * FROM departments LIMIT $start_from, $num_rec_per_page";
$result = $db_connection->query($sql_query);
if($result->num_rows > 0){
while($rows = $result->fetch_assoc()){
echo "<tr>";
echo "<td>".$rows["id"]."<td>";
echo "<td>".$rows["name"]."<td>";
echo "<tr>";
}
}
$sql = "SELECT * FROM departments"; //select query for total records
$rs_result = $db_connection->query($sql); //run the query
$total_records =$rs_result->num_rows; //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='index.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='index.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='index.php?page=$total_pages'>".'>|'."</a> ";
?>
</table>
</body>

how to show selected user to view mysql data

this code showing all MySQL table data to any users but i want only admin and user to show data of mysql query
i have multiple users
1.admin
2.user123
3.xyz
so i want only admin and user to show MySQL table data how can i do this
example
if admin and user123 open the page then mysql data showing correct if xyz user open the page then it will not showing any data
user login name <?php echo $_SESSION['SESS_FIRST_NAME']; ?>
i want to add this one in to mysql query to showing result only admin and user
$query = "SELECT * FROM `follower` WHERE `followername` LIKE '".$letter."%' ORDER BY `followername` ASC LIMIT $from, $max_results";
complete code
<?php
$max_results = 10;
$from = (($page * $max_results) - $max_results);
if(empty($_POST)) {
$query = "SELECT * FROM `follower` WHERE `followername` LIKE '".$letter."%' ORDER BY `followername` ASC LIMIT $from, $max_results";
}
$result = mysql_query("SET NAMES utf8"); //the main trick
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result);
echo "<table class='gridtable' border='1' cellpadding='0' cellspacing='0'>";
echo "<tr><th>Name</th><th>Company Name</th><th>Mobile No</th></tr>";
if ($rows > 0) {
while($row = mysql_fetch_array($result)) {
echo "<tr><td>";
echo $row['followername'];
echo "</td><td>";
echo $row['companyname'];
echo "</td><td>";
echo $row['mobileno'];
echo "</td><td>";
echo $row['contractdatee'];
echo "</td></tr>";
}
} else {
echo "<tr><td align=\"center\" colspan=\"4\">No results found!</td></tr>";
}
echo "</table>";
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as followername FROM follower ORDER BY followername ASC"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "<p class=\"style2\">Pages: ";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "Previous ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo " ";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "Next";
}
echo "</p>";
mysql_close();
?>

Mysql query paginate multi variable results

I would like to paginate my multi termed sql query in paginated results, page 1 works fine but page 2..previous or next do not pass the variable:
<?php
include "db.inc.php";
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 15;
$term1 = $_REQUEST['term1'];
$term2 = $_REQUEST['term2'];
$term3 = $_REQUEST['term3'];
$term4 = $_REQUEST['term4'];
$sql ="SELECT * FROM cdrequests WHERE pname LIKE '%$term1%' AND date LIKE '%$term2%' AND date LIKE '%$term3%' AND dept LIKE '%$term4%' LIMIT $start_from, 15";
$rs_result = mysql_query ($sql);
$num_rows = mysql_num_rows($rs_result);
$query = mysql_query("SELECT * FROM cdrequests WHERE pname LIKE '%$term1%' AND date LIKE '%$term2%' AND date LIKE '%$term3%' AND dept LIKE '%$term4%'");
$number=mysql_num_rows($query);
print "<font size=\"5\" color=white><b>CD Requests</b></font> </P>";
print "<table class=\"table1\" STYLE=\"word-wrap:break-word;\" width=1100 border=\"1\" bordercolor=\"#000000\" bgcolor=\"E6E6E6\" style=\"border-collapse: collapse\" cellpadding=\"2\" cellspacing=\"1\"> .............
?>
<?php
$term1 = $_REQUEST['term1'];
$term2 = $_REQUEST['term2'];
$term3 = $_REQUEST['term3'];
$term4 = $_REQUEST['term4'];
$sql = "SELECT COUNT(id) FROM cdrequests WHERE pname LIKE '%$term1%' AND date LIKE '%$term2%' AND date LIKE '%$term3%' AND dept LIKE '%$term4%'";
$rs_result = mysql_query($sql);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 15);
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($page > 1) {
// show << link to go back to page 1
echo " <a href='search2.php?page=1'><b>First</b></a> ";
// get previous page num
$prev = $page - 1;
// show < link to go back to 1 page
echo " <a href='search2.php?page=$prev'><b>«</b></a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($page - $range); $x < (($page + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $total_pages)) {
// if we're on current page...
if ($x == $page) {
// 'highlight' it but don't make a link
echo " <font size='5' color=yellow><b> $x </b></font> ";
// if not current page...
} else {
// make it a link
echo " <a href='search2.php?page=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($page != $total_pages) {
// get next page
$next = $page + 1;
// echo forward link for next page
echo " <a href='search2.php?page=$next'><b>»</b></a> ";
// echo forward link for lastpage
echo " <a href='search2.php?page=$total_pages'><b>Last</b></a> ";
} // end if
/****** end build pagination links ******/
echo " <font size='4' color=white>Total Records</font> <font size='5' color=yellow><b>$number</b></font>";
echo '</table>';
?>
not sure what I need to put in the echo " <a href='search2.php?page=$next'><b>»</b></a> "; in order to call the terms
thanks
Does this not work?
...
$start_from=$page*15
$query = mysql_query("SELECT * FROM cdrequests WHERE ...");
$all_rows = mysql_num_rows($query);
$totalPages = ceil($all_rows/15)-1; #How many pages?
$sql = $query . " LIMIT $start_from,15";
print "<font size=\"5\" color=white><b>CD Requests</b></font> </P>";
print "<table class=\"table1\..."
$terms= '&term1='.$term1 . '&term2='.$term2 . '&term3='.$term3 . '&term4='.$term4;
if($totalPages>1){
#Paging Starts Now
?>
<div align="center">
<? if ($page > 1) { ?>
Previous
First
<? } ?>
Page <? echo $page; ?> of <? echo $totalPages+1; ?>
<? if ($page < $totalPages) { ?>
Next
Last
</div>
<? } }?>
Make it much easier on yourself. Build the table once.
Apply jQuery Datatables to it
$('#table_id).datatables();
Paging done. It's really that easy! All it requires is jQuery and the DataTables plugin, plus a few lines of CSS. As a bonus, it will filter, sort, limit, and more with just additional line of code per feature required. Plus, it can be styled with Themeroller, making a better looking table than most developers can pull off.

Categories