how to add pagination in this code - php

How to add pagination in this code?
I want to limited data in one page and I want in bottom as:
1 2 3 4 5 6 8 9 10
If I click on page 2 then my more results show on that page.
How can I do like that ?
<?php
//connect to database
mysql_connect('localhost','root','pasword');
mysql_select_db('root');
/* Get the letter user clicked on and assign it a variable called $sort */
$sort = $_REQUEST['letter'];
/* Let's check if variable $sort is empty. If it is we will create a query to display all customers alphabetically ordered by last name. */
if($sort == ""){
$qry= "SELECT * FROM tree ORDER BY keywords ASC " ;
}else{
/* if varible $sort is not empty we will create a query that sorts out the customers by their last name, and order the selected records ascendingly. */
$qry = "SELECT * FROM tree WHERE keywords LIKE '$sort%' ORDER BY keywords ASC" ;
}
/* Notice the use of '%' wilde card in the above query "LIKE '$sort%'". */
//next step is to execute the query.
$execute = mysql_query($qry) or die(mysql_error());
/* Before we display results let's create our alphabetical navigation. The easiest way to create the navigation is to use character codes and run them through the "for" loop. */
echo "<p>" ;
for ($i = 65; $i < 91; $i++) {
printf('%s | ',
$PHP_SELF, chr($i), chr($i));
}
echo "</p>" ;
/* now we are ready to display the results. Since out tbl_customers table has only three fileds we will display the results in a paragraphs. In the real world you may need to display the results in a table.
To display the results we will use "do while" loop to fetch the results. If no customers are found we will display an error message. */
if(mysql_num_rows($execute)>0){
do{
echo "<p>" .$result['id']. " " .$result['keywords']. " " .$result['meaning']. "</p>" ;
}while($result = mysql_fetch_assoc($execute));
}else{
echo "<p>No customer found.</p>" ;
}
?>

First select no of datas to generate page numbers by total_number of rows by no of pages per page
mysql_num_rows($execute);
use limit command to limit the data like this
$qry= "SELECT * FROM tree ORDER BY keywords ASC limit 0,10 " ;
then for every page number increment the limit index

You can use the below code
$page = $_GET['page'];
if ($page < 1)
{
$page = 1;
}
$conc=mysql_connect('localhost','root','');
mysql_select_db('employer',$conc);
$resultsPerPage = 5;
$startResults = ($page - 1) * $resultsPerPage;
$query = mysql_query('SELECT * FROM employee ') or die(mysql_error());
$numberOfRows=mysql_num_rows($query);
$totalPages = ceil($numberOfRows / $resultsPerPage);
$query = mysql_query("SELECT * FROM employee LIMIT $startResults,$resultsPerPage");
while ($output = mysql_fetch_assoc($query))
{
$result[]=$output;
}
for($i = 1; $i <= $totalPages; $i++)
{
if($i == $page)
echo '<strong>'.$i.'</strong>&nbsp';
else
echo ''.$i.'&nbsp';
}

$page = intval(abs($_GET['page'])); // get security ONE
if(mysql_num_rows($sql)) // get page number to empty location index.php
{
header('Location: index.php');
}

Related

pagination in php recent row first

I've created different pages for 20 different rows in my php code, please have a look-
if(isset($_GET['page']))
{
$page = $_GET['page'];
}
else
{
$page = '';
}
if($page=='' || $page==1)
{
$page1=0;
}
else
{
$page1=($page*20)-20;
}
$query="SELECT * FROM issued_books limit $page1,20";
if($did_query_exec=mysqli_query($conn,$query))
{
while($query_exec=mysqli_fetch_array($did_query_exec, MYSQLI_ASSOC))
{
$isret = $query_exec["Date_returned"];
$dateret = $query_exec["Date_returned"];
$dateis = $query_exec['Date_issued'];
//echoing the values here
}
}
$query = "SELECT * FROM `issued_books`";
$Result = mysqli_query($conn, $query);
$cou = mysqli_num_rows($Result);
$a = $cou/20;
echo "</br></br>";
$a = ceil($a);
echo 'Page ';
for($b=$a; $b>=1; $b--)
{
echo "<a href='return-books.php?page=$b' style='text-decoration:none'>$b </a>";
}
The above code creates the link for all the pages with row 1 of page 1 contains the first record inserted by me.
I wish to display the recent most created record in row 1 of page 1
any kinda help would be really appreciated, thanks :)
You have to add ORDER BY with DESC:-
$query="SELECT * FROM `issued_books` ORDER BY id DESC limit $page1,20";
And
$query = "SELECT * FROM `issued_books` ORDER BY id DESC";
Note:- change the column name in ORDER BY according to your wish.

Pagination displaying issue php

In simple pagination, when clicking different no. 1,2,3,4, records from table not changing
as they should.
I'm displaying 5 records on each page,as on click 1 it display record from table 1 to 5,and so on.But,it is not changing.Help me.
<?php
mysql_connect("localhost","root","");
mysql_select_db("bluewater");
$page = $_GET["page"];
if($page=="" || $page=="1")
{
$page1=0;
}
else
{
$page1=($page*5)-5;
}
$sql=mysql_query("select * from countries limit 1,5");
while($ser = mysql_fetch_array($sql)) {
echo $ser["ccode"];
echo $ser["country"];
echo "<br>";
}
$sql=mysql_query("select * from countries");
$cou=mysql_num_rows($sql);
$a=$cou/5;
$a= ceil ($a);
echo "<br>"; echo "<br>";
for ($b=1;$b<=$a;$b++)
{
?><?php echo $b." ";?> <?php
}
?>
1) Don't use mysql_ functions, use mysqli_ or PDO.
2) To move via pages, you need to increase offset in query.
$page = $_GET['page'];
$itemsPerPage = 5;
$offset = ($page * $itemsPerPage) - $itemsPerPage;
$query = $db->prepare("SELECT * FROM `countries` LIMIT {$itemsPerPage} OFFSET {$offset}");
$query->execute();
$results = $query->fetchAll();
You never make use of your $page variable in the query. Change it to
select * from countries limit ".$page.", 5
and it should work
Also, mysql_real_escape_string($page)!
$sql=mysql_query("select * from countries limit $page1,5");
replace the first query with above code.
Just replace the your first sql query with the my sql query:
your sql query
$sql=mysql_query("select * from countries limit 1,5");
You provided static start limit is "1"
Correct Sql query:
$sql=mysql_query("select * from countries limit $page1,5");
Make its static limit is $page1 variable.
Try this approach.It is easier to understand.
//code for database connection
$left_rec=$rec_count-($page*$rec_limit);
$res=mysqli_query("select * from countries");
$rec_count=mysqli_num_rows($res);//Total no of records
if(isset($_GET['page']))
{
$page=$_GET['page']+1;
$offset=$rec_limit*$page;
}
else
{
$page=0;
$offset=0;
}
$rec_limit=5;//no of records per page
$offset=$page*$rec_limit;
$left_rec=$rec_count-($page*$rec_limit);
$res=mysqli_query("select * from countries limit $offset,$rec_limit");
while($row = mysqli_fetch_array($res))
{
echo $row["ccode"];
echo $row["country"];
echo "";
}
if($page==0)
{
echo"Next";
}
else if($left_rec <= $rec_limit)
{
$last=$page-2;
echo"Prev";
}
?>

How to send a search term in a url that paginates the results

I have a simple form to search a table for the search term entered into that form.
$query = $_GET['term'];
$min_length = 1;
if(strlen($query) >= $min_length){
$query = htmlspecialchars($query);
$query = mysqli_real_escape_string($link, $query);
}
Here the variable 'query' is the search query posted from the form.
The query for the database looks like this
$sql = mysqli_query($link, "SELECT * FROM products WHERE `name` LIKE '%".$query."%' OR
`brand` LIKE '%".$query."%'
OR `description` LIKE '%".$query."%' OR `spec` LIKE '%".$query."%'
OR `category` LIKE '%".$query."%' OR `subcategory` LIKE '%".$query."%' AND status = 1
ORDER BY id DESC LIMIT $offset, $rowsperpage") OR die(mysqli_error($link));
So this works ok if I dont paginate results, but I need to paginate them.
I think I need to get this line of code to send the variable '$query' to each paginated page.
echo " <a href='{$_SERVER['PHP_SELF']}?$query&currentpage=$x'>$x</a> ";
'$x' is just the variable for the current page
My form is now set to method="get"
EDIT: Sorted (I think)
I had everything wrapped in a
if(isset( $_GET['submit'] ) ){
}
So it appears to me that when I went to any other page but the first page (the first time) that submit from my search form wasn't being posted again obviously, so there were no results to get.
When I took that 'if' out the pagination works. But is it ok not to have it?
You need to forward term parameter and current page index. Also you must add
offset $x*$pagesize
limit $pagesize
to your query...
A few thoughts here:
Before even getting into the pagination aspects, just by looking at your query, I would suggest you look at using a MySQL natural language full text search (http://dev.mysql.com/doc/refman/5.5/en/fulltext-natural-language.html). that would make your query something like this:
SELECT * FROM products
WHERE
MATCH (name, brand, description, spec, category, subcategory)
AGAINST :query
AND STATUS = 1
This requires a FULLTEXT index across the fields used in the search. This requires use of MyISAM engine for the table.
An interesting thing to note about using the full text search in the where clause like this is that it will return rows in order of "relevance". So, for example, a record that has a number of matches across DB columns for you search term will have higher relevance than a record with a single match. In most search uses cases like yours, this is desired behavior, and your current query does nothing at all to provide this sort of functionality.
Now on to pagination.
You would likely need to pass some additional parameters to indicate page. So say your parameters are like this:
// you should provide some validation for these parameters, but I am not showing that
$query = $_GET['term'];
$page = $_GET['page'];
You would also somewhere in your code have some value set to determine rows per page to be shown. This could even be in the form of a parameter as well if you want to let the user select number of rows per page.
$rows_per_page = $_GET['pagerows'];
// or simply something like $rows_per_page = 10;
Calculation of values to use in the LIMIT clause is trivial. I am assuming $page is 1-based (i.e. first page has value of 1)
$offset = ($page - 1) * $rows_per_page;
Putting it together with the query above you get:
SELECT * FROM products
WHERE
MATCH (name, brand, description, spec, category, subcategory)
AGAINST :query
AND STATUS = 1
LIMIT :offset, :pagerows
Note I am just using named parameter syntax in the queries. You should consider using prepared statements for these queries.
So that is is the simple use case. Now what if you want to show the user some indication as to the number of pages associated with the search result? This means that you need to understand how many rows the query would return if there was no LIMIT clause. There are a couple of ways of doing this. You could simply run the same query without the LIMIT first and get the number of rows from that query. This may be appropriate for cases where you were not accepting user input to filter the result set and the user could arrive in the pagination view on any arbitrary page number.
That does not seem to be the use cases realted to a search, so I will give you another option which may best suite the search query use case. That is the use of SQL_CALC_FOUND_ROWS keyword in conjunction with FOUND_ROWS() functions. Here is how it works.
You simply add SQL_CALC_FOUND_ROWS into you SELECT query like this:
SELECT SQL_CALC_FOUND_ROWS * FROM products
WHERE
MATCH (name, brand, description, spec, category, subcategory)
AGAINST :query
AND STATUS = 1
LIMIT :offset, :pagerows
Your query will return the paginated result as expected, but using the same DB connection, you can then make a follow-up query to get the number of "found rows" (i.e. rows in result set before applying LIMIT from the last query). That query is very simple:
SELECT FOUND_ROWS()
So here is how this may look from a coding perspective:
$sql = <<<EOT
SELECT SQL_CALC_FOUND_ROWS * FROM products
WHERE
MATCH (name, brand, description, spec, category, subcategory)
AGAINST :query
AND STATUS = 1
LIMIT :offset, :pagerows
EOT;
// perform query here
// work with your result set
while ($row = /* your row fetch logic here */) {
// do something with row
}
// now query DB again to get found rows
// you must use same DB connection used for initial query
// and you must not perform any queries between your initial
// SELECT and this query to get row count
$row_sql = 'SELECT FOUND_ROWS()';
// execute your query and read the value from the result
// lets assume you set this to a variable named $total_rows
// now simply determine number of available page by simple math and rounding
$page_count = ceil($total_rows / $rows_per_page);
Please try something like this:
pagination.php
<?php
function getPagingQuery($sql, $itemPerPage = 10)
{
if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
$page = (int)$_GET['page'];
} else {
$page = 1;
}
// start fetching from this row number
$offset = ($page - 1) * $itemPerPage;
return $sql . " LIMIT $offset, $itemPerPage";
}
function getPagingLink($sql, $itemPerPage = 10,$strGet)
{
$result = mysql_query($sql) or die(mysql_error());
$pagingLink = '';
$totalResults = mysql_numrows($result);
$totalPages = ceil($totalResults / $itemPerPage);
// how many link pages to show
$numLinks = 10;
// create the paging links only if we have more than one page of results
if ($totalPages > 1) {
$self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;
if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
$pageNumber = (int)$_GET['page'];
} else {
$pageNumber = 1;
}
// print 'previous' link only if we're not
// on page one
if ($pageNumber > 1) {
$page = $pageNumber - 1;
if ($page > 1) {
$prev = " [Prev] ";
} else {
$prev = " [Prev] ";
}
$first = " [First] ";
} else {
$prev = ''; // we're on page one, don't show 'previous' link
$first = ''; // nor 'first page' link
}
// print 'next' link only if we're not
// on the last page
if ($pageNumber < $totalPages) {
$page = $pageNumber + 1;
$next = " [Next] ";
$last = " [Last] ";
} else {
$next = ''; // we're on the last page, don't show 'next' link
$last = ''; // nor 'last page' link
}
$start = $pageNumber - ($pageNumber % $numLinks) + 1;
$end = $start + $numLinks - 1;
$end = min($totalPages, $end);
$pagingLink = array();
for($page = $start; $page <= $end; $page++) {
if ($page == $pageNumber) {
$pagingLink[] = " $page "; // no need to create a link to current page
} else {
if ($page == 1) {
$pagingLink[] = " $page ";
} else {
$pagingLink[] = " $page ";
}
}
}
$pagingLink = implode(' | ', $pagingLink);
// return the page navigation link
$pagingLink = $first . $prev . $pagingLink . $next . $last;
}
return $pagingLink;
}
?>
product_search.php
$query = $_GET['term'];
$select = "SELECT * FROM products WHERE `name` LIKE '%".$query."%' ";
$strget="term=".$query;
$rowsPerPage =10;
$result= mysql_query(getPagingQuery($sel_products, $rowsPerPage, $strget)) or die(mysql_error());
$pagingLink = getPagingLink($sel_products, $rowsPerPage, $strget);
$num_rows = mysql_numrows($result);
if($num_rows > 0) {
while($array_in= mysql_fetch_array($result))
{
$product_id = $array_in['id'];
$product_name = $array_in['name'];
}
echo $pagingLink;
}
else
{
echo "No Results To Display";
}
I hope it should work.

Trouble setting PHP variable in if statement

I have an Ajax call to create an image gallery. There is a combo box to set the number of images per page, and pagination to navigate through the pages. It's working fine, except that if a person is on the last page, and they increase the number of images per page, it reloads to the current page number. This is a problem because there are now less pages and so the page is blank. In other words, you end up on page 7 of 5 for instance.
I reckon the problem is in the PHP as that is where the number of pages is calculated. I've thought out an if statement to deal with this:
if ($page > $no_of_paginations) {
$page = $no_of_paginations;
}
However, I don't know where to place this. The reason being, $page needs to be defined before the mysql_query, but $no_of_paginations is defined after the query.
Any thoughts on how I can make this functional?
I'll post the pertinent code below:
<?php
$page = 0;
if(isset($_GET['page'])){
$page = (int) $_GET['page'];
}
$cur_page = $page;
$page -= 1;
if((int) $_GET['imgs'] > 0){
$per_page = (int) $_GET['imgs'];
} else {
$per_page = 16;
}
$start = $per_page * $page;
include"db.php";
$query_pag_data = "SELECT `imgURL`,`imgTitle` FROM `images` ".
"ORDER BY `imgDate` DESC LIMIT $start, $per_page";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
echo "<ul class='new_arrivals_gallery'>";
while($row = mysql_fetch_assoc($result_pag_data)) {
echo "<li><a target='_blank' href='new_arrivals_img/".$row['imgURL']."' class='gallery' title='".$row['imgTitle']."'><img src='new_arrivals_img/thumbnails/".$row['imgURL']."'></a></li>";
}
echo "</ul>";
/* --------------------------------------------- */
$query_pag_num = "SELECT COUNT(*) AS count FROM images";
$result_pag_num = mysql_query($query_pag_num);
$row = mysql_fetch_array($result_pag_num);
$count = $row['count'];
$no_of_paginations = ceil($count / $per_page);
?>
Thanks for your help!
As you are currently doing it, there would be no problem with moving the last section to above the main query.
A better way to find the total records with MySQL is to use the SQL_CALC_FOUND_ROWS keyword on your main query (so here it is SELECT SQL_CALC_FOUND_ROWS imgURL, imgTitle FROM images WHERE etc) then you can query SELECT FOUND_ROWS() and just get the number of records found in the last query. As well as being faster and more efficient, this avoids a race condition when records are added between the two queries.
However, in this case you should probably just do the two current queries in the reverse order as your only other option is to check at the end and repeat if necessary.
You should use query:
$query_pag_data = "SELECT SQL_CALC_FOUND_ROWS `imgURL`,`imgTitle` FROM `images` ".
"ORDER BY `imgDate` DESC LIMIT $start, $per_page";
And instead of
$query_pag_num = "SELECT COUNT(*) AS count FROM images";
use
$query_pag_num = "SELECT FOUND_ROWS()";
If you do the "SELECT COUNT(*) ..." query earlier in the script (at least before the other query), you will have $no_of_paginations earlier, and can use it to clamp $page to the correct range.
Sometimes you just have to bite the bullet and do a query twice. Accept the user-provided "I want page X" value, and try to get that particular page. If it ends up being past the end of the available data, you can either say "hey, wait... that ain't right" and abort, or redo the loop and default to the last available page.
while(true) {
$sql = "select sql_calc_found_rows .... limit $X,$Y";
$result = mysql_query($sql) or die(mysql_error());
$sql = "select found_rows();"; // retrieve the sql_calc_found_rows_value
$res2 = mysql_query($sql);
$row = mysql_fetch_array($res2);
$total_rows = $row[0];
if ($total_rows < $requested_row) {
... redo the loop and request last possible page ...
} else {
break;
}
}
details on found_rows() function here.

How to display five rows?

How can I use PHP to show five rows from a MySQL database, then create a new line and show another five, etc?
Use the LIMIT clause if you want to limit the amount of results returned from the query.
If you want to print an <hr/> after every fifth record you can check it via the modulus operator:
$counter = 1;
while ($row = mysql_fetch_assoc($rst)) {
// print $row stuff
if ($counter % 5 == 0)
print "<hr />";
$counter++;
}
Basically, we have a variable used to count how many records we've printed. Once that counter can be divided by five, and leave no remainder, we print our horizontal-rule.
Something like this may be helpful:
$result = mysql_query($query);
if($result) {
while($row = mysql_fetch_assoc($result)) {
if(++$i%5==0 && $i>0) {
/* do the extra thing... new line some styles */
}
}
}
Err.. you mean something like:
SELECT * FROM `tablename` WHERE ... LIMIT 5
$total = 20;//get total number here;
$limit = 5;
for($i = 0;$i< $total/$limit;$i++)
{
$sql = $result = $rows = "";
$start = $limit * $i;
$sql = "select * from m_table order by id desc limit $start,$limit";
$result = mysql_query($query);
while($rows = mysql_fetch_assoc($result))
{
//print the result here;
}
}
You can fetch 5 rows every time from mysql without fetching all the rows at once.

Categories