From 0d34d5f80a6c0aaab9c4c76d8ac74c4922ed908d Mon Sep 17 00:00:00 2001 From: Benjamin Wells <ma301bw@gold.ac.uk> Date: Fri, 22 Apr 2016 13:13:38 +0100 Subject: [PATCH] another update --- blog/admin.php | 55 +++++++++++++++++++++++++ blog/controllers/.DS_Store | Bin 0 -> 6148 bytes blog/controllers/._.DS_Store | Bin 0 -> 4096 bytes blog/controllers/._blog.php | Bin 0 -> 4096 bytes blog/controllers/admin/editor.php | 24 +++++++++++ blog/controllers/admin/entries.php | 13 ++++++ blog/controllers/blog.php | 20 +++++++++ blog/coursework-blog.sql | 9 ++++ blog/css/.Rhistory | 0 blog/css/blog.css | 25 +++++++++++ blog/index.php | 20 +++++++++ blog/list-entries-html.php | 30 ++++++++++++++ blog/models/Blog_Entry_Table.class.php | 42 +++++++++++++++++++ blog/views/.DS_Store | Bin 0 -> 6148 bytes blog/views/._.DS_Store | Bin 0 -> 4096 bytes blog/views/._entry-html.php | Bin 0 -> 4096 bytes blog/views/._list-entries-html.php | Bin 0 -> 4096 bytes blog/views/admin/admin-navigation.php | 13 ++++++ blog/views/admin/editor-html.php | 26 ++++++++++++ blog/views/admin/entries-html.php | 21 ++++++++++ blog/views/entry-html.php | 17 ++++++++ blog/views/footer.php | 11 +++++ blog/views/header.php | 15 +++++++ blog/views/list-entries-html.php | 26 ++++++++++++ 24 files changed, 367 insertions(+) create mode 100755 blog/admin.php create mode 100755 blog/controllers/.DS_Store create mode 100755 blog/controllers/._.DS_Store create mode 100755 blog/controllers/._blog.php create mode 100755 blog/controllers/admin/editor.php create mode 100755 blog/controllers/admin/entries.php create mode 100755 blog/controllers/blog.php create mode 100755 blog/coursework-blog.sql create mode 100755 blog/css/.Rhistory create mode 100755 blog/css/blog.css create mode 100755 blog/index.php create mode 100755 blog/list-entries-html.php create mode 100755 blog/models/Blog_Entry_Table.class.php create mode 100755 blog/views/.DS_Store create mode 100755 blog/views/._.DS_Store create mode 100755 blog/views/._entry-html.php create mode 100755 blog/views/._list-entries-html.php create mode 100755 blog/views/admin/admin-navigation.php create mode 100755 blog/views/admin/editor-html.php create mode 100755 blog/views/admin/entries-html.php create mode 100755 blog/views/entry-html.php create mode 100755 blog/views/footer.php create mode 100755 blog/views/header.php create mode 100755 blog/views/list-entries-html.php diff --git a/blog/admin.php b/blog/admin.php new file mode 100755 index 0000000..d377b0f --- /dev/null +++ b/blog/admin.php @@ -0,0 +1,55 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <title>Benjamin Wells Blog</title> + <link rel="stylesheet" href="style.css"> +</head> +<body> + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +error_reporting( E_ALL ); +ini_set( "display_errors", 1 ); + +include_once "views/admin/admin-navigation.php"; + +//my myphp credentials that i use to connet to the database +$dbInfo = "mysql:host=igor.gold.ac.uk;dbname=ma301bw_myblog"; +$dbUser = "ma301bw"; +$dbPassword = "workdal1"; + + +try{ +//connecting to database using pdo +$db = new PDO( $dbInfo, $dbUser, $dbPassword ); +$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); + echo "<h1>Successfully Connected!</h1>"; + +}catch ( Exception $e ) { +//"connection failed!" is printed if you do not successfully connect to the database + echo "<h1>Connection failed!</h1><p>$e</p>"; +} + +$title = "PHP/MySQL blog demo"; +$css="css/blog.css"; +$embeddedStyle = ""; + +include_once "views/header.php"; +include_once "views/admin/admin-navigation.php"; + +$navigation = isset( $_GET['page'] ); +if ( $navigation ) { + //prepare to load corresponding controller + $contrl = $_GET['page']; +} else { + //or prepare to load default controller + $contrl = "Entries"; +} +//load the controller +include_once "controllers/admin/$contrl.php"; + +include_once "views/footer.php"; + +?> diff --git a/blog/controllers/.DS_Store b/blog/controllers/.DS_Store new file mode 100755 index 0000000000000000000000000000000000000000..83bf3a8c0f021b5b21b0aae9024a9da31062006a GIT binary patch literal 6148 zcmeH~J#GR)427QqNRenLDW}N+xWNdK6L0~3wm~9LMCs?~dG@$rlNzniv*dYW&v<{n zVrLD&wm-uLSOIvayW+!#ks0F!ju`NT3vR>r`FgtFub#$R?)8AqYdo&!vP1+#Km<fU z1Vms$1mY0q`M;ddGwD%8Km?{iz`qZL?pjk@*Z6dBh!%i4XF81Y=q0Gd6V#g8x-vtv z>>eyjE!q&zM>)0RzM9&)_HtM@AC`AE?_y}y%VC89&1#4S5fFh9fmx4Ne*S;b|Lgxp zB??7A1YV4Q&3F6VmM@iO>z~*2{2{YGZ**#G=kV|oz`&2<H9d^$#V6F7+PX4B(~m%4 K&>#X&CGZQyP7{3q literal 0 HcmV?d00001 diff --git a/blog/controllers/._.DS_Store b/blog/controllers/._.DS_Store new file mode 100755 index 0000000000000000000000000000000000000000..95109874405d38e9c6dd139982f20673c2a03e88 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIk*Y|peR=07!ne6 zp1)uQL<d6!Rz;(NqaiRF0;3@?8UmvsFd71*Aut*OqaiRF0;3@?8UmvsK;;l%1eyoJ zU?3Ngky)%zlv<o$T9llskd|MRt&o{llA2eNnV*-KlT)dXlbTkdkd%{{mksRdLUj#k P3f2E`ugEaS{r?XDosAxR literal 0 HcmV?d00001 diff --git a/blog/controllers/._blog.php b/blog/controllers/._blog.php new file mode 100755 index 0000000000000000000000000000000000000000..dde4dbc63200b28fccb3f1bdad143dd6d8ae738b GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103v`rqSs z7NUcpf`IZ->Cq4v4S~@R7!85Z5Eu=C(GVC7fzc2c4S~@R7!85Z5THs3Fak{hVK9&j z$;d2LC`v8PFD*(=RY=P(%2vqCD@n~O$;{77%*m-#$Vp8rQAo;3%*zJ$g`v8JG==Ja MxL0Ht<o^E$0F#;?EC2ui literal 0 HcmV?d00001 diff --git a/blog/controllers/admin/editor.php b/blog/controllers/admin/editor.php new file mode 100755 index 0000000..ed55774 --- /dev/null +++ b/blog/controllers/admin/editor.php @@ -0,0 +1,24 @@ +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +include_once "blog/models/Blog_Entry_Table.class.php"; +$entryTable = new Blog_Entry_Table( $db ); + +//was editor form submitted? +$editorSubmitted = isset( $_POST['action'] ); +if ( $editorSubmitted ) { + $buttonClicked = $_POST['action']; + //was "save" button clicked + $insertNewEntry = ( $buttonClicked === 'save' ); + + if ( $insertNewEntry ) { + $title = $_POST['title']; + $entry = $_POST['entry']; + //save the new entry + $entryTable->saveEntry( $title, $entry ); + } +} + +include_once "blog/views/admin/editor-html.php"; + +?> \ No newline at end of file diff --git a/blog/controllers/admin/entries.php b/blog/controllers/admin/entries.php new file mode 100755 index 0000000..c6587c7 --- /dev/null +++ b/blog/controllers/admin/entries.php @@ -0,0 +1,13 @@ + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +//In entries.php, i include my Blog_Entry_Table class +include_once "models/Blog_Entry_Table.class.php"; +//i then instigate a Blog_Entry_Table Object and then the object assign it to a variable +//using the getAllEntries method +$entryTable = new Blog_Entry_Table( $db ); +$allEntries = $entryTable->getAllEntries(); + +include_once "views/admin/entries-html.php"; +?> \ No newline at end of file diff --git a/blog/controllers/blog.php b/blog/controllers/blog.php new file mode 100755 index 0000000..f463536 --- /dev/null +++ b/blog/controllers/blog.php @@ -0,0 +1,20 @@ +<? +//code excerpts taken from dans examples on gitlab and sources on the VLE + + +//This selects all the database entries and displays them on the home page of the blog +include_once "models/Blog_Entry_Table.class.php"; +$entryTable = new Blog_Entry_Table( $db ); + +$entryClicked = isset( $_GET['id'] ); +if ($entryClicked ) { + $entryId = $_GET['id']; + $entryData = $entryTable->getEntry( $entryId ); +// print_r($entryData); + include_once "views/entry-html.php"; +} else { + + $entries = $entryTable->getallentries(); + include_once "views/list-entries-html.php"; + +?> \ No newline at end of file diff --git a/blog/coursework-blog.sql b/blog/coursework-blog.sql new file mode 100755 index 0000000..9331ce5 --- /dev/null +++ b/blog/coursework-blog.sql @@ -0,0 +1,9 @@ +DROP TABLE IF EXISTS blog_entry; +//this creates the neccesary table for you to add blog posts +CREATE TABLE blog_entry ( +entry_id INT NOT NULL AUTO_INCREMENT, +title VARCHAR(140), +entry_text TEXT, +date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, +PRIMARY KEY (entry_id), +); diff --git a/blog/css/.Rhistory b/blog/css/.Rhistory new file mode 100755 index 0000000..e69de29 diff --git a/blog/css/blog.css b/blog/css/blog.css new file mode 100755 index 0000000..0b0bcab --- /dev/null +++ b/blog/css/blog.css @@ -0,0 +1,25 @@ +//code excerpts taken from dans examples on gitlab and sources on the VLE + +form#editor{ + width: 300px; + margin:0px; + padding:0px; +} + +form#editor label, form#editor input[type='text']{ + display:block; +} + +form#editor #editor-buttons{ + border:none; + text-align:right; +} + +form#editor textarea, form#editor input[type='text']{ + width:90%; + margin-bottom:2em; +} + +form#editor textarea{ + height:10em; +} \ No newline at end of file diff --git a/blog/index.php b/blog/index.php new file mode 100755 index 0000000..fd196b7 --- /dev/null +++ b/blog/index.php @@ -0,0 +1,20 @@ +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +error_reporting( E_ALL ); +ini_set( "display_errors", 1 ); + +include_once "../../../coursework_blog_config.php"; +$db = new PDO( $dbInfo, $dbUser, $dbPassword ); +$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); + +$title = "PHP/MySQL blog demo"; +$css="css/blog.css"; +$embeddedStyle = ""; +include_once "views/header.php"; + +include_once "controllers/blog.php"; + +include_once "views/footer.php"; + +?> \ No newline at end of file diff --git a/blog/list-entries-html.php b/blog/list-entries-html.php new file mode 100755 index 0000000..b891eac --- /dev/null +++ b/blog/list-entries-html.php @@ -0,0 +1,30 @@ + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +$entriesFound = isset( $entries ); +if ( $entriesFound === false ) { + trigger_error( 'views/list-entries-html.php needs $entries' ); +} + +$entriesHTML = "<ul id='blog-entries'>"; + +//this while loop will loop over every blog entry and will print/echo the intro and title +//for each blog post + +//the read more link will tell the controller to show that specific blog entry only +while ( $entry = $entries->fetchObject() ) { + $href = "index.php?page=blog&id=$entry->entry_id"; + //create an <li> for each of the entries + $entriesHTML .= "<li> + <h2>$entry->title</h2> + <div>$entry->intro + <p><a href='$href'>Read more</a></p> + </div> + </li>"; +} +$entriesHTML .= "</ul>"; + +echo $entriesHTML; + +?> \ No newline at end of file diff --git a/blog/models/Blog_Entry_Table.class.php b/blog/models/Blog_Entry_Table.class.php new file mode 100755 index 0000000..77c0393 --- /dev/null +++ b/blog/models/Blog_Entry_Table.class.php @@ -0,0 +1,42 @@ + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +class Blog_Entry_Table { + private $db; + + + public function __construct ( $db ) { + $this->db = $db; + } + + public function saveEntry ( $title, $entry ) { + $entrySQL = "INSERT INTO blog_entry ( title, entry_text ) + VALUES ( ?, ?)"; + $entryStatement = $this->db->prepare( $entrySQL ); + $formData = array( $title, $entry ); + try{ + $entryStatement->execute( $formData ); + } catch (Exception $e){ + $msg = "<p>You tried to run this sql: $entrySQL<p> + <p>Exception: $e</p>"; + trigger_error($msg); + } + } + + public function getAllEntries () { + $entrySQL = "SELECT entry_id, title, SUBSTRING(entry_text, 1, 140) AS intro FROM blog_entry"; + $statement = $this->db->prepare( $entrySQL ); + try{ + $statement->execute(); + } catch (Exception $e){ + $msg = "<p>You tried to run this sql: $entrySQL<p> + <p>Exception: $e</p>"; + trigger_error($msg); + } + return $statement; + } + + +} +?> \ No newline at end of file diff --git a/blog/views/.DS_Store b/blog/views/.DS_Store new file mode 100755 index 0000000000000000000000000000000000000000..83bf3a8c0f021b5b21b0aae9024a9da31062006a GIT binary patch literal 6148 zcmeH~J#GR)427QqNRenLDW}N+xWNdK6L0~3wm~9LMCs?~dG@$rlNzniv*dYW&v<{n zVrLD&wm-uLSOIvayW+!#ks0F!ju`NT3vR>r`FgtFub#$R?)8AqYdo&!vP1+#Km<fU z1Vms$1mY0q`M;ddGwD%8Km?{iz`qZL?pjk@*Z6dBh!%i4XF81Y=q0Gd6V#g8x-vtv z>>eyjE!q&zM>)0RzM9&)_HtM@AC`AE?_y}y%VC89&1#4S5fFh9fmx4Ne*S;b|Lgxp zB??7A1YV4Q&3F6VmM@iO>z~*2{2{YGZ**#G=kV|oz`&2<H9d^$#V6F7+PX4B(~m%4 K&>#X&CGZQyP7{3q literal 0 HcmV?d00001 diff --git a/blog/views/._.DS_Store b/blog/views/._.DS_Store new file mode 100755 index 0000000000000000000000000000000000000000..51f491048ba8d50f851a5db01e6bb9e8ebf11e93 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIk*Y|peR=07!ndR z|M&Ba5FHE^SQU*5j)uT!2#kinXb6mkz-S1JhQMeDjE2By2#kinXb6mk0F^_45ojI= zgMnN~MrN@>QEG91X;E^jLRx-NwnAoJNorn6W`15`PEMslPHI|-LQ+m*UN*3+3)MBG PDOCT%y&}UP_y0ct>U1B- literal 0 HcmV?d00001 diff --git a/blog/views/._entry-html.php b/blog/views/._entry-html.php new file mode 100755 index 0000000000000000000000000000000000000000..72b98c31e375da284302b40a77cfb448f9dad89b GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103vk`1c+ z4AH?*K|uMa^k@i-hQMeDjE2By2#kinXb6mkz-S1JhQMeDjE2By2v8*i7=fmMFc`>% zWMmdA6r~pDmlh?bDx~EXWh-Rnm89mCWaj53=Hyf=<fNvRC?w@1=4Au>!cbj9nnLwI M+$%B+a{vDW0Crv-W&i*H literal 0 HcmV?d00001 diff --git a/blog/views/._list-entries-html.php b/blog/views/._list-entries-html.php new file mode 100755 index 0000000000000000000000000000000000000000..656b8332b0100b2d68fb85ad66268d20ccbee345 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103vdT({k z1EPbWf`IZ->Cq4v4S~@R7!85Z5Eu=C(GVC7fzc2c4S~@R7!85Z5THs3Fak{hVK9&j z$;d2LC`v8PFD*(=RY=P(%2vqCD@n~O$;{77%*m-#$Vp8rQAo;3%*zJ$g`v8JG==Ja MxL0Ht<o^E$07r=(#Q*>R literal 0 HcmV?d00001 diff --git a/blog/views/admin/admin-navigation.php b/blog/views/admin/admin-navigation.php new file mode 100755 index 0000000..ec466a8 --- /dev/null +++ b/blog/views/admin/admin-navigation.php @@ -0,0 +1,13 @@ + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +$out = " +<nav id='admin-navigation'> + <a href='admin.php?page=entries'>All entries</a> + <a href='admin.php?page=editor'>Editor</a> +</nav>"; + +echo $out; + +?> \ No newline at end of file diff --git a/blog/views/admin/editor-html.php b/blog/views/admin/editor-html.php new file mode 100755 index 0000000..e09a10a --- /dev/null +++ b/blog/views/admin/editor-html.php @@ -0,0 +1,26 @@ + +<?php +//code excerpts taken from dans lab examples on gitlab and sources on the VLE + +//loads the blog entry in to the form, enabling you to edit and delete it +$out = " +<form method='post' action='admin.php?page=editor' id='editor'> + <input type='hidden' name='id' value='$entryData->entry_id' /> + <fieldset> + <legend>New Entry Submission</legend> + <label>Title</label> + <input type='text' name='title' maxlength='150' value='$entryData->title' required /> + + <label>Entry</label> + <textarea name='entry'>$entryData->entry_text</textarea> + + <fieldset id='editor-buttons'> + <input type='submit' name='action' value='save' /> + </fieldset> + </fieldset> +</form> +"; + +echo $out; + +?> \ No newline at end of file diff --git a/blog/views/admin/entries-html.php b/blog/views/admin/entries-html.php new file mode 100755 index 0000000..2820449 --- /dev/null +++ b/blog/views/admin/entries-html.php @@ -0,0 +1,21 @@ + + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +if ( isset( $allEntries ) === false ) { +trigger_error('views/admin/entries-html.php needs $allEntries'); +} +$entriesAsHTML = "<ul>"; +// this while loop gets the blog entries and displays them in a list. +while ( $entry = $allEntries->fetchObject() ) { + $href = "admin.php?page=editor&id=$entry->entry_id"; + $entriesAsHTML .= "<li><a href='$href'>$entry->title</a></li>"; +} + +$entriesAsHTML .= "</ul>"; + +//this echo then outputs the entries to the screen. +echo $entriesAsHTML; + +?> \ No newline at end of file diff --git a/blog/views/entry-html.php b/blog/views/entry-html.php new file mode 100755 index 0000000..7bdc8cf --- /dev/null +++ b/blog/views/entry-html.php @@ -0,0 +1,17 @@ +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +//the entry-html.php file essentially echos the date title and entry that have been +// created +//check if required data is available +$entryDataFound = isset( $entryData ); +if ( $entryDataFound === false ) { + trigger_error('views/entry-html.php needs an $entryData object'); +} +//properties available in $entry: entry_id, title, entry_text, 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 diff --git a/blog/views/footer.php b/blog/views/footer.php new file mode 100755 index 0000000..58d6ef5 --- /dev/null +++ b/blog/views/footer.php @@ -0,0 +1,11 @@ + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +$out = " +</body> +</html> +"; + +echo $out; +?> \ No newline at end of file diff --git a/blog/views/header.php b/blog/views/header.php new file mode 100755 index 0000000..7bb615d --- /dev/null +++ b/blog/views/header.php @@ -0,0 +1,15 @@ + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +$out = "<!DOCTYPE html> +<html> + <head> + <title>$title</title> + <meta http-equiv='Content-Type' content='text/html;charset=utf-8' /> + <link rel='stylesheet' type='text/css' href='$css'> + <link rel='stylesheet' type='text/css' href='$embeddedStyle'> + </head>"; + +echo $out; +?> \ No newline at end of file diff --git a/blog/views/list-entries-html.php b/blog/views/list-entries-html.php new file mode 100755 index 0000000..c81df28 --- /dev/null +++ b/blog/views/list-entries-html.php @@ -0,0 +1,26 @@ + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +$entriesFound = isset( $entries ); +if ( $entriesFound === false ) { + trigger_error( 'views/list-entries-html.php needs $entries' ); +} + +$entriesHTML = "<ul id='blog-entries'>"; + +while ( $entry = $entries->fetchObject() ) { + $href = "index.php?page=blog&id=$entry->entry_id"; + //create an <li> for each of the entries + $entriesHTML .= "<li> + <h2>$entry->title</h2> + <div>$entry->intro + <p><a href='$href'>Read more</a></p> + </div> + </li>"; +} +$entriesHTML .= "</ul>"; + +echo $entriesHTML; + +?> \ No newline at end of file -- GitLab