The obj_paging method allows you to facilitate recordset paging queries with minimal SQL markup and is called from the objSQL class. Common uses include online catalogs where the user can view multi-page results by specifying a price range, color, material, etc., as well as display the results by ordering by price or availability.
array obj_paging ( [str table[, str cols[, str where[, str order by[, str limit[, int offset ]]]]]] )
Array
<?php
try
{
$output = '';
$limit = ( isset( $_GET["limit"] ) ) ? $_GET["limit"] : 20;
$offset = ( isset( $_GET["page"] ) && $_GET["page"] > 0 ) ? $_GET["page"] : 1;
$rs = $dbh->obj_paging( "mytable", "id,first_name,last_name", "", "id", $limit, $offset );
$result = $rs[0];
$last_page = $rs[1];
if ( $dbh->obj_error() )
throw new Exception( $dbh->obj_error_message() );
while( $row = $result->obj_fetch_object() )
echo "{$row->id}: {$row->first_name} - {$row->last_name}<br />";
if ( $offset == 1 )
{
$output .= "<< < ";
}
else
{
$output .= "<a href=test_paging.php?page=1><< </a>";
$prev = $offset - 1;
$output .= "<a href=test_paging.php?page=$prev>< </a>";
}
$output .= " [ Page $offset of $last_page ] ";
if ( $offset == $last_page )
{
$output .= " > >>";
}
else
{
$next = $offset + 1;
$output .= "<a href=test_paging.php?page=$next> ></a>";
$output .= "<a href=test_paging.php?page=$last_page> >></a>";
}
echo "<p>$output</p>";
$result->obj_free_result();
Displays:
1: Stephanie - Parker
2: Estaban - White
3: Xavier - Nichols
4: Betty - Jefferies
5: Stephanie - Clark
6: Greg - Pipes
9: Ezra - Yontz
11: Marcia - Raymer
13: Andrew - Jordan
14: Ian - Jenkins
15: Howard - Dixon
16: Bonnie - Ward
17: Louis - Pipes
18: Orson - Schroeder
19: Tiffany - Darden
20: Xavier - Clark
21: George - Raymer
22: Cheryl - Yontz
24: Percy - Cavenaugh
25: David - Ingram
<< < [ Page 1 of 23 ] > >>
}
catch ( Exception $e )
{
//log error and/or redirect user to error page
}
try
{
//set arguments using statement argument helper methods
$output = '';
$limit = ( isset( $_GET["limit"] ) ) ? $_GET["limit"] : 20;
$offset = ( isset( $_GET["page"] ) && $_GET["page"] > 0 ) ? $_GET["page"] : 1;
$dbh->obj_table( "mytable" );
$dbh->obj_cols( "id,first_name,last_name" );
$dbh->obj_order_by( "id" );
$dbh->obj_limit( $limit );
$dbh->obj_offset( $offset );
$rs = $dbh->obj_paging();
$result = $rs[0];
$last_page = $rs[1];
if ( $dbh->obj_error() )
throw new Exception( $dbh->obj_error_message() );
while( $row = $result->obj_fetch_object() )
echo "{$row->id}: {$row->first_name} - {$row->last_name}<br />";
if ( $offset == 1 )
{
$output .= "<< < ";
}
else
{
$output .= "<a href=test_paging.php?page=1><< </a>";
$prev = $offset - 1;
$output .= "<a href=test_paging.php?page=$prev>< </a>";
}
$output .= " [ Page $offset of $last_page ] ";
if ( $offset == $last_page )
{
$output .= " > >>";
}
else
{
$next = $offset + 1;
$output .= "<a href=test_paging.php?page=$next> ></a>";
$output .= "<a href=test_paging.php?page=$last_page> >></a>";
}
echo "<p>$output</p>";
$result->obj_free_result();
Displays:
1: Stephanie - Parker
2: Estaban - White
3: Xavier - Nichols
4: Betty - Jefferies
5: Stephanie - Clark
6: Greg - Pipes
9: Ezra - Yontz
11: Marcia - Raymer
13: Andrew - Jordan
14: Ian - Jenkins
15: Howard - Dixon
16: Bonnie - Ward
17: Louis - Pipes
18: Orson - Schroeder
19: Tiffany - Darden
20: Xavier - Clark
21: George - Raymer
22: Cheryl - Yontz
24: Percy - Cavenaugh
25: David - Ingram
<< < [ Page 1 of 23 ] > >>
}
catch ( Exception $e )
{
//log error and/or redirect user to error page
}
?>
See also: obj_delete, obj_insert, obj_query, obj_select, obj_update