Limiting the number of pagination pages - php

I have one page where I echo 10 items from MySql DB and has pagination for next page. The problem is that there are 1000+ rows in database.. so you can guess how many pages are in pagination links. How can I limit their numbers for example to show
1 2 3 4 5 ... N
when user change to page 2
2 3 4 5 6 ... N
and so on?
Or something else.. the main goal is just to hide every single page number.
<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 10;
$result = $pdo->prepare("SELECT t.* FROM images t order by randorder ASC LIMIT $start_from, 10");
$result->execute();
for($i=0; $row = $result->fetch(); $i++)
{
echo 'echo data';
}
echo '<div class="pagination" >';
$result = $pdo->prepare("SELECT COUNT(image_id) FROM images");
$result->execute();
$row = $result->fetch();
$total_records = $row[0];
$total_pages = ceil($total_records / 10);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='?page=".$i."'";
if($page==$i)
{
echo "class=active";
}
echo ">";
echo "".$i."</a> ";
};
echo '</div>';
?>

Related

Dropdown in pagination

I want to add dropdown in pagination so that if there is more then 10 pages it will be stored in dropdown so that any one can select any page from it.
Here is my php code which show this output:
1 2 3 4 5 6 7 8 9 10 11........... 100
$sql = "SELECT * FROM `new_data`";
$rs_result = mysqli_query($con,$sql);
$total_records = mysqli_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='View.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo " <a href='View.php?page=".$i."'>".$i."</a> ";
};
echo " <a href='View.php?page=$total_pages'>".'>|'."</a> ";//Goto last page
Output i want :
1 2 3 4 5 6 7 8 9 10 dropdown(contains all pages 11 to 100)
$sql = "SELECT * FROM `new_data`";
$rs_result = mysqli_query($con,$sql);
$total_records = mysqli_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='View.php?page=1'>".'|<'."</a> "; // Goto 1st page
$array = [];
for ($i=1; $i<=$total_pages; $i++) {
$array[$i] = "<a href='View.php?page=".$i."'>".$i."</a>";
};
echo " <a href='View.php?page=$total_pages'>".'>|'."</a> ";//Goto last page
Add all the page informations in array and then later populate the dropdown with an array I have changed the for loop code only ... you just popultae your dropdown with the $array;
I done it with php using your php code. Here is my code:
$sql = "SELECT * FROM `new_data`";
$rs_result = mysqli_query($con,$sql); //run the query
$total_records = mysqli_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='View.php?page=1'>".'FIRST <<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++)
{
if($total_pages<=10)
{
echo " <a style='color:#333;' href='View.php?page=".$i."'>".$i."</a> ";
}
else
{
for ($i=1; $i<=10; $i++)
{
echo " <a style='color:#333;' href="View.php?page=".$i."'>".$i."</a> ";
};
echo "<select class='mySelectBox' onchange='location = this.options[this.selectedIndex].value;'>";
for ($i=11; $i<=$total_pages; $i++)
{
echo "<option value= View.php?page=".$i.">".$i."</option>";
};
echo "</select>";
}
};
echo " <a href='View.php?page=$total_pages'>".'........>> LAST'."</a> "; // Goto last page

PHP pagination with page numbers

I have code that I have used on several sites to deal with pagination. The problem I'm facing is that the code only give 'next' and 'previous' links. For the site I am working on I need page numbers too, not them all, maybe 5 at a time, kin of like this
< 1 2 3 4 5 >
then when you get to page 5
< 6 7 8 9 10 >
This is my pagination code so farf
//paganation settings
$pages_query = mysqli_query($link, "SELECT COUNT(`id`) FROM `products` WHERE subcat = 1 AND
status = 1") or die(mysqli_error($link));
$result = mysqli_fetch_array($pages_query, MYSQLI_NUM);
$pages = $result[0] / $per_page;
$page = (isset($_GET['page']) AND (int)$_GET['page'] > 0) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$last = ($pages - 1) / $per_page;
$prev = $page - 1;
$next = $page + 1;
//query the db
$q =mysqli_query($link, "SELECT * FROM products WHERE subcat = 1 AND status = 1
ORDER BY id DESC LIMIT $start, $per_page");
if(mysqli_num_rows($q) > 0){
//find out the page we are on and display next and previous links accordingly
if ($page >= 1){
if ($page < $pages) {
echo "<span class='next'>";
echo "Next ";
echo "</span>";
}
if ($page >=2){
echo "<span class='prev'>";
echo "Back ";
echo "</span>";
}
}
else if ($page > $pages + 1){
echo 'No more images in the database';
}
Can anyone help me add the page numbers in here
Thanks
for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<a href='index.php?page=".$i."'>".$i."</a>";
};
This might hel you
put it between if loops for next and prev page

limit page links based on the current page

I have this code which splits the mysql results into 10 results per page. but I want to edit it so that it limits links based on what the current page is. so on pages 1-10 it only shows page links 1-10 on pages 11-20 it only shows page links 11-20 and so on. how could I achieve this? Thanks.
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
};
$start_from = ($page-1) * 10;
$message = "SELECT * FROM document WHERE email='$_SESSION[email]' ORDER BY id DESC LIMIT $start_from , 10";
// echo results
// make page links for results
$sql = "SELECT id FROM document WHERE email = '$_SESSION[email]'";
$rs_result = $db->query($sql);
$total_records = $rs_result->num_rows;
$total_pages = ceil($total_records / 10);
if($rs_result->num_rows >10) {
$page = "<p class = 'page'>";
for ($i=1; $i<=$total_pages; $i++)
{
$page .="<a href='results.html?page=".$i."'>".$i."</a> ";
}
$page .="</p>";
echo $page;
}
Do this
"SELECT * FROM document WHERE email='$_SESSION[email]' ORDER BY id DESC LIMIT 10 OFFSET $start_from";
Tell me if it works.

Problem with PHP Pagination

I am having a problem with my code ( sorry its a lot, but its the only way I knew to show you, its basically just a select statement from a table) that shows a page link but doesnt change the page results. Basically I set it for example at 1 result per page but it shows all the results but still shows a link at the top to go to the next page. The next page just shows the same. I'm a PHP beginner so any help would be greatly appreciated.
Thanks!
<?php
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1;
$max_results = 1;
$from = (($page * $max_results) - $max_results);
REQUIRE('config.php');
$q = mysql_real_escape_string(ucfirst(trim($_REQUEST['q'])));
$result = mysql_query("SELECT * FROM gj WHERE name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q' ORDER by name") or trigger_error(mysql_error());
$rows = mysql_num_rows($result);
if($rows == 0){
}
echo " <div id='title'>Search for "$q"<div class='righttitle'>$rows business";if($rows > 1){echo "es";}elseif($rows == "0"){echo "es";}echo" found";
echo"<div id='pagenumbers'>";
// (1) get the total number of results for your query
// modify this to match the total results for the main query
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM gj where name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q'"),0);
// (2) Calculate total number of pages. Round up using ceil()
$total_pages = ceil($total_results / $max_results);
if($total_results > $max_results)
{
// (3) build Previous link
if($page > 1)
{
$prev = ($page - 1);
echo "<< Prev ";
}
// (4) display page numbers
for($i = 1; $i <= $total_pages; $i++)
{
if($page == $i)
{
echo $i . " ";
}
else
{
echo "$i ";
}
}
// (5) build Next Link
if($page < $total_pages)
{
$next = ($page + 1);
echo "Next >>";
}
}
echo"</div></div></div>";
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$name=$row['name'];
$phone=$row['phone'];
$website=$row['website'];
$city=$row['city'];
$address=$row['address1'];
$zipcode=$row['zipcode'];
$sponsored = $row['sponsored'];
$addressmap = preg_replace('/\s/', '+',$address);
$citymap = preg_replace('/\s/', '+',$city);
//Start While Loop
echo"
<div id='listing'>
<div id='mainlisting'>";
echo"
<div class='name'>
<a href='./more.php?id=$id' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>$name</a> <div class='right'>$phone</div>
</div>
<div class='other'>
$address, $city, CO $zipcode
|<a target='_blank' href='http://maps.google.com/maps? f=q&source=s_q&hl=en&geocode=&q=$addressmap,+$city+CO&&&ie=UTF8&hq=&hnear=$address,+$city,+Colorado+$zipcode&safe=active&&&t=h&z=14&iwloc=A&output=embed' rel='lyteframe' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"' title='$name' rev='width: 500px; height: 500px; scrolling: no;'> See Map</a><br/>
<a href='#' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>";if($website != null){ echo "<a target='_blank' href='$website' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>Website</a> |";}echo" <a href='#' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>More Info</a>
</div>
</div>
</div><!--/LISTING-->";
}
Michael, I tried what you did but I might have done it wrong...Here is my code before the where statement
<?php
$page = 1; $total_pages = 9; $record_start = ($page * $total_pages) - $total_pages;
REQUIRE('config.php');
$q = mysql_real_escape_string(ucfirst(trim($_REQUEST['q'])));
$result = mysql_query("SELECT * FROM gj WHERE name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q' ORDER by name LIMIT 0,9") or trigger_error(mysql_error());
$rows = mysql_num_rows($result);
if($rows == 0){
}
echo " <div id='title'>Search for "$q"<div class='righttitle'>$rows business";if($rows > 1){echo "es";}elseif($rows == "0"){echo "es";}echo" found";
echo"<div id='pagenumbers'>";
// (1) get the total number of results for your query
// modify this to match the total results for the main query
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM gj where name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q '"),0);
// (2) Calculate total number of pages. Round up using ceil()
$alltotal_pages = ceil($total_results / $max_results);
if($total_results > $max_results)
{
// (3) build Previous link
if($page > 1)
{
$prev = ($page - 1);
echo "<< Prev ";
}
// (4) display page numbers
for($i = 1; $i <= $alltotal_pages; $i++)
{
if($page == $i)
{
echo $i . " ";
}
else
{
echo "$i ";
}
}
// (5) build Next Link
if($page < $alltotal_pages)
{
$next = ($page + 1);
echo "Next >>";
}
}
echo"";
What you need to do is add something like:
$page = 1;
$results_per_page = 10;
$record_start = ($page *
$results_per_page) -
$results_per_page;
$result = mysql_query("SELECT * FROM
gj WHERE name LIKE '%$q%' OR cat1 LIKE
'%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE
'%$q' ORDER by name LIMIT
$record_start,$results_per_page") or
trigger_error(mysql_error());
The problem is that you always fetch the same result set and then just output all of it, no matter which page is currently "active". If you want to paginate, you'll probably want to use something like a LIMIT clause in your SQL query (e.g. LIMIT 20,10 to return 10 records starting from offset 20 (zero-based, i.e. record number 21)).
http://www.apnacode.com/php/simple-php-pagination/
Check Above Link for Code, May it help you

PHP & MySQL Pagination

I have a MySQL query
SELECT * FROM 'redirect'
WHERE 'user_id'= \''.$_SESSION['user_id'].' \'
ORDER BY 'timestamp'`
I want to paginate 10 results per page. How Can I do it?
Here is a nice starting point:
<?php
// insert your mysql connection code here
$perPage = 10;
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$startAt = $perPage * ($page - 1);
$query = "SELECT COUNT(*) as total FROM redirect
WHERE user_id = '".$_SESSION['user_id']."'";
$r = mysql_fetch_assoc(mysql_query($query));
$totalPages = ceil($r['total'] / $perPage);
$links = "";
for ($i = 1; $i <= $totalPages; $i++) {
$links .= ($i != $page )
? "<a href='index.php?page=$i'>Page $i</a> "
: "$page ";
}
$r = mysql_query($query);
$query = "SELECT * FROM 'redirect'
WHERE 'user_id'= \''.$_SESSION['user_id'].' \'
ORDER BY 'timestamp' LIMIT $startAt, $perPage";
$r = mysql_query($query);
// display results here the way you want
echo $links; // show links to other pages
Use LIMIT.
SELECT *
FROM redirect
WHERE user_id = '35251'
ORDER BY timestamp
LIMIT 40, 10
40 is how many records to skip, 10 is how many to display.
There are also a few problems with your PHP. You use backticks (not single quotes) to surround table and column names. And you shouldn't use string concatenation to build your query.
Here is my code
which contains next and Previous button
<?php
$limit = 3; //set Number of entries to show in a page.
// Look for a GET variable page if not found default is 1.
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else { $page=1;
}
//determine the sql LIMIT starting number for the results on the displaying page
$page_index = ($page-1) * $limit; // 0
$All_Users=mysqli_query($con,"select * from users limit $page_index, $limit");
while($row=mysqli_fetch_array($All_Users))
{
//show data in table or where you want..
}
$all_data=mysqli_query($con,"select count(*) from users");
$user_count = mysqli_fetch_row($all_data); // say total count 9
$total_records = $user_count[0]; //9
$total_pages = ceil($total_records / $limit); // 9/3= 3
if($page >= 2){
echo "<a href='blog.php?page=".($page-1)."' class='btn
customBtn2'>Previous</a>";
}
if($page<$total_pages) {
echo "<a href='blog.php?page=".($page+1)."' class='btn customBtn2'>NEXT</a>";
}
?>
Use the LIMIT clausule of the query to limit the amount of results you retrieve from the database.
See: http://dev.mysql.com/doc/refman/5.1/en/select.html

Categories