Moving Through the Table with the Admin Browser
Introduction.
Now I want to add the code and buttons so that the admin can move around
through the table using the admin browser. We will build in five
capabilities.
Rather than messing around with trying to explain where you should embed new code I will redisplay the admin_browser.php each time we augment its functionality. The new code will be highlighted in red, while the comments will stay in green. Then we will discuss the major enhancements after the code is presented. |
<?php // assign the values for database access $host = "localhost"; $user = "your_user_name"; $password = "your_password"; $db_name = "database_name"; $table_name = "user_registration"; session_start( ); // initializing the record count // and session variable to keep track of current record if (!isset($_SESSION['current_row'])) {
}
}
}
}
} |
The form should now look like the following. |
So you should modify the code for your server's
connections to MySQL and then upload it and move around. Code Explanation. The truly new lines of code are the following. We will put them in a table in order to put an appropriate discussion in an adjacent cell. |
session_start( ); // initializing the record count // and session variable to keep track of current record if (!isset($_SESSION['current_row'])) {
} |
At the start of a
session a session variable that retains the current record location needs
to be used due to the stateless interactions on the web. If this session variable is not yet initialized then we want to move to the first record. The actual move does not occur until later. This
session variable is just used to keep track of the current position. |
//
obtaining the number of rows in the record set $number_rows = mysql_num_rows($result_set); |
The number of
rows is placed in a variable for later use. |
// if
the First button has been pressed if ($cmdFirst != "") {
} |
If the command
button to move to the first record is pressed then the session variable
that keeps track of the position is set to one. The actual move does not occur until later. |
// the
block of code to move the admin to the previous record // if the Previous button has been pressed if ($cmdPrevious != "") {
} |
If the Previous
button is pressed we first need to make certain we aren't already at the
first record. If we are at the first record then a warning message is displayed and nothing else happens. If we aren't at the first record the session variable tracking the record position is decremented by 1. Remember, as usual, the actual move doesn't occur until later. |
// the
block of code to move the admin to the next record // if the Next button has been pressed if ($cmdNext != "") {
} |
If the Next
button is pressed we first need to make certain we aren't already at the
last record. If we are at the last record then a warning message is displayed and nothing else happens. If we aren't at the last record the session variable tracking the record position is incremented by 1. Remember, as usual, the actual move doesn't occur until later.
|
// if
the Last button has been pressed if ($cmdLast != "") {
} |
If the command
button to move to the last record is pressed then the session variable
that keeps track of the position is set to the number of records in the
table. The actual move does not occur until later.
|
// a
variable for printing $current_row_number = $_SESSION['current_row']; |
Placing the current row number into a variable for later use and display. |
// go
to this current row mysql_data_seek($result_set, $_SESSION['current_row']); |
This is where we
actually move to whatever has been assigned as the current row. |
// and
retrieve it into an array $row = mysql_fetch_row($result_set); |
This is where we
retrieve the current row and place it into an array. Remember, we
cannot access the fields in the row by the field names we need to use
their ordinal position number. |
<tr> <td align = right> <?php echo("<font size=4>Record $current_row_number</font>"); ?> </td> <td align = left> <?php echo("<font size=4>of $number_rows</font>"); ?> </td> </tr> |
This is the HTML for the table row and columns with embedded PHP that displays the current row number and the total number of rows in the table near the bottom. |
<tr> <td align = right> <input type = submit name="cmdFirst" value="First"> <input type = submit name="cmdPrevious" value="Previous"> </td> <td align = left> <input type = submit name="cmdNext" value="Next"> <input type = submit name="cmdLast" value="Last"> </td> </tr> |
This is the HTML for the table row and columns that display the submit buttons that act actually are posted back to this same processing script and cause the PHP code to perform the appropriate processing. |
This is a new style of presentation for me, hopefully it helps elucidate what different segtments of code are actually doing. |