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.
  • move next
    • give feedback if already at last record
  • move previous
    • give feedback if already at first record
  • move to the first record
  • move to the last record
  • display the current record position out of the total number of records

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']))
{

$_SESSION['current_row'] = 1;

}

// connecting to the database on battcave.com
$link = mysql_connect($host, $user, $password);
// constructing the query string
$query_string = "SELECT * FROM $table_name";
// executing the query
$result_set = mysql_db_query($db_name, $query_string, $link);
// obtaining the number of rows in the record set
$number_rows = mysql_num_rows($result_set);
// Now we have our code that handles the command buttons
// the block of code to move the admin to the previous record
//
// if the First button has been pressed

if ($cmdFirst != "")
{

// setting the row counter to 1
$_SESSION['current_row'] = 1;

}
// the block of code to move the admin to the previous record
// if the Previous button has been pressed

if ($cmdPrevious != "")
{

// making certain the admin is not already at the first record
if ($_SESSION['current_row'] == 1)
{

echo("<font size=5>You are already at the first record!</font>");

}
else
{

$_SESSION['current_row'] = $_SESSION['current_row'] - 1;

}

}
// the block of code to move the admin to the next record
// if the Next button has been pressed

if ($cmdNext != "")
{

// making certain the admin is not already at the last record
if ($_SESSION['current_row'] == $number_rows)
{

echo("<font size=5>You are already at the last record!</font>");

}
else
{

$_SESSION['current_row'] = $_SESSION['current_row'] + 1;

}

}
// if the Last button has been pressed
if ($cmdLast != "")
{

// setting the row counter to the last record
$_SESSION['current_row'] = $number_rows;

}
// a variable for printing
$current_row_number = $_SESSION['current_row'];
// go to this current row
mysql_data_seek($result_set, $_SESSION['current_row']);
// and retrieve it into an array
$row = mysql_fetch_row($result_set);
?>
<html>
<head>
<title>Admin Browser for User Registration Web</title>
</head>

<body bgcolor = "00bbdd" text="004466" link="004466" vlink="007799">
<form action="admin_browser.php" method=post>
<h2>Admin Browser for User Registration</h2>
<table>
<tr>
<td><font size = 4 color=004466>First Name:</font>
</td>
<td><input type=text name="txt_first_name" size=20 value = "<?php echo $row[1]; ?>">
</td>
</tr>
<tr>
<td><font size = 4 color=004466>Last Name:</font>
</td>
<td><input type=text name="txt_last_name" size=20 value = "<?php echo $row[2]; ?>">
</td>
</tr>
<tr>
<td><font size = 4 color=004466>EMail Address:</font>
</td>
<td><input type=text name="txt_email" size=50 value = "<?php echo $row[3]; ?>">
</td>
</tr>
<tr>
<td><font size = 4 color=004466>Password:</font>
</td>
<td><input type=password name="txt_password" size=50 value = "<?php echo $row[4]; ?>">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=004466>Interests:</font>
</td>
<td><input type=checkbox name="chk_php" value=1
<?php if (($row[5]) == 1 ) echo 'checked'; ?>>
<font size = 4 color=004466>PHP</font>
</td>
</tr>
<tr>
<td>
</td>
<td><input type=checkbox name="chk_jsp" value=1
<?php if (($row[6]) == 1 ) echo 'checked'; ?>>
<font size = 4 color=004466>JSP</font>
</td>
</tr>
<tr>
<td>
</td>
<td><input type=checkbox name="chk_mysql" value=1
<?php if (($row[7]) == 1 ) echo 'checked'; ?>>
<font size = 4 color=004466>MySQL</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=004466>Credit Card:</font>
</td>
<td><select name="sel_credit_card">
<option value="Discover" <?php if (($row[8]) == 'Discover') echo 'selected'; ?>>Discover
<option value="MasterCard" <?php if (($row[8]) == 'MasterCard') echo 'selected'; ?>>Mastercard
<option value="Visa" <?php if (($row[8]) == 'Visa') echo 'selected'; ?>>Visa
</select>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=004466>Education:</font>
</td>
<td><input type=radio name="rb_education" value="NoCollege"
<?php if (($row[9]) == 'NoCollege') echo 'checked'; ?>>
<font size = 4 color=004466>No College</font><br>
<input type=radio name="rb_education" value="College"
<?php if (($row[9]) == 'College') echo 'checked'; ?>>
<font size = 4 color=004466>College</font><br>
<input type=radio name="rb_education" value="Masters"
<?php if (($row[9]) == 'Masters') echo 'checked'; ?>>
<font size = 4 color=004466>Masters</font><br>
<input type=radio name="rb_education" value="PhD"
<?php if (($row[9]) == 'PhD') echo 'checked'; ?>>
<font size = 4 color=004466>Ph.D.</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<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>

<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>

</table>
</form>
<?php
mysql_close($link);
?>
</body>
</html>

 

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']))
{

$_SESSION['current_row'] = 1;

}

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 != "")
{

// setting the row counter to 1
$_SESSION['current_row'] = 1;

}

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 != "")
{

// making certain the admin is not already at the first record
if ($_SESSION['current_row'] == 1)
{

echo("<font size=5>You are already at the first record!</font>");

}
else
{

$_SESSION['current_row'] = $_SESSION['current_row'] - 1;

}

}

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 != "")
{

// making certain the admin is not already at the last record
if ($_SESSION['current_row'] == $number_rows)
{

echo("<font size=5>You are already at the last record!</font>");

}
else
{

$_SESSION['current_row'] = $_SESSION['current_row'] + 1;

}

}

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 != "")
{

// setting the row counter to the last record
$_SESSION['current_row'] = $number_rows;

}

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.