Basic Features:
- Input Page without formatting
- Blog Output Page with formatting
- User
- Entry Title
- Blog Entry
- Labels
- Date
So how did I get here. Let's look at some code. The first page to look at is the post-creater page.
The very modest looking page right now:
![]() |
Blog Input Page |
<body>
<?php
if(isset($_POST["start"]) == false){
$_POST["start"] = "y";
}
echo '<br/><p> Welcome To The Overenginered Blogger Post Creator</p> <hr\>';
?>
<?php
if($_POST["start"] === "n"){
try{
date_default_timezone_set( 'America/New_York' );
$conn = new Mongo();
$db = $conn->selectDB("blog");
$collection = $db->items;
$item =array(
'title' => $_POST['title'],
'txt' => $_POST['txt'],
'labels' => $_POST['labels'],
'user' => $_POST['user'],
'dt' => new MongoDate( strtotime( date( 'Y-m-d H:i:s')))
);
$collection->insert($item);
/// disconnect from server
$conn->close();
} catch ( MongoConnectionException $e ) {
echo '<p>Couldn\'t connect to mongodb, is the "mongo" process running?</p>';
exit();
}
}
?>
<form action="postCreator.php" method="post">Enter a post:<br/>Title:<input type="text" name="title" /><br/><textarea name="txt" cols='88' rows="12"/><br/>Labels: <input type="text" name="labels" /><br/>User: <input type="text" name="user" /><input type="hidden" name='start' value='n' /><input type="hidden" name='action' value='i' /><input type="submit" value="Blog"/></form>
</body>
</html>
The key parts of the page are:
- The first few lines which checks for the POST parameter 'start'
- The large chunk of code in the middle of the page is where I am connecting to mongodb and inserting a new record.
- The form at the bottom of the page presents a form to for user to enter a new blog entry.
The second part is where the meat of the code is. I connect to the mongoDB
$conn = new Mongo(); // connect
$db = $conn->selectDB("blog");
$collection = $db->items;
Then I insert the entry using the elements passed in using the form at the bottom of the page.
$item =array(
'title' => $_POST['title'],
'txt' => $_POST['txt'],
'labels' => $_POST['labels'],
'user' => $_POST['user'],
'dt' => new MongoDate(strtotime(date('Y-m-d H:i:s')))
);
The trickiest part for me was inserting the date. My real issue was that I didn't take enough time checking out all the documentation so I missed PHP Mongodate Class. Everything went much smoother once I found that class. I'm so used to Oracle where I can do something like
insert into table (date_column)
values to_date('01-Jan-2010','DD-Mon-YYYY');
So when I first hit the point I needed to put in a date I just slapped in some code:
'dt' => date('j-m-y h-i-s')
DESIGN FLAW #1 - WRONG DATE TYPE
By using the MongoDate class I can now sort Blog Entries by date.
Finally, the form is present to allow data entry. Its not a WYSIWYG editor (obviously). Right now I'm going with a WIKI Text Markup PEAR module to help bridge the gap. (More on that latter). This is a long post already so I'll save the Blog Output page for next time.
Until Then
Cheers,
TheSortedProgrammer
No comments:
Post a Comment