...

View Full Version : help with paging



nurvirus
07-09-2007, 03:15 AM
http://i12.photobucket.com/albums/a208/n_amalyna/paging.jpg

Can someone please help me.. I'm doing paging for my website. As you can see in the picture it is done. But, my problem is.. the No. column i didnt take it from database, i'm using loop. So when i click on next, the number restarting at 1 2 3 instead of continuing 6,7,8. Hope someone can help me. Thank you.



<?php

//connection
include 'library/config.php';
include 'library/opendb.php';

// how many rows to show per page
$rowsPerPage = 5;

// by default we show first page
$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;

$query = "SELECT memo_code, memo_date, memo_title, memo_sender
FROM memo where id='$memo_code' ";

$pagingQuery = "LIMIT $offset, $rowsPerPage";
$result = mysql_query($query . $pagingQuery) or die('Error, query failed');



//numbering
$counter=($pageNum + 1) * $rowsPerPage;

$number= "LIMIT $counter, $rowsPerPage";
$number=1;



// print the memo info in table
echo '<table border="1" width="100%"><tr bordercolor="0" bgcolor="#CCCCCC"><td width="1%" bordercolor="#FFFFFF"><div align="center">NO.</td><td width="3%" bordercolor="#FFFFFF"><div align="center">DATE</td><td width="22%" bordercolor="#FFFFFF"><div align="center">TITLE</td></tr>';
while(list($memo_code, $memo_date, $memo_title) = mysql_fetch_array($result))
{
echo "<tr>";
?>

<td bordercolor='#FFFFFF' bgcolor='#FFFFCC'><div align="center"><? echo $number ?></td>

<td bordercolor='#FFFFFF' bgcolor='#CCCCCC'><div align="center"><? echo $memo_date ?></td>

<td bordercolor='#FFFFFF' bgcolor='#FFFFCC'><a href="memo_content.php?id=<?php echo $memo_code; ?>"><?echo $memo_title;?></td>
<?


$number ++;




echo "</tr>";
}
echo '</table>';
echo '<br>';

// how many rows we have in database
$result = mysql_query($query) or die('Error, query failed');
$numrows = mysql_num_rows($result);

// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

$self = $_SERVER['PHP_SELF'];

// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link




// print 'previous' link only if we're not
// on page one
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";

$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' '; // we're on page one, don't enable 'previous' link
$first = ' '; // nor 'first page' link
}

// print 'next' link only if we're not
// on the last page
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;

$next = " <a href=\"$self?page=$page\">[Next]</a> ";

$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' '; // we're on the last page, don't enable 'next' link
$last = ' '; // nor 'last page' link
}

// print the page navigation link
echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;

include 'library/closedb.php';
?>

PappaJohn
07-09-2007, 04:49 AM
I'm not sure what you're trying to accomplish here


$number= "LIMIT $counter, $rowsPerPage";
$number=1;

since you set $number to a string and then immediately set it to 1.

But anyway, since you are using $number as your row counter, try replacing


$number=1;

with


$number = (($pageNum - 1) * $rowsPerPage) + 1;

nurvirus
07-09-2007, 04:57 AM
it works! thank you so much for your help :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum