MySQL PHP Pagination - php

Is it possible to create pagination without getting all elements of table?
But with pages in GET like /1 /666…

It usually involves issuing two queries: one to get your "slice" of the result set, and one to get the total number of records. From there, you can work out how many pages you have and build pagination accordingly.
A simply example:
<?php
$where = ""; // your WHERE clause would go in here
$batch = 10; // how many results to show at any one time
$page = (intval($_GET['page']) > 0) ? intval($_GET['page']) : 1;
$start = $page-1/$batch;
$pages = ceil($total/$batch);
$sql = "SELECT COUNT(*) AS total FROM tbl $where";
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
$total = $row['total'];
// start pagination
$paging = '<p class="paging">Pages:';
for ($i=1; $i <= $pages; $i++) {
if ($i==$page) {
$paging.= sprintf(' <span class="current">%d</a>', $i);
} else {
$paging.= sprintf(' %1$d', $i);
}
}
$paging.= sprintf' (%d total; showing %d to %d)', $total, $start+1, min($total, $start+$batch));
And then to see your pagination links:
...
// loop over result set here
// render pagination links
echo $paging;
I hope this helps.

Yes, using mySQL's LIMIT clause. Most pagination tutorials make good examples of how to use it.
See these questions for further links and information:
How do you implement pagination in PHP?
Searching for advanced php/mysql pagination script
more results

You can use LIMIT to paginate over your result set.
SELECT * FROM comments WHERE post_id = 1 LIMIT 5, 10
where LIMIT 5 means 5 comments and 10 is the offset. You can also use the longer syntax:
... LIMIT 5 OFFSET 10

Related

How to index a table with order by?

Using a while loop I'm able to return my table in the order I want, but after implementing pagination the variable I've created (counter) resets itself on each page, frustratingly. Example code:
$sql = ('SELECT id,name,logo FROM mytable ORDER BY name DESC LIMIT 25');
$query = mysqli_query($db_conx,$sql);
$counter = 0;
while ($row = $query->fetch_assoc()) {
$counter++;
echo "$counter, $row['id'], $row['name']";
echo "<br />";
}
I've tried many things and can't get this to work. Obviously my logic is flawed. The loop returns the correct results, but the $counter variable breaks on each page, resetting itself indefinitely.
What I am trying to do is get $counter to increase by 25 (representing results for each page) for each of the pages created by the pagination loop. Example code:
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='page.php?page=".$i."'>&nbsp[".$i."]</a> ";
$GLOBALS["counter"]+=25;
};
Obviously this was not working, so I am stumped at what I should try next. If anyone has any ideas I would love to hear them, I have heard great things about the SO community.
You seem to display only the first 25 results at any time.
You need to initialize $counter to zero if it's the first page, to 26 if it's the second page, and so on :
$counter = 0;
if(isset($_GET['counter'])){
$counter = intval($_GET['counter']);
}
You need to modify your query to fetch a different set of results for each page :
$sql = 'SELECT id,name,logo FROM mytable ORDER BY name DESC LIMIT ' . mysqli_real_escape_string($db_conx, $counter . ',25');
$query = mysqli_query($db_conx,$sql);
Then I assume you display a link to the other paginated pages, you need to pass it the value of $counter :
Next

End Foreach after X number of records PHP

I have the following query:
$trabajos = $con->query("SELECT * FROM portafolio ORDER BY RAND() LIMIT 4");
while ($po = $trabajos->fetch_array()) {
$trab[] = $po;
}
This query is working fine, but, it shows only 4 records, but then again in another part of the same page I need the same table information but I don't want it to be limited, say I want 10 instead of only 4. so.
Section Header
Section Slider <-- here only shows 4 items as intended
Using a foreach as follow:
foreach ($trab as $it) {
// My html Code
}
Section Mid Body some info
Section Main Body More info
Section Mid End Footer <-- here I need the same information as in the slider section but with a different layout instead of only 4 items I need 10 items.
For that last item I have to open another query similar as before.
$trabajos = $con->query("SELECT * FROM portafolio ORDER BY RAND() LIMIT 10");
while ($po = $trabajos->fetch_array()) {
$trab[] = $po;
}
So I was thinking is there a way to use:
$trabajos = $con->query("SELECT * FROM portafolio ORDER BY RAND()");
while ($po = $trabajos->fetch_array()) {
$trab[] = $po;
}
That query without the LIMIT and instead Limit the foreach to only fetch the first 4 rows or is there a way to use that query with the LIMIT but instead of 4 or 10, say I use 20, and for the first foreach only display the first 4 and for the second foreach show the rest after the first 4.
The main idea is not have more than 1 query for the same information.
this will exit a loop, but why not query 10 records, build your results, all 10 and then just output 4 of them, there and all 10 over yonder.
$x = 0;
foreach ($trab as $it) {
if($x == 5){
break; //will exit loop as will return.
}
// My html Code
++$x;
}
This will work with any kind of loop, you can also use continue to skip to the next pass of the loop
foreach ($trab as $it) {
if($x < 5){
continue; //code below here wont run until $x >= 5.
}
// My html Code
++$x;
}
i'm not sure to understand exactly what you want, but if you want to divide your array in 2 part. You can use a counter
$trabajos = $con->query("SELECT * FROM portafolio ORDER BY RAND() LIMIT 20");
$i = 0;
$trab2 = new Array();
while ($po = $trabajos->fetch_array()) {
if($i < 4) {
$trab[] = $po;
} else {
$trab2[] = $po;
}
$i++;
}
First, RAND() is horrible since it essentially disables MySQL’s built in caching with each run of the query. But there is no best way for me to advise how to get around that based on your script’s requirements. So just keep that in mind. But here is what I would recommend.
That query without the LIMIT and instead Limit the foreach to only
fetch the first 4 rows or is there a way to use that query with the
LIMIT but instead of 4 or 10, say I use 20, and for the first
foreach only display the first 4 and for the second foreach show
the rest after the first 4.
Yes, just use array_slice like this:
$trabajos = $con->query("SELECT * FROM portafolio ORDER BY RAND() LIMIT 20");
while ($po = $trabajos->fetch_array()) {
$trab[] = $po;
}
$trab_slice_1_size = 4;
$trab_slice_2_size = 10;
$trab_slice_1 = array_slice($trab, 0, $trab_slice_1_size);
$trab_slice_2 = array_slice($trab, $trab_slice_1_size, $trab_slice_2_size);
The nice thing about array_slice is you can cleanly select a start & end point. In my example I set the sizes in variables. Which makes it easier to adjust in your code if you somehow want to dynamically change values.
The easiest way is to use a counter variable:
function fetch_items($limit=10) {
public $trabajos,$trab;
$count=0;
$trabajos = $con->query("SELECT * FROM portafolio ORDER BY RAND()");
while (($po = $trabajos->fetch_array()) && ($count<$limit)) {
$count++;
$trab[] = $po;
}
}
If you call fetch_items() without a parameter, it defaults to 10 or less. If you specify a parameter it loads the array with up to the amount you specify. You could also have the function return the value of $count so you know how many were returned.
try;
$trabajos = $con->query("SELECT * FROM portafolio ORDER BY RAND() LIMIT 10");
$i = 0;
while ($i < 5 && $po = $trabajos->fetch_array()) {
.....
.....
$i++;
}
then, if you want to re-use the data,
mysql_data_seek($trabajos, 0);
$i = 0;
while ($i < 11 && $po = $trabajos->fetch_array()) {
.....
.....
$i++;
}

How to hide the next button when no pages to dispaly

Can someone help me please? I am sure it is easy for you guys. I am battling to find a solution on how to hide the next link when there are no pages to display my code is as follows:
if (!isset($_GET['page']) or !is_numeric($_GET['page'])) {
$page = 0;
} else {
$page = (int)$_GET['page'];
}
$pages_query=mysql_query ("SELECT COUNT * FROM hardware");
$result = mysql_query("SELECT * FROM hardware LIMIT $page, 3");
echo 'Next<p>';
$prev = $page - 3;
//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0) {
echo 'Previous';
}
You can use mysql_num_rows($result) to get the number of records in hardware:
$result = mysql_query("SELECT * FROM hardware LIMIT $page, 3");
$record_count = mysql_num_rows($result);
if ($record_count > 1)
echo 'Next';
in your if statement check if the $page is greater than 0 then according to the outcome of the value of $page write your code. you can use another if statement in the first if statement and make it detect the situation and decide what to do. The other thing is if the user clicked next then the user is on the second page so your previous should appear if $prev is higher than 1 it should make it
something along the lines of:
$itemsPerPage = 3;
$sql = "SELECT * FROM hardware";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
$pageCount = $count/$itemsPerPage;
if($pageCount > $page) { //Are there more pages worth of items stored, than we're currently looking at?
echo 'next';
}
You want to be using OFFSET in your SQL syntax, as well as LIMIT.
LIMIT limits the number of rows returned.
OFFSET tells it to start a number of rows into the result set.
You need to limit to the number of items you want on a page. and offset by that number*page.
Hopes this helps.

How do I get the first and last results from a query?

I am creating a pagination script and I need to get the first and last results in the database query so that I can determine what results appear when the user clicks a page to go to. This is the code that I have at the minute:
// my database connection is opened
// this gets all of the entries in the database
$q = mysql_query("SELECT * FROM my_table ORDER BY id ASC");
$count = mysql_num_rows($q);
// this is how many results I want to display
$max = 2;
// this determines how many pages there will be
$pages = round($count/$max,0);
// this is where I think my script goes wrong
// I want to get the last result of the first page
// or the first result of the previous page
// so the query can start where the last query left off
// I've tried a few different things to get this script to work
// but I think that I need to get the first or last result of the previous page
// but I don't know how to.
$get = $_GET['p'];
$pn = $_GET['pn'];
$pq = mysql_query("SELECT * FROM my_table ORDER BY id ASC LIMIT $max OFFSET $get");
// my query results appear
if(!$pn) {
$pn = 1;
}
echo "</table><br />
Page $pn of $pages<br />";
for($p = 1;$p<=$pages;$p++) {
echo "<a href='javascript:void(0);' onclick='nextPage($max, $p);' title='Page $p'>Page $p</a> ";
}
I think you have few problems there, but I try to tackle them for you. First, as comments say above, you are using code that it vulnerable to SQL injection. Take care of that - you might want to use PDO, which is as easy use as MySQL extension, and will save you from many trouble (like injection).
But to your code, lets go through it:
You should ask DB to get count of the rows, not using mysql function, it's far more effective, so use SELECT count(*) FROM mytable.
For $pages use ceil() as you want all rows to be printed, if you have $max 5 and have 11 rows, round will make $pages 2, where you actually want 3 (last page just contains that last 11th row)
in LIMIT you want to LIMIT row_count OFFSET offset. You can calculate offset from page number, so: $max = row_count but $offset = ($max * $page) - $max. In your code if $get is directly the page, it means you get $get'th row (Not sure though what happens in your JS nextpage. Bare in mind that not all use JavaScript.)
I have prepared simple example here which uses PDO, maybe that gives you idea how simple it's use PDO.
The selecting rows shows example how to put parameters in SQL, it would be perfectly safe in this case state, 'SELECT * FROM pseudorows LIMIT '.$start.','.$max by I wanted to make an example how easy it is (and then safe):
// DB config
$DB_NAME = 'test';
$DB_USER = 'test';
$DB_PASSWD = 'test';
// make connection
try {
$DB_CONN = new PDO("mysql:host=localhost;dbname=".$DB_NAME, $DB_USER, $DB_PASSWD);
$DB_CONN->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die($e);
}
// lets say user param 'p' is page, we cast it int, just to be safe
$page = (int) (isset($_GET['p'])?$_GET['p']:1);
// max rows in page
$max = 20;
// first select count of all rows in the table
$stmt = $DB_CONN->prepare('SELECT count(*) FROM pseudorows');
$stmt->execute();
if($value = $stmt->fetch()) {
// now we know how many pages we must print in pagination
// it's $value/$max = pages
$pages = ceil($value[0]/$max);
// now let's print this page results, we are on $page page
// we start from position max_rows_in_page * page_we_are_in - max_rows_in_page
// (as first page is 1 not 0, and rows in DB start from 0 when LIMITing)
$start = ($page * $max) - $max;
$stmt = $DB_CONN->prepare('SELECT * FROM pseudorows LIMIT :start,:max');
$stmt->bindParam(':start',$start,PDO::PARAM_INT);
$stmt->bindParam(':max', $max,PDO::PARAM_INT);
$stmt->execute();
// simply just print rows
echo '<table>';
while($row = $stmt->fetch()) {
echo '<tr><td>#'.$row['id'].'</td><td>'.$row['title'].'</td></tr>';
}
echo '</table>';
// let's show pagination
for($i=1;$i<=$pages;$i++) {
echo '[ '.$i.' ]';
}
}
mysql_fetch_array returns an associative array
Which means you can use reset and end to get the first and last results:
$pqa = mysql_fetch_array($pq);
$first = reset($pqa);
$last = end($pqa);
I don't see how you plan to use the actual results, just page numbers should be sufficient for pagination.
Still, hope it helps. And yes, upgrade to mysqli, so your code doesn't get obsolete.

Modifing Pagination-Sorting Method to Change Rows Per Page

Hi i have a PHP based Pagination and Sorting Method which works fine.
I am able to sort and paginate according to any categories, but having problems when it comes to rows per page.
i have used GET Method to fetch information for pagination and sorting as
if (isset($_GET['rpp'])&& is_numeric($_GET['rpp'])) {
$rowsperpage = mysql_real_escape_string($_GET['rpp']);
}else{
$rowsperpage='5';
}
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
$currentpage = (int) mysql_real_escape_string($_GET['page']);
} else {
$currentpage = 1;
}
if (isset($_GET['order']) && in_array($_GET['order'], $order)) {
$orderby = mysql_real_escape_string($_GET['order']);
}else{
$orderby='id';
}
$sortby = '';
if (isset($_GET['sort'])) {
$sortby = mysql_real_escape_string($_GET['sort']);
}else{
$sortby='desc';
}
for pagination i have used following type of url string
echo " <li><a href='$pagename?order=$orderby&sort=$sortby&rpp=$rowsperpage&page=$nextpage'>Next»»</a>
and my mysql query is
$sql2 = "SELECT * FROM internet_security ORDER BY $orderby $sortby LIMIT $rowsperpage OFFSET $offset";
and in html i have used links to sort categories like
id-desc:
id-asc:
title-desc:
title-asc:
**limit rows by**
5
10
20
My question is
every thing is working fine except when i limit row per page via limit rows by - rows get limited to whatever limit(5,10,20) is selected but then if i click pagination every thing goes to its default set value i.e by id and in DESC order with LIMIT of 5.
and if i do it like
id-desc:
id-desc:
id-desc:
then it works but then i must have at least 12 links for full functionality.which is not a standard approach.
what i want is once a limit is set either by default or by sorting options, i shall be able to sort by id title etc.. and paginatin should works along with it.
i hope i made it clear.
please see what i am doing wrong and suggest any possible solution to my approach.
(Upgrading to an answer)
Store $rpp in a $_SESSION variable, updating only if it's present in $_GET:
if ( isset($_GET[ 'rpp'])) $_SESSION['rpp'] = intval($_GET['rpp']);
elseif (!isset($_SESSION['rpp'])) $_SESSION['rpp'] = 5;
Then use $_SESSION['rpp'] in your query:
$sql2 = "
SELECT *
FROM internet_security
ORDER BY $orderby $sortby
LIMIT $_SESSION[rpp] OFFSET $offset
";

Categories