Dynamic Web Sites 2 Notes Tonight's Topics Dynamic Web sites, as opposed to static ones on which the Web was first built, are: Easier to maintain More responsive to users Can alter their content in response to differing situations. Related Concepts External files to compartmentalize code. Handling HTML forms. Site structure Directory structure works with compartmentalized code to impact site development. Impacts ease of maintenance and security... Impacts addressing, relative or absolute, of external files... Modularization Use multiple files to extract the HTML from the PHP or to separate out commonly used processes Unless the file contains code within PHP tags, PHP will treat included code as HTML (i.e., send it directly to the browser). In our example, included files separate primary HTML formatting from any PHP code. Then, the rest of the pages can have the same appearance as if they are all part of the same Web site without the need to rewrite the common HTML for each change. This creates a template system -- an easy way to make large applications consistent and manageable. For attributes such as page titles, you can use variables such as: If a block of PHP code contains only a single executable, it's common to place both it and the PHP tags on a single line. For example, Dynamic Web Sites 1 Review/Redo If you did the class exercise correctly last week, you are good. If not, you can catch up relatively quickly by following these steps: One Create a home directory and below that directory create an includes directory. Two From the Dynamic WebSites 1 page, download the style.css file and place it in the includes directory. Three From the Dynamic WebSites 1 page, download the index.txt file and place it in the home directory with a name similar to text.html. Four View the text.html web site to prove that it works and that the css style sheet applies. Five In the includes directory create and open two files: header.html footer.html Six From text.html cut everything above the "Start of the page-specific content" comment. Paste that information into the header.html file and save it. Change the page title to: Seven From text.html cut everything below the "End of the page-specific content". Paste that information into the footer.html file and save it. Nine in your home directory, create a new file called index.php Open it for editing and add these four lines at the top: Ten Copy these four lines into the bootom of the file: Eleven Cut and paste the remaining text from text.html into index.php above the include footer and below the include header. Save the file. View the presented HTML. Note that it doesn't include any of the PHP. Nor will it include any of the db connection info after that is added... MySQL Here we are going to build a database that we will later use in our dynamic website. The database will be called 'sitename.' It will have a single table called 'users.' Note, when you create the database archive the db name, the user name, and password. CREATE DATABASE sitename; CREATE TABLE users ( user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, first_name VARCHAR(20) NOT NULL, last_name VARCHAR(40) NOT NULL, email VARCHAR(60) NOT NULL, pass CHAR(40) NOT NULL, registration_date DATETIME NOT NULL, PRIMARY KEY (user_id) ); To demonstrate that the create tables command worked: SHOW TABLES; Two Different ways to INSERT a single record INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES ('Larry' 'Ullman' 'email@example.com' SHA1('mypass'), NOW( )); INSERT INTO users VALUES (NULL, 'Zoe' 'Isabella','email2@example.com',SHA1('mojito'), NOW( )); Now, lets insert multiple records INSERT INTO users (first_name, last_name, email, pass,registration_date) VALUES ('John' 'Lennon' 'john@beatles.com',SHA1('Happiness'), NOW( )), ('Paul' 'McCartney','paul@beatles.com',SHA1('letITbe'), NOW( )), ('George' 'Harrison','george@beatles.com',SHA1('something'), NOW( )), ('Ringo' 'Starr' 'ringo@beatles.com',SHA1('thisboy'), NOW( )); Another Multiple Record Insert INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES ('David', 'Jones', 'davey@monkees.com', SHA1('fasfd'), NOW()), ('Peter', 'Tork', 'peter@monkees.com', SHA1('warw'), NOW()), ('Micky', 'Dolenz', 'micky@monkees.com ', SHA1('afsa'), NOW()), ('Mike', 'Nesmith', 'mike@monkees.com', SHA1('abdfadf'), NOW()), ('David', 'Sedaris', 'david@authors.com', SHA1('adfwrq'), NOW()), ('Nick', 'Hornby', 'nick@authors.com', SHA1('jk78'), NOW()), ('Melissa', 'Bank', 'melissa@authors.com', SHA1('jhk,h'), NOW()), ('Toni', 'Morrison', 'toni@authors.com', SHA1('hdhd'), NOW()), ('Jonathan', 'Franzen', 'jonathan@authors.com', SHA1('64654'), NOW()), ('Don', 'DeLillo', 'don@authors.com', SHA1('asf8'), NOW()), ('Graham', 'Greene', 'graham@authors.com', SHA1('5684eq'), NOW()), ('Michael', 'Chabon', 'michael@authors.com', SHA1('srw6'), NOW()), ('Richard', 'Brautigan', 'richard@authors.com', SHA1('zfs654'), NOW()), ('Russell', 'Banks', 'russell@authors.com', SHA1('wwr321'), NOW()), ('Homer', 'Simpson', 'homer@simpson.com', SHA1('5srw651'), NOW()), ('Marge', 'Simpson', 'marge@simpson.com', SHA1('ljsa'), NOW()), ('Bart', 'Simpson', 'bart@simpson.com', SHA1('pwqojz'), NOW()), ('Lisa', 'Simpson', 'lisa@simpson.com', SHA1('uh6'), NOW()), ('Maggie', 'Simpson', 'maggie@simpson.com', SHA1('plda664'), NOW()), ('Abe', 'Simpson', 'abe@simpson.com', SHA1('qopkrokr65'), NOW()); To demonstrate that all inserts worked, do this query: SELECT * FROM users; Query Selected Records SELECT first_name, last_name FROM users; Query the Simpsons SELECT * FROM users WHERE last_name = 'Simpson'; Create a dynamic website Use the sitename database to build a PHP interface for interacting with the users table. Modify the Original Template (Index.php) Open Header File (header.html)
  • Home Page
  • Register
  • View Users
  • Change Password
  • link five
  • Test with Browser Connecting to MySQL Create new php file that will contain a script named 'mysqli_connect.php' Thank you!

    You are now registered. In Chapter 12 you will actually be able to log in!


    '; } else { // If it did not run OK. // Public message: echo '

    System Error

    You could not be registered due to a system error. We apologize for any inconvenience.

    '; // Debugging message: echo '

    ' . mysqli_error($dbc) . '

    Query: ' . $q . '

    '; } // End of if ($r) IF. mysqli_close($dbc); // Close the database connection. // Include the footer and quit the script: include ('includes/footer.html'); exit(); } else { // Report the errors. echo '

    Error!

    The following error(s) occurred:
    '; foreach ($errors as $msg) { // Print each error. echo " - $msg
    \n"; } echo '

    Please try again.


    '; } // End of if (empty($errors)) IF. } // End of the main Submit conditional. ?>

    Register

    First Name:

    Last Name:

    Email Address:

    Password:

    Confirm Password:

    Troubleshoot and run the register.php file. Test it by adding a record to the database.