Database Reference
In-Depth Information
# If we're not at the beginning of the query result, present a live
# link to the previous page. Otherwise, present static text.
if
(
$start
>
1
)
# live link
{
my
$url
=
sprintf
(
"%s?start=%d;per_page=%d"
,
url
(),
$start
-
$per_page
,
$per_page
);
$page
.=
"["
.
a
({
-
href
=>
$url
},
"previous page"
)
.
"] "
;
}
else
# static text
{
$page
.=
"[previous page]"
;
}
# If we got the extra record, present a live link to the next page.
# Otherwise, present static text.
if
(
@
{
$tbl_ref
}
>
$per_page
)
# live link
{
my
$url
=
sprintf
(
"%s?start=%d;per_page=%d"
,
url
(),
$start
+
$per_page
,
$per_page
);
$page
.=
"["
.
a
({
-
href
=>
$url
},
"next page"
)
.
"]"
;
}
else
# static text
{
$page
.=
"[next page]"
;
}
$page
.=
end_html
();
print
$page
;
Paged displays with links to each page
The next script,
state_pager2.pl
, is much like
state_pager1.pl
, but presents a paged dis‐
play that includes navigation links to each page of the query result. To do this, it's nec‐
essary to know how many rows there are in all.
state_pager2.pl
determines this by run‐
ning a
SELECT
COUNT(*)
statement. Because the script then knows the total row count,
it need not select an extra row when fetching the section of the result to be displayed.
(For a large table,
SELECT
COUNT(*)
with no
WHERE
clause can be slow. For an application
using a such a table, it's best to include a
WHERE
clause that narrows down the result.)
Omitting the parts of
state_pager2.pl
that are the same as
state_pager1.pl
, the middle
part that retrieves rows and generates links is implemented as follows:
# Determine total number of records