User's Manual

Navigating Through Database Records
4-8 Oracle Database Express Edition 2 Day Plus PHP Developer Guide Beta Draft
isset($_POST['prevdept']) &&
$_SESSION['currentdept'] > 1) {
$current = $_SESSION['currentdept'] - 1;
}
elseif (isset($_SESSION['currentdept']) &&
isset($_POST['nextdept'])) {
$current = $_SESSION['currentdept'] + 1;
}
elseif (isset($_POST['showdept']) &&
isset($_SESSION['currentdept'])) {
$current = $_SESSION['currentdept'];
}
else {
$current = 1;
}
$query = 'SELECT department_id, department_name,
manager_id, location_id
FROM departments
ORDER BY department_id asc';
$conn = db_connect();
$dept = db_get_page_data($conn, $query, $current, 1);
$deptid = $dept[0]['DEPARTMENT_ID'];
$_SESSION['currentdept'] = $current;
ui_print_header('Department');
ui_print_department($dept[0], $_SERVER['SCRIPT_NAME']);
ui_print_footer(date('Y-m-d H:i:s'));
}
?>
The if and elseif construct at the start of the construct_departments()
function is used to detect if a navigation button was with a HTTP post request to
process the page, and tracks if the currentdept number is set in the session
state. Depending on the circumstances, the variable $current is decremented by
one when the previous button is clicked, $current is incremented by one when
the Next button is clicked, otherwise $current is set to the current department,
or initialized to one for the first time through.
A query is formed to obtain all the department rows in ascending sequence of the
department_id. The ORDER BY clause is an essential part of the navigation
logic. The query is used as a sub query inside the db_get_page_data()
function to obtain a page of a number of rows, where the number of rows per page
is specified as the fourth argument to the db_get_page_data() function. After
connecting to the database, db_get_page_data() is called retrieve the set of
rows obtained for the specified query. The db_get_page_data() function is
provide with the connection resource, the query string, a value in $current
specifying the first row in the next page of data rows required, and the number of
rows per page (in this case one row per page).
After calling db_get_page_data() to obtain a page of rows, the value of
$current is stored in the application session state.
Between printing the page header and footer, the ui_print_department()
function is called to display the recently fetched department row, and uses
$_SERVER['SCRIPT_NAME'] to supply the current PHP script name for the