/    Sign up×
Community /Pin to ProfileBookmark

Pagination…Urgent

hi all

i have below script for pagination
I have 5 results per page…
I want to show the following text on every page

on first page – “Showing records from 1 – 5”
on second page – “Showing records from 6 – 10”
on third page – “Showing records from 11 – 15”
and so on….

how to do it….? i m not getting any clue…

[CODE]<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
include ‘../library/config.php’;
include ‘../library/opendb.php’;

// 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 val FROM randoms LIMIT $offset, $rowsPerPage”;
$result = mysql_query($query) or die(‘Error, query failed’);

// print the random numbers
while($row = mysql_fetch_array($result))
{
echo $row[‘val’] . ‘<br>’;
}
echo ‘<br>’;

// how many rows we have in database
$query = “SELECT COUNT(val) AS numrows FROM randoms”;
$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);

// print the link to access each page
$self = $_SERVER[‘PHP_SELF’];
$nav = ”;
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= ” $page “; // no need to create a link to current page
}
else
{
$nav .= ” <a href=”$self?page=$page”>$page</a> “;
}
}

// creating previous and next link
// plus the link to go straight to
// the first and last page

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 = ‘&nbsp;’; // we’re on page one, don’t print previous link
$first = ‘&nbsp;’; // nor the first page link
}

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 = ‘&nbsp;’; // we’re on the last page, don’t print next link
$last = ‘&nbsp;’; // nor the last page link
}

// print the navigation link
echo $first . $prev . $nav . $next . $last;

// and close the database connection
include ‘../library/closedb.php’;
?>
</body>
</html>
[/CODE]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@scragarDec 16.2007 — firstly you really badly need some checking, what if $_GET['page'] is not a number? maybe it's a bad link, maybe it's someone trying to hack you and maybe it's someone copying a bad URL, either way you could get some really bad info if you don't check it.

secondly try:
[code=php]echo "showing results $offset to ".($offset-1+$rowsPerPage);[/code]
Copy linkTweet thisAlerts:
@jack001authorDec 16.2007 — firstly you really badly need some checking, what if $_GET['page'] is not a number? maybe it's a bad link, maybe it's someone trying to hack you and maybe it's someone copying a bad URL, either way you could get some really bad info if you don't check it.

secondly try:
[code=php]echo "showing results $offset to ".($offset-1+$rowsPerPage);[/code][/QUOTE]


its give me result as

showing results 0 to 6
Copy linkTweet thisAlerts:
@scragarDec 16.2007 — small adjutment then:

[code=php]echo 'showing results '.($offset-1).' to '.($offset-2+$rowsPerPage);[/code]
Copy linkTweet thisAlerts:
@jack001authorDec 17.2007 — small adjutment then:

[code=php]echo 'showing results '.($offset-1).' to '.($offset-2+$rowsPerPage);[/code][/QUOTE]



this also not working
×

Success!

Help @jack001 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...