Designing the blog editor pages
A blog isn't a blog without your own content. Here's where you'll design the adding and editing portion of your blog application. You will also develop the application to allow you to add new blog entries and view them from the main section.
Add a new blog entry, or edit/delete a current entry
This page gives you two options: add a new blog entry, or edit/delete a current entry. Define the edit/index.php file, as shown in Listing 8.
Listing 8. Defining the edit/index.php file
<?php
include('includes/functions.php');
define('DIRECTORY_LISTING', "../blogs");
require('header.html');
print "<a href='index.php?edit=EditIt'>Edit/delete current blog entries</a><br>";
print "<a href='index.php?addnew=Add New'>Add new blog entry for today</a><br>";
require('footer.html');
?>
|
Notice here that the DIRECTORY_LISTING constant is a little different because the blogs directory is in a new location in reference to the main welcome page of your blog. After the header.html and before the footer.html files are required()'d, there are two links that give you options to edit blog entries or add new content to your blog.
Next, you'll define the simple header and footer files referenced above.
These two files have the same purpose as the header and footer files for the main welcome page of the blog. Define the header file as follows:
<html><head><title>My blog editor</title></head><body>
<h1>My blog editor</h1>
<a href="..">Main blog page</a><br><br>
|
This makes it so that there will always be a link to the main welcome page of your blog, as shown in the last line above.
Now define the footer file:
</body></html> |
That completes the Web interfaces. See Figure 2 for sample browser output.
Figure 2. Editing the blog menu
Now create the functionality to add new blog entries.
Processing add new blog entry requests
Now you'll capture from the URL when a request to add a new page has been sent to your PHP application. Continue defining the edit/index.php file, as shown in Listing 9.
Listing 9. Capturing the desired command from the URL
...
if($_GET['addnew'] == 'Add New'){
displayAddNewBlogForm();
}
else{
print "<a href='index.php?edit=EditIt'>Edit/delete".
" current blog entries</a><br>";
print "<a href='index.php?addnew=Add New'>Add new blog".
" entry for today</a><br>";
}
...
|
Now instead of the links displaying again, a form allowing you to enter your daily log will be displayed. You'll define this function and its accompanying form next.
Since you'll probably want to create at least more than one blog an hour, you're going to have to index the blogs in the file system down to the minute. On top of that, you'll need to make sure you're not overwriting a previous blog entry. This function helps to accomplish all that. Define the displayAddNewBlogForm() function in edit/includes/functions.php, as shown in Listing 10.
Listing 10. Defining the
displayAddNewBlogForm() function
function displayAddNewBlogForm(){
print "<a href='index.php'>Editor Home</a><br>";
$month = date("M");
$day = date("d");
$year = date("Y");
$hour = date("H");
$minute = date("i");
print "<h2>Add new blog entry for $month $day, $year ".
"$hour:$minute</h2>";
if(is_file(DIRECTORY_LISTING."/$month $year/$month $day $year ".
"$hour $minute")){
print "You've already created a blog entry in the last ".
"minute. Try deleting or editing it instead.";
return;
}
print "<form action='index.php' method='POST'>";
print "<input type='hidden' name='blog' value='$month $year/".
"$month $day $year $hour $minute'>";
print "<input type='hidden' name='dir' value='$month $year'>";
print "<textarea name='content' cols='100' ".
"rows='15'></textarea><br>";
print "<input type='submit' name='save' value='Save'> ";
print "</form>";
}
|
First, create a link that allows you to return to the main editing page and cancel your current request to add a new blog entry. Then you get the current month, day, year, hour, and minute, which you'll use for the title information of the blog. You'll also check to make sure the blog doesn't already exist using the is_file() function. If the file doesn't exist, code execution will continue as planned and the form to add a new blog entry will be displayed. There are a few input items with type="hidden". These will allow you to propagate the directory, title, and name of the file you're going to add.
An example preview of the Add new blog entry form can be seen in Figure 3.
Figure 3. The Add new blog entry form
Next, you'll see the code that saves the new blog entry to the hard disk.
You don't want your new blog to just vanish. Here's where you'll save the file so that visitors to your blog can see what you have to say.
Continue defining the edit/index.php file by adding file writing support, as shown in Listing 11.
Listing 11. Adding file writing support to edit/index.php
...
require('header.html');
if($_POST['save'] == 'Save'){
if(!is_dir(DIRECTORY_LISTING.'/'.$_POST['dir'])){
mkdir(DIRECTORY_LISTING.'/'.$_POST['dir']);
}
$fp = fopen(DIRECTORY_LISTING.'/'.$_POST['blog'], 'w');
fwrite($fp, $_POST['content']);
fclose($fp);
print "Blog entry saved/updated<br><br>";
}
if($_GET['addnew'] == 'Add New'){
displayAddNewBlogForm();
}
...
|
If the Save button gets pressed, the save> variable in the POST array will be set to Save. If this is the case, you need to save the content of the new blog entry. Also, for new months where a directory doesn't exist, you'll need to make sure that a directory exists for the month and year that you're saving a blog for and, if the directory doesn't exist, create it. Once you've ensured that the directory exists, create a new file, write the contents passed through in the POST request, and display a confirmation back to the user.
See Figure 4 for example browser output when a new blog entry has been added.
Figure 4. Saving a new blog entry
Next, you'll go back to the main welcome page and set up functionality for viewing the blog entries there.





