Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Ifrah Shahid
lab-exercises
Commits
c4f529dc
Commit
c4f529dc
authored
Mar 02, 2016
by
danmcquillan
Browse files
adding coursework-blog steps 3,4,5
parent
45a2b1e6
Changes
45
Hide whitespace changes
Inline
Side-by-side
coursework-blog/step-3/admin.php
0 → 100644
View file @
c4f529dc
<?php
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
"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"
;
?>
coursework-blog/step-3/controllers/admin/editor.php
0 → 100644
View file @
c4f529dc
<?php
include_once
"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
"views/admin/editor-html.php"
;
?>
coursework-blog/step-3/controllers/admin/entries.php
0 → 100644
View file @
c4f529dc
<?
include_once
"models/Blog_Entry_Table.class.php"
;
$entryTable
=
new
Blog_Entry_Table
(
$db
);
$allEntries
=
$entryTable
->
getAllEntries
();
include_once
"views/admin/entries-html.php"
;
?>
coursework-blog/step-3/controllers/blog.php
0 → 100644
View file @
c4f529dc
<?
include_once
"models/Blog_Entry_Table.class.php"
;
$entryTable
=
new
Blog_Entry_Table
(
$db
);
$entries
=
$entryTable
->
getallentries
();
include_once
"views/list-entries-html.php"
;
?>
coursework-blog/step-3/coursework-blog.sql
0 → 100644
View file @
c4f529dc
-- this will create a table for blog entries
CREATE
TABLE
blog_entry
(
entry_id
INT
NOT
NULL
AUTO_INCREMENT
,
title
VARCHAR
(
150
),
entry_text
TEXT
,
date_created
TIMESTAMP
DEFAULT
CURRENT_TIMESTAMP
,
PRIMARY
KEY
(
entry_id
)
)
coursework-blog/step-3/css/blog.css
0 → 100644
View file @
c4f529dc
/* code listing for blog/css/blog.css */
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
;
}
coursework-blog/step-3/index.php
0 → 100644
View file @
c4f529dc
<?php
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"
;
?>
coursework-blog/step-3/models/Blog_Entry_Table.class.php
0 → 100644
View file @
c4f529dc
<?php
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, 150) 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
;
}
}
?>
coursework-blog/step-3/views/admin/admin-navigation.php
0 → 100644
View file @
c4f529dc
<?php
$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
;
?>
coursework-blog/step-3/views/admin/editor-html.php
0 → 100644
View file @
c4f529dc
<?php
$out
=
"
<form method='post' action='admin.php?page=editor' id='editor'>
<fieldset>
<legend>New Entry Submission</legend>
<label>Title</label>
<input type='text' name='title' maxlength='150' />
<label>Entry</label>
<textarea name='entry'></textarea>
<fieldset id='editor-buttons'>
<input type='submit' name='action' value='save' />
</fieldset>
</fieldset>
</form>
"
;
echo
$out
;
?>
coursework-blog/step-3/views/admin/entries-html.php
0 → 100644
View file @
c4f529dc
<?php
$entriesAsHTML
=
"<ul>"
;
while
(
$entry
=
$allEntries
->
fetchObject
()
)
{
$href
=
"admin.php?page=editor&id=
$entry->entry_id
"
;
$entriesAsHTML
.
=
"<li><a href='
$href
'>
$entry->title
</a></li>"
;
}
$entriesAsHTML
.
=
"</ul>"
;
echo
$entriesAsHTML
;
?>
coursework-blog/step-3/views/footer.php
0 → 100644
View file @
c4f529dc
<?php
$out
=
"
</body>
</html>
"
;
echo
$out
;
?>
coursework-blog/step-3/views/header.php
0 → 100644
View file @
c4f529dc
<?php
$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
;
?>
coursework-blog/step-3/views/list-entries-html.php
0 → 100644
View file @
c4f529dc
<?php
$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
;
?>
coursework-blog/step-4/admin.php
0 → 100644
View file @
c4f529dc
<?php
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
"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"
;
?>
coursework-blog/step-4/controllers/admin/editor.php
0 → 100644
View file @
c4f529dc
<?php
include_once
"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
"views/admin/editor-html.php"
;
?>
coursework-blog/step-4/controllers/admin/entries.php
0 → 100644
View file @
c4f529dc
<?
include_once
"models/Blog_Entry_Table.class.php"
;
$entryTable
=
new
Blog_Entry_Table
(
$db
);
$allEntries
=
$entryTable
->
getAllEntries
();
include_once
"views/admin/entries-html.php"
;
?>
coursework-blog/step-4/controllers/blog.php
0 → 100644
View file @
c4f529dc
<?
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"
;
}
?>
coursework-blog/step-4/coursework-blog.sql
0 → 100644
View file @
c4f529dc
-- this will create a table for blog entries
CREATE
TABLE
blog_entry
(
entry_id
INT
NOT
NULL
AUTO_INCREMENT
,
title
VARCHAR
(
150
),
entry_text
TEXT
,
date_created
TIMESTAMP
DEFAULT
CURRENT_TIMESTAMP
,
PRIMARY
KEY
(
entry_id
)
)
coursework-blog/step-4/css/blog.css
0 → 100644
View file @
c4f529dc
/* code listing for blog/css/blog.css */
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
;
}
Prev
1
2
3
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment