Skip to content
Snippets Groups Projects
Commit a5156152 authored by Uyen Le's avatar Uyen Le
Browse files

lab17 work in progress

parent 140ddb10
Branches
No related merge requests found
......@@ -5,20 +5,18 @@
error_reporting(E_ALL);
ini_set( "display_errors", 1 );
//Define database information
$dbInfo = "mysql:host=localhost; dbname=tle004_coursework_blog";
$dbUsername = 'tle004';
$dbPassword = 'Honguyen57';
//Must include authentication details to connect to database
include 'config.php';
//Try to create a connection with database using PDO
include 'config.php';
try {
$pdo = new PDO( $dbInfo, $dbUsername, $dbPassword );
//ensures that PDO throws an exception if there's a problem:
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
echo "Successfully connected.<br/>";
echo "<br/>";
} catch (PDOException $e) {
}
catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
......@@ -32,25 +30,26 @@
// }
$title = "Coursework Blog | tle004";
$title = "Coursework Blog: Admin";
$css="css/blog.css";
$embeddedStyle = "";
include_once "views/header.php";
include_once "views/admin/navigation.php";
include_once "views/footer.php";
$navigation = isset( $_GET['page'] );
if ( $navigation ) {
$contrl = $_GET['page']; //prepare to load corresponding controller
$contrl = $_GET['page']; //prepare to load corresponding controller (editor.php)
} else {
$contrl = "entries"; //or prepare to load default controller
$contrl = "entries"; //or prepare to load default controller (entries.php)
}
//load the controller
//load the controllers, depending on the user's selection
include_once "controllers/admin/$contrl.php";
include_once "views/footer.php";
//close connection
//close PDO connection
unset($pdo);
?>
\ No newline at end of file
<?
//Define database authentication information
$dbInfo = "mysql:host=localhost; dbname=tle004_coursework_blog";
$dbUsername = 'tle004';
$dbPassword = 'Honguyen57';
?>
\ No newline at end of file
<?
//This file displays all entries in the database in the Homepage (index.php)
//including the entry title, first 140 characters and a Read more link.
echo "hellooooo!";
include_once "models/Blog_Entry_Table.class.php";
include_once "views/list-entries-html.php";
$entryTable = new Blog_Entry_Table( $pdo );
$allEntries = $entryTable->getAllEntries(); //execute the query by invoking the getAllEntries method
//after executing the query, the PDO statement we prepared in the getAllEntries methods will contain the result set
//create a test to check it's all working well
// $firstObject = $allEntries->fetchObject(PDO::FETCH_CLASS); //an variable to hold the first object returned by the query
// print_r($firstObject); //not working?
//Showing individual entry:
//check that the user has clicked on an entry 'id'
$entryClicked = isset($_GET['id']);
if($entryClicked) {
$entryId = $_GET['id'];
$entryData = $entryTable->getEntry( $entryId );
include_once "views/entry-html.php";
echo "Showing entryies";
} else {
echo "No entry showing";
}
//Getting individual entry:
try {
$sql = "SELECT id, title, text, date_created FROM blog_entry WHERE entry_id = ?";
$data = array($id); //pass entry 'id' to the method to be executed
$statement->execute( $data );
echo "Getting individual entry successful.<br/>";
}
catch (PDOException $e) {
echo "Getting individual entry failed: " . $e->getMessage();
}
//After executing the statement the result should be one blog entry
//which you can return as an object:
$model = $statement->fetchObject();
return $model;
print_r($model);
?>
......@@ -14,11 +14,11 @@
echo "<br/>Please enter entry content (Maximum 140 characters).";
} else {
//check if the user exceeds character limit
if (strlen($_POST['entry_content']) > 140) {
echo "<br/> Please reduce characters (Max: 140).";
} else {
echo "";
}
// if (strlen($_POST['entry_content']) > 140) {
// echo "<br/> Please reduce characters (Max: 140).";
// } else {
// echo "";
// }
}
//check if editor form was submitted
......
<?
include_once "models/Blog_Entry_Table.class.php";
$entryTable = new Blog_Entry_Table( $pdo );
$allEntries = $entryTable->getAllEntries();
$allEntries = $entryTable->getAllEntries(); //execute the query by invoking the getAllEntries method
include_once "views/admin/entries-html.php";
?>
\ No newline at end of file
<?
//This file displays all entries in the database in the Homepage (index.php)
//including the entry title, first 140 characters and a Read more link.
echo "hellooooo!";
include_once "models/Blog_Entry_Table.class.php";
include_once "views/list-entries-html.php";
$entryTable = new Blog_Entry_Table( $pdo );
$allEntries = $entryTable->getAllEntries(); //execute the query by invoking the getAllEntries method
//after executing the query, the PDO statement we prepared in the getAllEntries methods will contain the result set
//create a test to check it's all working well
// $firstObject = $allEntries->fetchObject(PDO::FETCH_CLASS); //an variable to hold the first object returned by the query
// print_r($firstObject); //not working?
//Showing individual entry:
//check that the user has clicked on an entry 'id'
$entryClicked = isset($_GET['id']);
if($entryClicked) {
$entryId = $_GET['id'];
$entryData = $entryTable->getEntry( $entryId );
include_once "views/entry-html.php";
echo "Showing entry: " .$entryClicked.;
} else {
echo "No entry showing.";
}
//Getting individual entry:
try {
$sql = "SELECT id, title, text, date_created FROM blog_entry WHERE entry_id = ?";
$data = array($id); //pass entry 'id' to the method to be executed
$statement->execute( $data );
echo "Getting individual entry successful.<br/>";
}
catch (PDOException $e) {
echo "Getting individual entry failed: " . $e->getMessage();
}
//After executing the statement the result should be one blog entry
//which you can return as an object:
$model = $statement->fetchObject();
return $model;
print_r($model);
?>
<?
echo $heading = "<h1>PHP/MySQL Coursework Blog</h1>";
echo $subheading = "<h4>by tle004</h4>";
error_reporting(E_ALL);
ini_set( "display_errors", 1);
//Must include authentication details to connect to database
include 'config.php';
//Try to create a connection with database using PDO
try {
$pdo = new PDO( $dbInfo, $dbUsername, $dbPassword );
//ensures that PDO throws an exception if there's a problem:
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
echo "Successfully connected.<br/>";
echo "<br/>";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$title = "Coursework Blog: Home";
$css="css/blog.css";
$embeddedStyle = "";
include_once "views/header.php";
include_once "views/admin/navigation.php";
include_once "views/footer.php";
//Load the blog controller - this allows viewing each entries' contents
include_once "controllers/admin/blog.php";
//close PDO connection
unset($pdo);
?>
\ No newline at end of file
......@@ -27,6 +27,9 @@ class Blog_Entry_Table {
//Get all entries
public function getAllEntries () {
$entrySQL = "SELECT id, title, SUBSTRING(text, 1, 140) AS intro FROM blog_entry";
//SELECT 'columnName' AS 'aliasName'
//this is SQL Aliases: temporarily rename a table or column heading
//so that it's more readable.
$statement = $this->db->prepare( $entrySQL );
try{
$statement->execute();
......@@ -37,7 +40,6 @@ class Blog_Entry_Table {
}
return $statement;
}
}
?>
\ No newline at end of file
<?
//This file displays the individual entry
//check if required data is available
$entryDataFound = isset( $entryData );
if ( $entryDataFound === false ) {
trigger_error('views/entry-html.php needs an $entryData object');
}
//echo out the title, entry and date created
echo "<article>
<h1>$entryData->title</h1>
<div class='date'>$entryData->date_created</div>
$entryData->entry_text
</article>";
?>
\ No newline at end of file
<? //This file creates a view for the blog entries
$entriesFound = isset( $entries );
if ( $entriesFound === false ) {
trigger_error( 'views/list-entries-html.php needs $entries' );
}
$entriesHTML = "<ul id='blog-entries'>";
while ($entry = $entries->fetchObject()) { //Loop over all the blog entries
//for each entry, echo out the title and first 140 characters
//a link for 'Read more' which uses the entry 'id' to tell the blog controller
//to display only that entry
$readmoreLink = "index.php?page=blog&amp;id=$entry->id";
$entriesHTML .= "<li><h2>$entry->title</h2>
<div>$entry->intro
<p><a href='$readmoreLink'>Read more</a></p>
</div></li>";
}
$entriesHTML .= "</ul>";
echo $entriesHTML;
?>
\ No newline at end of file
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