PDA

View Full Version : Paging system....involving arrays...


moe122
04-01-2008, 01:01 PM
Hey david,
you've helped me in the past...could i get some more help? plz, pwetty plz......i need to produce a paging system. It need to do the following,

i need to produce an array that contains ten rows of data, so could i have a variable that says start array from $a=1 which corresponds to row 1 then do a loop....until $a=10
i then need a button or link(saying something like next) which onclick or something similar changes $a to 11 and then keeps on changing it on click.

the reason for the array is because it goes to flash and then i have flash previewing the array.....the flash side is all sorted.....the only problem i think we may have is if for instance the page is not updated when clicking next.....
Also is it better to call the database in 1 go and get all the info or to get it bit by bit?

moe122
04-02-2008, 10:08 PM
Hey david.....u aight?
i cant seem to get this paging system to work......if i can just get the basics sorted for this i can then carry on from there.....at the moment all ive got when i preview this is values 1-6 (i.e i've got 6 record in the database) i then have the links printed but its just text.....i cant click on the next or previous links etc...any help would be appreciated....


<html>
<head>
<title>Implementing Paging with next and prev</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
require_once("Connections/Connection.php"); // Database Connection
// how many rows to show per page
$rowsPerPage = 20;
// 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 * FROM clients LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');
// print the random numbers
while(list($val) = mysql_fetch_array($result))
{
echo "$val <br>";
}
echo '<br>';
// how many rows we have in database
$query = "SELECT COUNT(*) AS numrows FROM clients";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// 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 = ' [Prev] '; // we're on page one, don't enable 'previous' link
$first = ' [First Page] '; // 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 = ' [Next] '; // we're on the last page, don't enable 'next' link
$last = ' [Last Page] '; // nor 'last page' link
}
// print the page navigation link
echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;

?>
</body>
</html>

davidj
04-03-2008, 01:27 PM
http://www.dreamweaverclub.com/forum/showthread.php?t=27634

student101
04-11-2008, 02:35 PM
In order to page through records you need more records.
How do you page through 6 records? if you only have 6 records?
You state:
$rowsPerPage = 20;Then you need 21 or more records for you paging to work, or change the rowsPerPage value to "6"

Cheers