See course schedule for due date.
You will store all your files in a folder called project2
and
upload your working project to our development servers,
either breimer.net
or sienacs.com
.
When naming folders and files, never use spaces.
Use underscores instead, i.e., site_core.php
View the following sample solution to under stand the big picture:
Project 2 Solution
PHP Code is naturally hidden on the server.
While you must follow the requirements below, you can apply your own CSS and reuse styles from project1. You are encourage to use content from project1. Your website will look different and unique.
The following references are helpful. Please use them.
zyBook Chapter 12 and 13.5 - 13.9
You PHP scripts should heavily utilize re-usable functions for creating typical content. To get started, we will create two core functions for generating an HTML document that uses Bootstrap's CSS and JavaScript. Bootstrap is the most widely used framework for making "mobile first" website, which are websites designed first for mobile devices.
site_core.php
project2
folder,
create a script called site_core.php
that we will use to store core HTML functions.site_core.php
:site_test.php
and add the following code
<? require_once('site_core.php'); echo_head('Site Test'); echo ' <div class="container"> <div>'; echo_foot(); ?>
style.css
and add the following code:.container { border: 1px solid gray; }
project2
folder to the server.project2
folder and finally click on site_test.php
.
Show your instructor your working page.
You will use PHP scripts to create initially empty tables for
storing content for your web pages
and asides
.
You will also create a table called has_aside
for storing
the relationship between pages
and asides
.
pages
tableproject2
folder,
create a script called site_db.php
that we will use to store database functions.<? /* ----------------------------------------------------------------------------- Connects to server, executes SQL statement, closes connection and returns MySQLi result object Input: SQL Statement (string) Output: MySQLi result object (iterator) ----------------------------------------------------------------------------- */ function run_query($sql) { $mysqli = new mysqli('localhost', 'rootname_p_admin', 'ask instructor', 'rootname_pages'); if ($mysqli->connect_errno) { die('Failed to connect to MySQL: '.$mysqli->connect_error); } $result = $mysqli->query($sql); if (!$result) { die('Error executing query: '.$mysqli->error.'<br> SQL: '.$sql); } $mysqli->close(); return $result; } ?>
pages
table to the following unique name: ea30brei_pages
.
In the instructions below, be sure to replace my username with your username.
project2
folder,
create a script called site_create_tables.php
and add the following code:<? require_once('site_db.php'); $sql = "CREATE TABLE IF NOT EXISTS `ea30brei_pages` ( `pageid` varchar(32) NOT NULL, `title` varchar(64) NOT NULL, `parent` varchar(32) DEFAULT NULL, `content` text, PRIMARY KEY (`pageid`) )"; run_query($sql); echo 'SUCCESS: The following query executed: <pre>'.$sql.'</pre>'; ?>
project2
folder to the server.project2
folder. Click on site_create_tables.php
.
Since you do not have root access to the database server, we will create a script to return all the table information.
project2
folder,
create a script called show_columns.php
and copy & paste the following code:project2
folder to the server.project2
folder. Click on show_columns.php
.
show_columns.php?table=pages
Show your instructor your working page.
asides
and has_asides
tablessite_create_tables.php
to also create your asides
table
and has_asides
table.
pages
table, add code to define a query string and call the run_query
funtiontion to create the asides
table.
Note: The asides
table is almost identical to the pages
table but
it has two differences:
pageid
, it has asideid
, which should
also be set as the primary keyparent
, it has color
, which
should also be type varchar(32)
asides
table, add code to define a query string and call the run_query
funtiontion to create the asides
table.
The query string will look exactly like this:CREATE TABLE IF NOT EXISTS `ea30brei_has_aside` ( `pageid` varchar(32) NOT NULL, `asideid` varchar(32) NOT NULL, `ord` int(11) DEFAULT NULL, PRIMARY KEY (`pageid`,`asideid`) )
pagid, asideid
. This table
implements a many-to-many relationship between the pages
and asides
. It allows
a page to have two or more asides where ord
specifies the display order.
It also allows an aside
to be used in two or more pages.pageid asideid ord csdept depthead 1 csdept location 2 csdept programs 3
pageid asideid ord people depthead 1 latest depthead 1
project2
folder to the server.project2
folder. Click on site_create_tables.php
.
show_columns.php?table=asides
and
show_columns.php?table=has_aside
to
see if the structure is correct.
Show your instructor your working page.
You will use PHP scripts to create simple forms for inserting data into
you three tables: pages
, asides
and has_aside
.
The tasks below will help you create a basic script for inserting
a new row into the pages
table. Afterwards, you
can modify this script to create working forms for inserting
new asides and for inserting new relationships between pages
and asides.
pages
formproject2
folder,
create a script called insert_page.php
and copy & paste the following code:project2
folder,
create a script called site_forms.php
and copy & paste the following code:site_forms.php
be sure to change the table name to replace my username with your username.return_input_text
and echo_input_text
,
write a functions for returning and echoing a textarea. See Bootstrap Form Controls
to understand the structure and classes used for a textarea.project2
folder,
create a script called show_table.php
and copy & paste the following code:project2
folder to the server.project2
folder. Click on insert_page.php
and try to insert some pages.
show_columns.php?table=pages
to see if the row was added.
Show your instructor that you can insert row to your pages table.
aside
forminsert_page.php
and save it as insert_aside.php
insert_aside
and echo_aside_form
site_forms.php
and use return_page_form
, echo_page_form
and insert_page
as models for creating three new functions called:
site_forms.php
be sure to change all table names to replace my username with your username.project2
folder to the server.project2
folder. Click on insert_aside.php
and try to insert some asides.
show_columns.php?table=asides
to see if the row was added.
Show your instructor that you can insert row to your asides table.
has_aside
forminsert_page.php
and save it as insert__has_aside.php
insert_has_aside
and echo_has_aside_form
site_forms.php
and use return_page_form
, echo_page_form
and insert_page
as models for creating three new functions called:
asideid
, pageid
and ord
project2
folder to the server.project2
folder. Click on insert_has_aside.php
and try to insert some asides.
show_columns.php?table=has_asides
to see if the row was added.
Show your instructor that you can insert row to your has_asides table.
Here we will create a PHP script to generate a page by passing the pageid in the URL and then fetching the page content and any aside content associated with that page.
index
scriptproject2
folder,
create a script called index.php
and copy & paste the following code:<? require_once('site_db.php'); require_once('site_core.php'); /* ----------------------------------------------------------------------------- Get the pageid from the URL and generate a page ----------------------------------------------------------------------------- */ // If the page is null, use the default pageid if ($_GET['pageid'] == null) $idpage = 'home'; else $idpage = $_GET['pageid']; // Echo the major parts of the page from head to foot echo_head('Countries of the World'); echo_content($idpage); echo_foot(); ?>
site_core.php
and copy & paste the following functions:project2
folder to the server.project2
folder.Show your instructor that you can generate webpages from your own table.
The previous lab included many functions to generate a Bootstrap navigation menu.
Add these functions to a file called site_nav.php
.
Modify the nav menu generation query so that it reads your pages table.
In your index script, require the site nav functions and call the proper function to generate your navigtion between between the head and the content.
Help will be given in class if you are completely lost.
We will create PHP scripts to delete and edit both pages and asides.
Design and implement a form that allows a user to delete pages and asides.
Design and implement a form that allows a user to edit existing pages and asides.
Make the following improvements to maximize your grade
The user should never have to type a pageid or asideid. Instead, provide a drop-down menu so the user can select existing ids.
This will be the focus of our next lab, but if you wish to try this on your own, you can modify your index script to call a function called echo_side_content that will fetch all the asides assoicated with the pageid and insert this content into a Bootstrap column after the main content.
Add custom CSS to make your web page look unique. It is OK to use styles from Project 1. Customize your aside content. For example, you can create a submenu if a page has "children" or you can use the aside color to decorate the border or text instead of the background color.
Design and implement a page that integrates all the content management functions: create, delete and edit pages and asides. In lab 10, we will learn how to secure these scripts using a session variable that is set during login.
Send the instructor an email with the URL of your project2 folder.
This project is individual. You should not share your html or css code with anyone in the class. While it may be natural for your resumes to have similar structure and styles, excessive similarity (especially in source code formatting) is a sign that you used someone else's files but changed the content. When you do this, you learn less about the syntax and rules of HTML and CSS. You are expected to author all your files from scratch and protect them so that no one can copy your hard work.