IS1500: Introduction to Web Development Dynamic Websites with PHP Martin Schedlbauer, Ph.D. m.schedlbauer@neu.edu
Dynamic Websites with PHP IS4300 Dynamic Websites Websites with just content pages are called static websites. Dynamic websites collect data and display pages that are generated right before being sent to the browser. IS1500 Dynamic Websites with PHP
Example of a Static Website Adding additional players requires that a web developer modify the page and then republish the page. This is time consuming, error prone, and expensive. IS1500 Dynamic Websites with PHP
Better Approach: Dynamic Pages Why not put the information into a database and then write code to extract the information and build a page programmatically? Then, to modify the page, only add additional data to the database. IS1500 Dynamic Websites with PHP
Dynamic Page Generation From http://blog.search3w.com/dynamic-to-static/hello-world/ IS1500 Dynamic Websites with PHP
Dynamic Websites with PHP Scripting Engines There are several server-side scripting engines that use different programming languages: PHP as the scripting language accessing data from a relational database, most commonly MySQL Ruby on Rails using MySQL ASP.NET using SQL Server or Microsoft Access IS1500 Dynamic Websites with PHP
Common Technology Stacks LAMP/WAMP: Linux or Windows as the server operating system Apache as the web server MySQL as the database PHP as the dynamic page scripting language IS1500 Dynamic Websites with PHP
Dynamic Websites with PHP Our Stack We are using a simplified stack requiring less programming experience: Hosted server (AwardSpace/Weebly) Hosted web development platform (AwardSapce) Database (MySQL) Dynamic page scripting language (PHP) IS1500 Dynamic Websites with PHP
Dynamic Page Generation in PHP Internet Generated HTML PHP Program HTML + PHP Script Database Browser IS1500 Dynamic Websites with PHP
Data: Definition & Generation A database is a collection of tables containing records each of which has fields. The MySQL database is a “relational database”. Administration is done through the PHPAdmin3 web tool. IS1500 Dynamic Websites with PHP
Steps for Creating Dynamic Pages create a database define tables for the database add data to the database or build a form that collects data and stores it in the tables write PHP program to build HTML page with data from database integrate PHP program into website IS1500 Dynamic Websites with PHP
Dynamic Websites with PHP Step 4: The PHP Program <?php $servername = "pdb18.awardspace.net"; $username = "1236683_bruins"; $password = "4BostonBruins"; $database = "1236683_bruins"; $port = "3306"; // Create connection $conn = mysqli_connect($servername, $username, $password, $database, $port); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "SELECT * FROM players"; $result = $conn->query($sql); if ($result->num_rows > 0) { // create table structure echo "<table border=0>"; // output data of each row: a single player with picture and name while($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td><img src='" . $row["p_image"] . "' width=100 height=120/></td>"; echo "<td valign=top style='color:darkgray'>"; echo $row["p_name"]. " (" . $row["p_pos"]. ")</td>"; echo "</tr>"; echo "</table>"; } else { echo "0 results"; ?> IS1500 Dynamic Websites with PHP
PHP: Connecting to the Database $servername = "pdb18.awardspace.net"; $username = "1236683_bruins"; $password = "4BostonBruins"; $database = "1236683_bruins"; $port = "3306"; // Create connection $conn = mysqli_connect($servername, $username, $password, $database, $port); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } IS1500 Dynamic Websites with PHP
PHP: Retrieving Data with SQL $sql = "SELECT * FROM players"; $result = $conn->query($sql); This is the name of the table you created in the MySQL database. Look at the table names using PHPAdmin3 IS1500 Dynamic Websites with PHP
PHP: Displaying the Data by Row if ($result->num_rows > 0) { // create table structure echo "<table border=0>"; // output data of each row: a single player with picture and name while($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td><img src='" . $row["p_image"] . "' width=100 height=120/></td>"; echo "<td valign=top style='color:darkgray'>"; echo $row["p_name"]. " (" . $row["p_pos"]. ")</td>"; echo "</tr>"; } echo "</table>"; This is the name of the column you specified when you created the table. <table border=0> <tr><td><img src='http://bit.ly/1CWFXUv' width=100 height=120/></td> <td valign=top style='color:darkgray'>Tuuka Rask (Goalie)</td></tr> <tr><td><img src='http://cdn.agilitycms.com/nhlpacom/27205.jpg' width=100 height=120/></td> <td valign=top style='color:darkgray'>Patrice Bergeron (Forward)</td></tr> <tr><td><img src='https://er.cloudfront.net/465499052.jpg?ts=1425829465' width=100 height=120/></td> <td valign=top style='color:darkgray'>Brett Connolly (Forward)</td></tr> </table> IS1500 Dynamic Websites with PHP
Dynamic Pages in Summary IS4300 Dynamic Pages in Summary MySQL Database 1. define database 3. Retrieve data and generate dynamic page via script 2. add data to database IS1500 Dynamic Websites with PHP
Dynamic Websites with PHP Embedding into HTML The simplest way is to add the URL as embedded <iframe> HTML tag: <iframe src="http://is1500-mysql-sandbox.boatventures.us/loadPlayers.php" width='100%'> </iframe> IS1500 Dynamic Websites with PHP
Dynamic Websites with PHP Database Checklist Tables name should not contain spaces Column names should not contain spaces Ensure that every table has a primary key which is generally a numeric ID field IS1500 Dynamic Websites with PHP
Summary, Review, & Questions… IS1500 Dynamic Websites with PHP