Skip to content
Snippets Groups Projects
Commit 9a7b56ab authored by Tran Le's avatar Tran Le
Browse files

lab16 complete

parent 6d0b38fd
Branches
No related merge requests found
<? <?
echo $heading = "<h1>PHP/MySQL Coursework Blog</h1>"; echo $heading = "<h1>PHP/MySQL Coursework Blog</h1>";
echo $subheading = "<h5>by tle004</h5>"; echo $subheading = "<h4>by tle004</h4>";
error_reporting(E_ALL); error_reporting(E_ALL);
ini_set( "display_errors", 1 ); ini_set( "display_errors", 1 );
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
$dbPassword = 'Honguyen57'; $dbPassword = 'Honguyen57';
//Try to create a connection with database using PDO //Try to create a connection with database using PDO
//include 'config.php'; include 'config.php';
try { try {
$pdo = new PDO( $dbInfo, $dbUsername, $dbPassword ); $pdo = new PDO( $dbInfo, $dbUsername, $dbPassword );
//ensures that PDO throws an exception if there's a problem: //ensures that PDO throws an exception if there's a problem:
...@@ -41,11 +41,9 @@ ...@@ -41,11 +41,9 @@
$navigation = isset( $_GET['page'] ); $navigation = isset( $_GET['page'] );
if ( $navigation ) { if ( $navigation ) {
//prepare to load corresponding controller $contrl = $_GET['page']; //prepare to load corresponding controller
$contrl = $_GET['page'];
} else { } else {
//or prepare to load default controller $contrl = "entries"; //or prepare to load default controller
$contrl = "controller";
} }
//load the controller //load the controller
......
INSERT INTO blog_entry(id, title, text, date_created)
VALUES (NULL, 'Hello World!', 'This is an example post.', CURRENT_TIMESTAMP);
\ No newline at end of file
<? <?
include_once "models/Blog_Entry_Table.class.php";
$entryTable = new Blog_Entry_Table( $pdo );
include_once "models/Blog_Entry_Table.class.php"; //check if editor form was submitted
$entryTable = new Blog_Entry_Table( $db ); $editorSubmitted = isset( $_POST['action'] );
if ( $editorSubmitted ) {
//check if editor form was submitted $buttonClicked = $_POST['action'];
$editorSubmitted = isset( $_POST['action'] ); $insertNewEntry = ( $buttonClicked === 'save' ); //was "save" button clicked
if ( $editorSubmitted ) {
$buttonClicked = $_POST['action']; if ( $insertNewEntry ) {
//was "save" button clicked $title = $_POST['title'];
$insertNewEntry = ( $buttonClicked === 'save' ); $entry = $_POST['entry'];
$entryTable->saveEntry( $title, $entry ); //save the new entry
if ( $insertNewEntry ) { }
$title = $_POST['title']; }
$entry = $_POST['entry'];
//save the new entry
$entryTable->saveEntry( $title, $entry );
}
}
include_once "views/admin/editor-html.php";
include_once "views/admin/editor-html.php";
?> ?>
\ No newline at end of file
<? <?
include_once "models/Blog_Entry_Table.class.php";
$entryTable = new Blog_Entry_Table( $pdo );
$allEntries = $entryTable->getAllEntries();
echo "here's the entries!"; include_once "views/admin/entries-html.php";
include_once "models/Blog_Entry_Table.class.php";
$entryTable = new Blog_Entry_Table( $db );
$allEntries = $entryTable->getAllEntries();
include_once "views/admin/entries-html.php";
?> ?>
\ No newline at end of file
DROP TABLE IF EXISTS blog_entry;
CREATE TABLE blog_entry ( CREATE TABLE blog_entry (
id INT AUTO_INCREMENT, id INT AUTO_INCREMENT,
title VARCHAR(140) NOT NULL, title VARCHAR(140) NOT NULL,
......
index.php 100644 → 100755
<?
?>
\ No newline at end of file
...@@ -10,8 +10,7 @@ class Blog_Entry_Table { ...@@ -10,8 +10,7 @@ class Blog_Entry_Table {
//Insert new entries //Insert new entries
public function saveEntry ( $title, $text ) { public function saveEntry ( $title, $text ) {
$entrySQL = "INSERT INTO blog_entry (title, text) $entrySQL = "INSERT INTO blog_entry (title, text) VALUES (?, ?)";
VALUES ( ?, ?)";
$entryStatement = $this->db->prepare( $entrySQL ); $entryStatement = $this->db->prepare( $entrySQL );
$formData = array( $title, $entry ); $formData = array( $title, $entry );
...@@ -26,7 +25,7 @@ class Blog_Entry_Table { ...@@ -26,7 +25,7 @@ class Blog_Entry_Table {
//Get all entries //Get all entries
public function getAllEntries () { public function getAllEntries () {
$entrySQL = "SELECT id, title, SUBSTRING(text, 1, 140) AS intro FROM blog_entry"; $entrySQL = "SELECT id, title, SUBSTRING(text, 1, 140) AS intro FROM blog_entry";
$statement = $this->db->prepare( $entrySQL ); $statement = $this->db->prepare( $entrySQL );
try{ try{
$statement->execute(); $statement->execute();
......
<?php <?php
$content = " $content = "<form method='post' action='admin.php?page=editor' id='editor'>
<form method='post' action='admin.php?page=editor' id='editor'> <fieldset>
<fieldset> <legend>New Entry</legend>
<legend>New Entry Submission</legend> <label>Title</label>
<label>Title</label> <input type='text' name='title' maxlength='150' />
<input type='text' name='title' maxlength='150' /> <br/>
<label>Entry</label>
<label>Entry</label> <textarea rows='10' cols='100' name='entry'></textarea>
<textarea name='entry'></textarea>
<fieldset id='editor-buttons'>
<fieldset id='editor-buttons'> <input type='submit' name='action' value='Submit' />
<input type='submit' name='action' value='save' /> </fieldset>
</fieldset> </fieldset>
</fieldset> </form>
</form> ";
";
echo $content; echo $content;
?> ?>
\ No newline at end of file
...@@ -4,7 +4,7 @@ $entriesAsHTML = "<ul>"; ...@@ -4,7 +4,7 @@ $entriesAsHTML = "<ul>";
//get the entries and display them as a list //get the entries and display them as a list
while ( $entry = $allEntries->fetchObject() ) { while ( $entry = $allEntries->fetchObject() ) {
$href = "admin.php?page=editor&amp;id=$entry->entry_id"; $href = "admin.php?page=editor&amp;id=$entry->id";
$entriesAsHTML .= "<li><a href='$href'>$entry->title</a></li>"; $entriesAsHTML .= "<li><a href='$href'>$entry->title</a></li>";
} }
$entriesAsHTML .= "</ul>"; $entriesAsHTML .= "</ul>";
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment