Problem with PHP Pagination - php

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

Related

How to show 1 result per page in PHP

I have three restaurants in my database(MySQL) just for testing and i tried to create some pagination code that would show me one restaurant per page.
So i have implemented some code but my problem is that every page displays me all the restaurants again and again.
<?php
$page = 1;
if(isset($_GET['page']))
{
$page = $_GET['page'];
}
$query=mysqli_query($con,"SELECT * FROM restaurants LIMIT 10". ($page * 10 - 10));
$result = mysqli_query($con,"SELECT COUNT(*) as c FROM restaurants");
$row = mysqli_fetch_assoc($result);
$numberOfPages = $row['c'] / 1;
for ($i = 1; $i <= $numberOfPages; $i++)
{
echo '<a style="margin-left:10px;" href="allrestaurants.php?page='. $i . '">' . $i . '</a>';
}
if (!empty($query))
{
if(mysqli_num_rows($query)>0)
{
while ($row=mysqli_fetch_assoc($query))
{
$resId=isset($row['id'])?$row['id']:''; ?>
<div class="col-md-4 p-t-30">
<!-- Block1 -->
<div class="blo1">
<?php
$q="SELECT * FROM restaurant_images JOIN restaurants ON restaurant_images.resaurant_id=restaurants.id AND restaurants.id='$resId'";
$queryRestaunt_image=mysqli_query($con,$q);
if (!empty($queryRestaunt_image))
{
if(mysqli_num_rows($queryRestaunt_image)>0)
{
$count=0;
while ($image=mysqli_fetch_assoc($queryRestaunt_image))
{
$count++;
?>
<a href="restaurantdetails.php?id=<?=$resId?>">
<div class="wrap-pic-blo1 bo-rad-10 hov-img-zoom" >
<div style="background:url('./images/restaurants/<?=isset($image['image'])?$image['image']:'intro-01.jpg'?>') no-repeat center; background-size: contain;height:230px; background-color: black">
</div>
</div>
</a>
<?php
if($count==1)break;
}
}
}
?>
I expect that that i have only one result per page.
You need to change your SQL query to properly use LIMIT clause:
SELECT * FROM restaurants LIMIT <page offset>, <number of restaurants per page>;
Example:
$query = mysqli_query($con, "SELECT * FROM restaurants LIMIT " . ($page - 1) . ", 1");
$query=mysqli_query($con,"SELECT * FROM restaurants LIMIT 10". ($page * 10 - 10));
You can limit to ONE, changing here:
$query=mysqli_query($con,"SELECT * FROM restaurants LIMIT 1". ($page * 10 - 10));
Then you would have to check your logic about the pagination system... if 10 in ($page * 10 - 10) the 10 is used because the default pagination would be 10 results, then you would have to change the 10 for 1 in every line...

Limiting the number of pagination pages

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>';
?>

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

pagination going to other link when clicking but counts the number of rows

I have a problem with my pagination it's working but when I click for example
I have search ermel ermel has 8 fields and it's gonna output pagination
because limit is set to 5 per page
[1][2]
but when I click on the second page it will go to the other page and remove my query.
and will count my whole query again
[1][2][3][4][5][6]
like that and will reset to may default query
I'm not sure where the error is - I tried to remove some part but that's the only part that's not working properly thanks in advance .
Here's my working php code.
<?php
$searchtext = '';
if(isset($_GET['q'])) $searchtext = mysql_real_escape_string($_GET['q']);
if($searchtext) {
$per_page =5;
$pages_query = mysql_query("SELECT COUNT('PersonID') FROM persons where firstname like '%$searchtext' or lastname like '%$searchtext' order by date desc ");
$pages = ceil(mysql_result($pages_query,0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query=mysql_query("select * from persons where firstname like '%$searchtext' or lastname like '%$searchtext' order by date desc LIMIT $start,$per_page ");
}
else
{
$per_page =5;
$pages_query = mysql_query("SELECT COUNT('PersonID') FROM persons ");
$pages = ceil(mysql_result($pages_query,0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query=mysql_query("select * from persons order by date desc LIMIT $start,$per_page ");
}
while($test = mysql_fetch_array($query))
{
$id = $test['PersonID'];
echo"<div class = content />";
echo "<table>
<tr>
<td rowspan='2'><img height=200 width=200 src='upload/". $test['Image'] ."'/></td>
<td><a href ='profile.php?PersonID=$id'>".$test['LastName'].", ". $test['FirstName']." ". $test['MiddleName']."</a>
</tr>
<tr>
<td><a href ='user_edit_reports.php?PersonID=$id'><input type='submit' name='submit' value='Edit'/></a>
</tr>
</table>";
echo"</div>";
}
if ($pages >=1 && $page <= $pages) {
for ($x=1; $x<=$pages; $x++) {
echo ($x == $page) ? '<strong>'.$x.' </strong>' : ''.$x.' ';
}
}
?>
My guess is that the links in the pagination do not include the search string part (?q=...), and so when you click them, you get the entire, unfiltered list again.

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