after completing lab 16 you can add new entries to your blog and view a list of those entries.
* 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.
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.
## 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.
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: