In: Computer Science
<?php # Script 10.5 - #5
// This script retrieves all the records from the users
table.
// This new version allows the results to be sorted in different
ways.
$page_title = 'View the Current Users';
line 6 include('includes/header.html');
echo '<h1>Registered Users</h1>';
require('mysqli_connect.php');
// Number of records to show per page:
$display = 10;
// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric($_GET['p'])) { //
Already been determined.
$pages = $_GET['p'];
} else { // Need to determine.
// Count the number of records:
$q = "SELECT COUNT(user_id) FROM users";
$r = @mysqli_query($dbc, $q);
$row = @mysqli_fetch_array($r, MYSQLI_NUM);
$records = $row[0];
// Calculate the number of pages...
if ($records > $display) { // More than 1
page.
$pages = ceil
($records/$display);
} else {
$pages = 1;
}
} // End of p IF.
// Determine where in the database to start returning
results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
// Determine the sort...
// Default is by registration date.
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'rd';
// Determine the sorting order:
switch ($sort) {
case 'ln':
$order_by = 'last_name ASC';
break;
case 'fn':
$order_by = 'first_name ASC';
break;
case 'rd':
$order_by = 'registration_date
ASC';
break;
default:
$order_by = 'registration_date
ASC';
$sort = 'rd';
break;
}
// Define the query:
$q = "SELECT last_name, first_name, DATE_FORMAT(registration_date,
'%M %d, %Y') AS dr, user_id FROM users ORDER BY $order_by LIMIT
$start, $display";
$r = @mysqli_query($dbc, $q); // Run the query.
// Table header:
echo '<table width="60%">
<thead>
<tr>
<th
align="left"><strong>Edit</strong></th>
<th
align="left"><strong>Delete</strong></th>
<th align="left"><strong><a
href="view_users.php?sort=ln">Last
Name</a></strong></th>
<th align="left"><strong><a
href="view_users.php?sort=fn">First
Name</a></strong></th>
<th align="left"><strong><a
href="view_users.php?sort=rd">Date
Registered</a></strong></th>
</tr>
</thead>
<tbody>
';
// Fetch and print all the records....
$bg = '#eeeeee';
79 while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
echo '<tr bgcolor="' . $bg .
'">
<td align="left"><a
href="edit_user.php?id=' . $row['user_id'] .
'">Edit</a></td>
<td align="left"><a
href="delete_user.php?id=' . $row['user_id'] .
'">Delete</a></td>
<td align="left">' .
$row['last_name'] . '</td>
<td align="left">' .
$row['first_name'] . '</td>
<td align="left">' .
$row['dr'] . '</td>
</tr>
';
} // End of WHILE loop.
echo '</tbody></table>';
92 mysqli_free_result($r);
mysqli_close($dbc);
// Make the links to other pages, if necessary.
if ($pages > 1) {
echo '<br><p>';
$current_page = ($start/$display) + 1;
// If it's not the first page, make a Previous
button:
if ($current_page != 1) {
echo '<a
href="view_users.php?s=' . ($start - $display) . '&p=' . $pages
. '&sort=' . $sort . '">Previous</a> ';
}
// Make all the numbered pages:
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
echo '<a
href="view_users.php?s=' . (($display * ($i - 1))) . '&p=' .
$pages . '&sort=' . $sort . '">' . $i . '</a> ';
} else {
echo $i . '
';
}
} // End of FOR loop.
// If it's not the last page, make a Next
button:
if ($current_page != $pages) {
echo '<a
href="view_users.php?s=' . ($start + $display) . '&p=' . $pages
. '&sort=' . $sort . '">Next</a>';
}
echo '</p>'; // Close the paragraph.
} // End of links section.
Line 124 include('includes/footer.html');
?>
==============================
Warning: include(includes/header.html): failed
to open stream: No such file or directory line
6
Warning: include(): Failed opening
'includes/header.html' for inclusion (include_path='.;C:\php\pear')
on line 6
Registered Users
Warning: mysqli_fetch_array() expects parameter 1
to be mysqli_result, boolean on line 79
Warning: mysqli_free_result() expects parameter 1
to be mysqli_result, boolean on line 92
Warning: include(includes/footer.html): failed to
open stream: No such file or directory on line
124
Warning: include(): Failed opening
'includes/footer.html' for inclusion (include_path='.;C:\php\pear')
on line 124
<?php # Script 10.5 - #5
// This script retrieves all the records from the users
table.
// This new version allows the results to be sorted in different
ways.
$page_title = 'View the Current Users';
include ('../includes/header.html');
echo '<h1>Registered Users</h1>'; // Page header
require_once ('../includes/mysqli_connect.php'); // Connect to the
db.
// Number of records to show per page.
$display = 10;
// Determine how many pages there are ...
if (isset($_GET['p']) && is_numeric ($_GET['p'])) { //
Already been determined.
$pages = $_GET['p'];
} else { // Need to determine.
// Count the number of records.
$q = "SELECT COUNT(user_id) FROM
users";
$r = @mysqli_query ($dbc, $q);
$row = @mysqli_fetch_array ($r, MYSQLI_NUM);
$records = $row[0];
// Calcualte the number of pages...
if ($records > $display) { // More than 1 page.
$pages = ceil ($records/$display);
} else {
$pages = 1;
}
} // End of p IF.
// Determine where in the database to start returning
results...
if (isset($_GET['s']) && is_numeric ($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
// Determine the sort...
// Default is by registration date.
$sort = (isset($_GET['sort'])) ?
$_GET['sort'] : 'rd';
// Determine the sorting order.
switch ($sort) {
case 'ln':
$order_by = 'last_name ASC';
break;
case 'fn':
$order_by = 'first_name ASC';
break;
case 'rd':
$order_by = 'registration_date ASC';
break;
default:
$order_by = 'registration_date ASC';
$sort = 'rd';
break;
}
// Define the query:
$q = "SELECT last_name, first_name,
DATE_FORMAT(registration_date, '%M %d, %Y') AS dr, user_id FROM
users ORDER BY $order_by LIMIT $start, $display";
$r = @mysqli_query ($dbc, $q); //
run the query.
// Table header.
echo '<table align="center"
cellspacing="3" cellpadding="3" width="75%">
<tr>
<td align="left"><b>Edit</b></td>
<td align="left"><b>Delete</b></td>
<td align="left"><b><a
href="view_users_3.php?sort=ln">Last
Name</a></b></td>
<td align="left"><b><a
href="view_users_3.php?sort=fn">First
Name</a></b></td>
<td align="left"><b><a
href="view_users_3.php?sort=rd">Date
Registered</a></b></td>
</tr>
';
// Fetch and print all the records.
$bg = '#eeeeee'; // Set the initial background
color.
while ($row = mysqli_fetch_array($r,
MYSQLI_ASSOC)) {
$bg = ($bg=='#eeeeee' ?
'#ffffff' : '#eeeeee'); // Switch the background color.
echo
'<tr bgcolor="' . $bg . '">
<td align="left"><a
href="edit_user.php?id=' . $row['user_id'] .
'">Edit</a></td>
<td align="left"><a href="delete_user.php?id=' .
$row['user_id'] . '">Delete</a></td>
<td align="left">' . $row['last_name'] . '</td>
<td align="left">' . $row['first_name'] . '</td>
<td align="left">' . $row['dr'] . '</td>
</tr>
';
} // End of WHILE loop.
echo '</table>'; //Close the table.
mysqli_free_result ($r); // Free up
the resources.
mysqli_close($dbc); // Close the
database connection.
// Make the links to other pages, if necessary.
if ($pages > 1) {
echo '<br /><p>'; // Add some spacing and start a
paragraph.
$current_page = ($start/$display) + 1; // Determine what page the
script is on.
// If it's not the first page, make a previous link:
if ($current_page != 1) {
echo '<a href="view_users_3.php?s=' . ($start - $display) .
'&p=' . $pages . '&sort=' . $sort .
'">Previous</a> ';
}
// Make all the numbered pages.
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
echo '<a href="view_users_3.php?s=' . (($display * ($i - 1))) .
'&p=' . $pages . '&sort' . $sort . '">' . $i .
'</a> ';
} else {
echo $i . ' ';
}
} // End of FOR loop.
// If it's not the last page, make a Next
button.
if ($current_page != $pages) {
echo '<a
href="view_users_3.php?s=' . ($start + $display) . '&p=' .
$pages . '&sort' . $sort . '">Next</a>';
}
echo '</p>'; // Close the
paragraph.
} // End of links section.
include ('../includes/footer.html');
?>
// if it still gives an error try using "=" in the 2 instances.