|
|
|
## is52027c lab 17
|
|
|
|
|
|
|
|
after completing lab 16 you can add new entries to your blog and view a list of those entries.
|
|
|
|
|
|
|
|
you are
|
|
|
|
* using PDO to connect to the database
|
|
|
|
* accessing the database through methods of your Blog_antry_Table class (your model)
|
|
|
|
* using an mvc structure with views & controllers
|
|
|
|
|
|
|
|
today we will create a site homepage which lists a summary of all blog entries, and allows us to click on an individual entry to see the full content.
|
|
|
|
|
|
|
|
# step-3
|
|
|
|
|
|
|
|
## index.php
|
|
|
|
|
|
|
|
create an index.php file - your front controller for the public side of the site.
|
|
|
|
|
|
|
|
this will be very similar to admin.php, in that it will:
|
|
|
|
* have the same error reporting
|
|
|
|
* include the config file
|
|
|
|
* create a new PDO object
|
|
|
|
* include header & footer views
|
|
|
|
|
|
|
|
in step-2, the admin.php front controller chose to load the editor controller or the entries controller, depending on the user's selection.
|
|
|
|
here we will simply load a new controller; the blog controller.
|
|
|
|
```
|
|
|
|
include_once "controllers/blog.php";
|
|
|
|
```
|
|
|
|
## the getAllEntries method
|
|
|
|
|
|
|
|
We want the public homepage to show a list of all blog entries found in the database. We will display the title, the first
|
|
|
|
150 characters of the entry_text, and a Read more link for each of the blog entries.
|
|
|
|
|
|
|
|
* declare a new method in the Blog_Entry_Table class.
|
|
|
|
* use PDO prepared statements for the sql
|
|
|
|
* use the 'try/catch' stucture when executing the query
|
|
|
|
* SUBSTRING() returns a part of a string, so to return only the first part of the entry text, your $sql will look like
|
|
|
|
* what does the 'AS intro' part of the statement do? satisfy yourself that you know (i.e. you remember the idea of an alias from term 1).
|
|
|
|
```
|
|
|
|
"SELECT entry_id, title, SUBSTRING(entry_text, 1, 150) AS intro FROM blog_entry"
|
|
|
|
```
|
|
|
|
|
|
|
|
## blog controller
|
|
|
|
|
|
|
|
now we will check we can successully select all the database entries and display them on the home page.
|
|
|
|
|
|
|
|
create the file controllers/blog.php
|
|
|
|
|
|
|
|
* include the Blog_Entry_Table class
|
|
|
|
* create a 'new $entryTable'
|
|
|
|
* 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.
|
|
|
|
|
|
|
|
## test
|
|
|
|
|
|
|
|
we can use a quick test in the blog controller to check it's all working, before we go to the trouble of writing the view. create a variable to hold the first object returned by the query i.e. where $entries holds the returned result from the method, use
|
|
|
|
```
|
|
|
|
$firstObject = $entries->fetchObject();
|
|
|
|
```
|
|
|
|
then use print_r() to print out the contents of that object in the browser. check the result is what you are expecting.
|
|
|
|
|
|
|
|
it's good to test your code in small steps as you go along, rather than creating something complex that doesn't work and having to unpick it to figure out which bit is failing.
|
|
|
|
|
|
|
|
## a view for the blog entries
|
|
|
|
|
|
|
|
as you will have seen from steps 1 and 2, i'm using the variable $out in the view to hold the html until i am ready to echo it out.
|
|
|
|
this has the advantage that i can include variables in the html, which will be evaluated when it is echoed (i.e replaced with the actual values).
|
|
|
|
i'll take the same approach with all views in this project.
|
|
|
|
|
|
|
|
|
|
|
|
create a file called views/list-entries-html.php.
|
|
|
|
in the view, you need to loop over all the blog entries:
|
|
|
|
```
|
|
|
|
while ( $entry = $entries->fetchObject() )
|
|
|
|
```
|
|
|
|
for each entry, echo out the title, the intro (i.e. the first 150 characters you selected in the query).
|
|
|
|
|
|
|
|
Add a link for 'Read more' which will use the entry_id to tell the blog controller to display only that entry (this will be our next addition). The link will look like:
|
|
|
|
```
|
|
|
|
href="index.php?page=blog&id=$entry->entry_id"
|
|
|
|
```
|
|
|
|
|
|
|
|
once you've finished the view file, include it in the blog controller, and check this gives you a view of all of the entries.
|
|
|
|
|
|
|
|
# step-4
|
|
|
|
|
|
|
|
## showing individual blog entries
|
|
|
|
|
|
|
|
ok, so we want to be able to show a full individual blog entry when the user clicks our 'read more' link.
|
|
|
|
|
|
|
|
if they clicked, it is a GET request and the entry_id will be available from the url via the $_GET array.
|
|
|
|
create an if/else statement in the blog controller that checks for this.
|
|
|
|
|
|
|
|
so it should have the form
|
|
|
|
```
|
|
|
|
$entryClicked = isset( $_GET['id'] );
|
|
|
|
if ($entryClicked ) {
|
|
|
|
//do whatever
|
|
|
|
} else {
|
|
|
|
```
|
|
|
|
|
|
|
|
for the moment, just print out a message to show if the link has been clicked.
|
|
|
|
in the next step we'll add a Blog_Entry_Class method, a controller and a view for dealing with this.
|
|
|
|
|
|
|
|
## getting an individual entry
|
|
|
|
|
|
|
|
this is simply a matter of selecting the blog entry who's id matches the one selected.
|
|
|
|
but we'll be using prepared statements, so your sql will have a placeholder for the actual value
|
|
|
|
```
|
|
|
|
$sql = "SELECT entry_id, title, entry_text, date_created FROM blog_entry WHERE entry_id = ?";
|
|
|
|
```
|
|
|
|
|
|
|
|
you will pass the id to the method, which will then use it to execute the prepared statement.
|
|
|
|
we can bind this using a separate step, or we can simply pass it as an array to the prepared statement i.e.
|
|
|
|
```
|
|
|
|
$data = array($id);
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```
|
|
|
|
$statement->execute( $data );
|
|
|
|
```
|
|
|
|
|
|
|
|
Remember to wrap this in a try/catch block.
|
|
|
|
|
|
|
|
After executing the statement the result should be one blog entry, which you can return as an object:
|
|
|
|
```
|
|
|
|
$model = $statement->fetchObject();
|
|
|
|
return $model;
|
|
|
|
```
|
|
|
|
|
|
|
|
before making the view, use print_r() on that returned object again to make sure everything is working ok.
|
|
|
|
|
|
|
|
## an entry view
|
|
|
|
|
|
|
|
create the file views/entry-html.php
|
|
|
|
|
|
|
|
all this needs to do is echo out the title, entry and date created, wrapped in some appropriate html.
|
|
|
|
|
|
|
|
once the view is done, you can include both the call to the method and the view in the loop you wrote in to the blog controller.
|
|
|
|
i.e. inside the $entryClicked loop, you'll have
|
|
|
|
|
|
|
|
```
|
|
|
|
$entryId = $_GET['id'];
|
|
|
|
$entryData = $entryTable->getEntry( $entryId );
|
|
|
|
include_once "views/entry-html.php";
|
|
|
|
```
|
|
|
|
|
|
|
|
check this works ok.
|
|
|
|
|