Paging Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
abie
Forum Newbie
Posts: 4
Joined: Fri Jun 26, 2009 4:13 am

Paging Question

Post by abie »

I can't figure out where I'm going wrong. I am trying to setup paging to show 5 rows per page. But it's not working out correctly. Any thoughts on why?

Code: Select all

 
include("dbinfo.inc.php");
include("header.inc");
 
//how many rows to show per page
$rowsPerPage = 5;
 
//show first page by default
$pageNum = 1;
 
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
    $pageNum = $_GET['page'];
}
 
// counting the offset
$offset = ($pageNum - 1 * $rowsPerPage);
 
mysql_connect("$hostname",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query= "SELECT * FROM scores order by ID DESC";
     "LIMIT $offset, $rowsPerPage";
 
$result=mysql_query($query);
 
//print the pages
while($row = mysql_fetch_array($result))
{
    echo $row['user'] .  ' bowled a' . $row['score'] . ' on ' . $row['date'] . '<br>';
}
 
 
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Paging Question

Post by Darhazer »

In my honest opinion

Code: Select all

$offset = ($pageNum - 1 * $rowsPerPage)
Should be

Code: Select all

$offset = ($pageNum - 1) * $rowsPerPage
Last edited by pickle on Mon Jun 29, 2009 11:36 am, edited 1 time in total.
Reason: Expanded IMHO acronym as per forum rules 1.1.11
abie
Forum Newbie
Posts: 4
Joined: Fri Jun 26, 2009 4:13 am

Re: Paging Question

Post by abie »

Didn't really do anything to help...still same problem

Code: Select all

<?
include("dbinfo.inc.php");
include("header.inc");
 
//how many rows to show per page
$rowsPerPage = 5;
 
//show first page by default
$pageNum = 1;
 
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
    $pageNum = $_GET['page'];
}
 
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
 
mysql_connect("$hostname",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query= "SELECT * FROM scores order by ID DESC";
     "LIMIT $offset, $rowsPerPage";
 
$result=mysql_query($query);
 
//print the pages
while($row = mysql_fetch_array($result))
{
    echo $row['user'] .  ' bowled a' . $row['score'] . ' on ' . $row['date'] . '<br>';
}
 
?>
 
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Paging Question

Post by McInfo »

Code: Select all

$query= "SELECT * FROM scores order by ID DESC"; // This semicolon should be a dot and there should be a space between DESC and LIMIT
     "LIMIT $offset, $rowsPerPage";
Edit: This post was recovered from search engine cache.
Post Reply