Project Pages

PHP Classes
SourceForge
Downloads
Archived Documentation

DB Connection Methods

objSQL
obj_close

Error Handling Methods

obj_error
obj_error_message

Statement Methods

obj_delete
obj_insert
obj_paging
obj_query
obj_select
obj_update

Statement Argument Methods

obj_batch_data
obj_cols
obj_data
obj_limit
obj_offset
obj_order_by
obj_sort_order
obj_table
obj_where

Resultset Methods

obj_affected_rows
obj_fetch_assoc
obj_fetch_num
obj_fetch_object
obj_field
obj_free_result
obj_num_fields
obj_num_rows

Prepared Statement Methods

obj_bind
obj_close_statement
obj_execute
obj_free_statement
obj_prepare_statement

Transaction Methods

obj_commit
obj_rollback
obj_savepoint
obj_transaction

Utility Methods

obj_escape
obj_info
obj_row_count


obj_paging()

Description:

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.

  • The table name argument is required unless using statement argument methods.
  • The optional cols argument specifies which columns to return and is entered as a comma delimited string. Entering an empty value is equivilent to an asterisk (*).
  • The optional where argument allows you to limit which rows are selected and only requires the column and its value. You can use any of the normal operators used in SQL statements:
    • "color IN ('blue','red')"
    • "color='red' AND material='leather'"
    • "brand LIKE 's%'"
    • "price BETWEEN 200 AND 400"
  • The optional order by argument is used to sort the resultset. SQL Server requires an order by argument and will throw an exception if not supplied.
  • The limit and offset arguments default to 1 if not supplied. limit is the number of records to display per page. The offset argument is normally supplied by the global $_GET or $_POST variables.

Parameters:

array obj_paging ( [str table[, str cols[, str where[, str order by[, str limit[, int offset ]]]]]] )


Returns:

Array

  • element[0]: Result resource/object or false on failure.
  • element[1]: Unsigned integer - Number of last page.

Example:

<?php 

try 

    
$output ''
    
$limit  = ( isset( $_GET["limit"] ) ) ? $_GET["limit"] : 20
    
$offset = ( isset( $_GET["page"] ) && $_GET["page"] > ) ? $_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 == )  
    { 
        
$output .= "&lt;&lt;&nbsp;&nbsp;&lt;&nbsp;"
    }  
    else  
    { 
        
$output .= "<a href=test_paging.php?page=1>&lt;&lt;&nbsp;&nbsp;</a>"
        
$prev    $offset 1
        
$output .= "<a href=test_paging.php?page=$prev>&lt;&nbsp;</a>"
    } 

    
$output .= " [ Page $offset of $last_page ] "

    if ( 
$offset == $last_page )  
    { 
        
$output .= "&nbsp;&gt;&nbsp;&nbsp;&gt;&gt;"
    }  
    else  
    { 
        
$next    $offset 1
        
$output .=  "<a href=test_paging.php?page=$next>&nbsp;&gt;</a>"
        
$output .=  "<a href=test_paging.php?page=$last_page>&nbsp;&nbsp;&gt;&gt;</a>"
    } 

    echo 
"<p>$output</p>"
     
    
$result->obj_free_result(); 
     
     
    
Displays
     
    
1Stephanie 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"] > ) ? $_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 == )  
    { 
        
$output .= "&lt;&lt;&nbsp;&nbsp;&lt;&nbsp;"
    }  
    else  
    { 
        
$output .= "<a href=test_paging.php?page=1>&lt;&lt;&nbsp;&nbsp;</a>"
        
$prev    $offset 1
        
$output .= "<a href=test_paging.php?page=$prev>&lt;&nbsp;</a>"
    } 

    
$output .= " [ Page $offset of $last_page ] "

    if ( 
$offset == $last_page )  
    { 
        
$output .= "&nbsp;&gt;&nbsp;&nbsp;&gt;&gt;"
    }  
    else  
    { 
        
$next    $offset 1
        
$output .=  "<a href=test_paging.php?page=$next>&nbsp;&gt;</a>"
        
$output .=  "<a href=test_paging.php?page=$last_page>&nbsp;&nbsp;&gt;&gt;</a>"
    } 

    echo 
"<p>$output</p>"
     
    
$result->obj_free_result(); 
     
     
    
Displays
     
    
1Stephanie 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