473,320 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to display 20 results per page, with a next and previous page?

I would like to add 20 offers per page and becuase the database is updated daily. i need a way to allow access to other pages so that all offers will be displayed. i was thinking the next / previous function, but my page count can constantly change so im wanting some way of it automatically only having the amount of pages needed to support the amount of offers. normally i can display anywhere from 100-1000+ offers


code:

<?
session_start();
$page="offers";
include"header.php";
include"mysql.php";

$_GET['out']=strip_tags($_GET['out']);
$_GET['out']=mysql_real_escape_string($_GET['out']);
if($_GET['out']!="")
{
mysql_query("update offers set visits=visits+1 where id={$_GET['out']}",$c);
$geturl=mysql_query("select * from offers where id={$_GET['out']}",$c);
$out=mysql_fetch_array($geturl);
print"
<h2>Please wait..</h2>
<p>
<center>
<meta http-equiv=\"refresh\" content=\"2;url={$out['url']}/&subid1={$ui['username']}\">
Taking you to the offer...<br>
If this page does not change in 2 seconds, <a href=\"{$out['url']}/&subid1={$ui['username']}\">click here</a>.
</center>
</p>
";
include"side.php";
include"footer.php";
exit;
}
else
{
$money=0;
$getoffercash=mysql_query("select * from offers where active=1",$c);
while($oc=mysql_fetch_array($getoffercash))
{
$money=$money+$oc['reward'];
}
}
if($_SESSION['loggedin']==1)
{
$refer="<br>Refer your friends: $siteurl/?ref={$ui['username']}<br>and receive an instant $$refbonus bonus per referral!";
}
else
{
$refer="";
}
?>


<h2>Free Offers</h2>
<p class="post-by"><a href="how.php?ref=<? print"$ref"; ?>">not sure what to do?</a></p>
<p><center>Possible Earnings: $<?=$money;?><?=$refer;?></center></p>
<table width="90%">
<tr>
<th colspan="0" align="center">
Free To Complete
</th>
</tr>
<tr>
<th width="25%">Offer</th>
<th width="10%">Date</th>
<th width="45%">Description</th>
<th width="10%">Reward</th>
<th width="10%">Completed?</th>
</tr>
<?

$_GET['offer']=strip_tags($_GET['offer']);
$_GET['offer']=mysql_real_escape_string($_GET['offer']);
$type="free";
$getoffers=mysql_query("select * from offers where `type`='$type' and active=1 order by reward desc",$c);

if(mysql_num_rows($getoffers)==0)
{
print"<tr><td colspan=\"3\">There are currently no free offers available</td></tr>";
}
else
{

while($off=mysql_fetch_array($getoffers))
{
$desc=$off['desc'];
$date=$off['date'];

if($_GET['offer']==$off['id'])
{
$color="style=\"background-color: #a2e250;\"";
$front="<font color=\"black\"><b>";
$back="</b></font>";
}
else
{
$color="";
$front="";
$back="";
}
if($_SESSION['loggedin']==1)
{
$form="<form class=\"searchform2\" action=\"completed.php\" method=\"get\"><input type=\"hidden\" name=\"offer\" value=\"{$off['id']}\"><input type=\"submit\" class=\"button\" value=\"Done!\"></form>";
}
else
{
$form="<font color=\"red\"><b>Please Login</b></font>";
}


$checkpend=mysql_query("select * from pending where offer_id={$off['id']} and user_id='{$_SESSION['userid']}' and denied=0",$c);
$checkcompleted=mysql_query("select * from completed where offer_id={$off['id']} and user_id='{$_SESSION['userid']}'",$c);

if(mysql_num_rows($checkpend)!=0)
{
$form="<font color=\"orange\"><b>Pending</b></font>";
}
else if(mysql_num_rows($checkcompleted)!=0)
{
$form="<font color=\"green\"><b>Completed</b></font>";
}

if(mysql_num_rows($checkpend)!=0 || mysql_num_rows($checkcompleted)!=0)
{
$link="$front{$off['name']}$back";
}
else
{
$link="<a href=\"offers.php?out={$off['id']}\" target=\"_blank\" title=\"Available in: {$off['countries']}\">$front{$off['name']}$back</a>";
}

print"
<tr $color>
<td width=\"100%\">$link</td>
<td align=\"center\">$date</td>
<td align=\"center\">$desc</td>
<td align=\"center\">$front\${$off['reward']}$back</font></td>
<td align=\"center\">$form</td>
</tr>
";
}
}

?>
</table>
<?
include"side.php";
include"footer.php";
?>
Feb 6 '11 #1
3 6259
HaLo2FrEeEk
404 256MB
First off, please use code tags in the future, they make code a lot easier to read on the forum because it preserves formatting.

As for your question, there are a few aspects. To get a limited set of results from the database use the LIMIT command in your query. Example:

SELECT * FROM `table` ORDER BY `id` DESC LIMIT 0, 20

This query would select the most recent 20 results. You need something to sort it by though, in this example I'm sorting by id, id would be a column set to auto increment, something where newer rows have a higher value than older rows.

Next, you need to count how many rows there are total, for this you could use the count() selector:

SELECT count(*) FROM `table`

This will allow you to calculate how many pages you need. For example, if you are displaying 20 results per page and you have 50 results in the table, then you need 3 pages. The first 2 pages would have 20 results and the last page would have 10. I'd recommend setting a variable with the number of results you want per page, you can use this variable in your first SELECT query, the one with the limit command, you can also use it in your calculations.

Finally, you need a GET variable that represents which page you're currently on.

Expand|Select|Wrap|Line Numbers
  1. $rpp = 20;
  2. $page = @$_GET['page'];
  3. if(!$page) {
  4.   $page = 0;
  5.   }
  6.  
  7. $sql = mysql_query("SELECT * FROM `table` LIMIT ".($rpp * $page).", ".$rpp) or die("Error: " . mysql_error());
  8. // Do something with your results
  9.  
  10. $sql = mysql_query("SELECT count(*) FROM `table`") or die("Error: " . mysql_error());
  11. $res = mysql_result($sql, 0);
  12. $totalpages = ceil($res / $rpp); // ceil() rounds fractions up to the next whole number
Hopefully I've given you enough to start with.
Feb 7 '11 #2
Ive added the Limit 20,30 to the command line, but im noticing that when i tried to change the values, the data is not showing the correct amt. i only see my first change of limit 0,20 and nothing else. ive also followed the guide for displaying pages and am not showing any links to the pages, im also receiving no errors. would be happy to provide link to site through an email

Expand|Select|Wrap|Line Numbers
  1. $_GET['offer']=strip_tags($_GET['offer']);
  2. $_GET['offer']=mysql_real_escape_string($_GET['offer']);
  3. $type="free";
  4. $getoffers=mysql_query("select * from offers where `type`='$type' and active=1 order by reward desc LIMIT 20,30",$c);
  5. if (isset($_GET['pageno'])) {
  6.    $pageno = $_GET['pageno'];
  7. } else {
  8.    $pageno = 1;
  9. } // if
  10. $query = "SELECT count(*) FROM table WHERE ...";
  11. $result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
  12. $query_data = mysql_fetch_row($result);
  13. $numrows = $query_data[0];
  14. $rows_per_page = 25;
  15. $lastpage      = ceil($numrows/$rows_per_page);
  16. $pageno = (int)$pageno;
  17. if ($pageno > $lastpage) {
  18.    $pageno = $lastpage;
  19. } // if
  20. if ($pageno < 1) {
  21.    $pageno = 1;
  22. } // if
  23. $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
  24. $query = "SELECT * FROM table $limit";
  25. $result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
  26. ... process contents of $result ...
  27. if ($pageno == 1) {
  28.    echo " FIRST PREV ";
  29. } else {
  30.    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
  31.    $prevpage = $pageno-1;
  32.    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
  33. } // if
  34. echo " ( Page $pageno of $lastpage ) ";
  35. if ($pageno == $lastpage) {
  36.    echo " NEXT LAST ";
  37. } else {
  38.    $nextpage = $pageno+1;
  39.    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
  40.    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
  41. } // if
  42.  
  43.  
  44.  
Feb 8 '11 #3
HaLo2FrEeEk
404 256MB
I gave you the tools and knowledge you need to solve problem, I won't write the code for you. You need to go back and read my post.

the LIMIT command in a query looks like this:

LIMIT 0, 20

That means that thequery will get 20 results starting from the 0th (first) result, so it will get results 0-19. You need to use your page variable to determine what value you start from, so if you're on page 1, then your limit will look like it does above, if you're on page 2 then it will be LIMIT 20, 20, page 3, LIMIT 40, 20. Make sense? You calculate this by having a variable where you specify how many results you want to display per page:

Expand|Select|Wrap|Line Numbers
  1. $page = @$_GET['page']; // Gets the current page
  2. if(!$page)
  3.   $page = 0; // Defaults the pae to 0 if it's not provided.
  4. $rpp = 20; // This is the number of results per page
  5.  
  6. // This query sets the limit to start at the page number multipled by the results per page, and to get $rpp results.
  7. $sql = "SELECT * FROM `table` ORDER BY `reward` LIMIT ".($pae * $rpp)).", ".($rpp);
Please note that this isn't plug n' play code, it's an example that you will need to understand before you can use it.

As for the pages, here's a simple way to show a previous and next link:

Expand|Select|Wrap|Line Numbers
  1. // You've already got your $page variable
  2. $sql = mysql_query("SELECT count(*) FROM `table`") or die("Error: ".mysql_error());
  3. $numrows = mysql_result($sql, 0);
  4. $numpages = ceil($numrows / $rpp);
  5.  
  6. if($page <= $numpages) {
  7.   echo "<a href=\"?page=".($page + 1)."\"><< Previous</a>";
  8.   }
  9. if($page > 0) {
  10.   echo "<a href=\"?page=".($page - 1)."\">Next >></a>";
  11.   }
Once again, this is example code, you need to understand HOW it works and not just THAT it works, otherwise you won't learn anything and you'll be right back here the next time you have a problem like this. Understanding and learning why and how the code works is the first step to furthering your own knowledge and ability, it's how I've gotten as good as I am at this kind of thing.
Feb 9 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: joe | last post by:
Can anyone help me out by either telling me how to get to the result i need or by pointing me to some documentation about the following question: A certain query to a database give me eg 100...
3
by: Marcel | last post by:
Hello, I'm working on a search application for my website. The website contains a lot of pictures, and a search should return clickable thumbnails. No problems there. My problem started when I...
11
by: middletree | last post by:
I need to capture the previous page's URL. I thought I could find a way to do it with client-side code, but I have been unable to find a way to do that, so perhaps there's an ASP solution. ...
2
by: Lee Connell | last post by:
Hi, How do I create a page that has the potential of grabbing a lot of results and displaying the result set in a per number basis. For example a google search will display say 15 pages with a...
3
by: g3000 | last post by:
I have a problem with a Visual Studio 2005 web project. I have two pages. SelectProject.aspx and ShowProject.aspx The first page ( SelectProject.aspx) has two drop down lists. After the user...
1
by: alanb | last post by:
Hi, I need help with only displaying a set of answers / feedback on the following site: I use Javascript to to the calculations and just pass the value through using hidden fields to the...
8
pentahari
by: pentahari | last post by:
Redirect to login page if session("UserName") is expired when the page useraccount.aspx load event My useraccount.aspx code : If Session("UserName") Is Nothing Then ...
3
oranoos3000
by: oranoos3000 | last post by:
hi i'm a beginer javascript would you please help me i d like to findout this page has next page or previous page in history object i use this line of code if(window.history.length>0) and with...
3
by: rjlorenzo | last post by:
Hi to all, I have created a survey page that consists of 4 pages 10 question on each page. Each question are just answer using a radio button. Now my problem is i have to retain all the...
1
by: mfaisalwarraich | last post by:
hi everybody, im trying to make next and previous buttons. im displaying record based on a search. when any image on the search results is clicked then dat image opened in a new page with details...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.