Question

In: Computer Science

<?php # Script 10.5 - #5 // This script retrieves all the records from the users...

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

Solutions

Expert Solution

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


Related Solutions

Write a simple PHP script. In your script use all of the following: HTML, Javascript, and...
Write a simple PHP script. In your script use all of the following: HTML, Javascript, and PHP.
Write a PHP script to display in frequency all the words in the submitted paragraph. Convert...
Write a PHP script to display in frequency all the words in the submitted paragraph. Convert the string to an array of words. Count the number of times the words are used. Hint: array_count_values($arrayname) Sort the array in frequency order. Print the array to the screen. Hint: print_r($arrayname)
Write a PHP script that checks a word or a phrase (stored in a string variable)...
Write a PHP script that checks a word or a phrase (stored in a string variable) to determine if it is a standard palindrome, a perfect palindrome, or not a palindrome at all. Also, for each letter that the word/phrase contains, count and print the number of times that each consonant and vowel is encountered. Your output will look as follows: Word/phrase: racecar Perfect palindrome Contains consonants: r - 2 c - 2 Contains vowels: a - 2 e -...
Displaying Content of an XML File Using PHP Script or Code In this week, you are...
Displaying Content of an XML File Using PHP Script or Code In this week, you are going to write the PHP script or code to read an XML file and display its content on the screen. The file you will modify is the Products page, which you created in Week 1 Project. Given the following data in XML format, modify the Products page to read and display the information in the Products page: <Product> <Item> <Name>T-Shirt</Name> <Price>12.5</Price> </Item> <Item> <Name>Pants</Name>...
Create a web page using PHP that allows the users to create an account (a username...
Create a web page using PHP that allows the users to create an account (a username and a password). There should be a login page that allows users to create an account by entering their preferred username and password. The same page should also let users login if they already have an account. If a user is logged in successfully , they should be able to enter a comment and also read the comments entered by others previously.
PHP Question: Write the PHP code to list out all of the dates of the current...
PHP Question: Write the PHP code to list out all of the dates of the current month and assign them to their given days. As an example, for the month of October 2020, the output should look something like this: October 2020       Monday: 5, 12, 19, 26 Tuesday: 6, 13, 20, 27 Wednesday: 7, 14, 21, 28 Thursday: 1, 8, 15, 22, 29 Friday: 2, 9, 16, 23, 30 Saturday: 3, 10, 17, 24, 31 Sunday: 4, 11, 18,...
I need assistance in making a simple html and php script that lets a user explor...
I need assistance in making a simple html and php script that lets a user explor the RGB color specturum. It should be able to use 6 buttons (R+,G+,B+,R-,G-,B-) that when pressed, add or subtract 2 points (ranging from 0-255) of that color from the RGB ratio. The new RGB color is then displayed in a small window/box at the bottom of the page for the user to see. This should allow the user to explore all the different colors...
The MySQL script provided in chapter 4 (murachs php & mysql) creates and populates the DB...
The MySQL script provided in chapter 4 (murachs php & mysql) creates and populates the DB used in this application(s). A look at the tables shows no connection between parent and child tables. Provide the necessary SQL for enforcing data integrity between tables: categories and products. JUST NEED SQL FOR ENFORCING ??
Write a PHP script that: 1) Has an associative array with 10 student names and test...
Write a PHP script that: 1) Has an associative array with 10 student names and test scores (0 to 100). ie. $test_scores('John' => 95, ... ); 2)Write a function to find the Average of an array. Input is an array, output is the average. Test to make sure an array is inputted. Use ARRAY_SUM and COUNT. 3)Output the test scores highest to lowest, print scores above the average in Green. Find a PHP Sort function for associative arrays, high to...
Using the combination of HTML and PHP, implement a web page where the users can upload...
Using the combination of HTML and PHP, implement a web page where the users can upload a text file, exclusively in .txt format, which contains a string of 1000 numbers, such as: 71636269561882670428252483600823257530420752963450 85861560789112949495459501737958331952853208805511 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 96983520312774506326239578318016984801869478851843 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 05886116467109405077541002256983155200055935729725 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 62229893423380308135336276614282806444486645238749 73167176531330624919225119674426574742355349194934 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 Your code should contain a PHP function that, accepting the string of 1000 numbers in input, is able to: 1) Find the 5 adjacent numbers that multiplied together...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT