Home > mongodb, NOSQL, php > php and mongodb powered blog – Part 2

php and mongodb powered blog – Part 2

In the previous post about adding and viewing the blog post. This post is about creating the dashboard for the blog. Create dashboard.php. Add the following.
<?php
 try {
 $mongodb = new Mongo();
 $articleCollection = $mongodb->myblogsite->articles;
 } catch (MongoConnectionException $e) {
 die('Failed to connect to MongoDB '.$e->getMessage());
 }
 $currentPage = (isset($_GET['page'])) ? (int) $_GET['page']
 : 1;
 $articlesPerPage = 5;
 $skip = ($currentPage - 1) * $articlesPerPage;
 $cursor = $articleCollection->find(array(),array('title',
 'saved_at'));
 $totalArticles = $cursor->count();
 $totalPages = (int) ceil($totalArticles / $articlesPerPage);
 $cursor->sort(array('saved_at'=>-1))->skip($skip)
 ->limit($articlesPerPage);
?>
<html>
 <head>
 <title>Dashboard</title>
 <link rel="stylesheet" href="style.css"/>
 <style type="text/css" media="screen">
 body { font-size: 13px; }
 div#contentarea { width : 650px; }
 </style>
 </head>
 <body>
 <div id="contentarea">
 <div id="innercontentarea">
 <h1>Dashboard</h1>
 <table class="articles" cellspacing="0"
 cellpadding="0">
 <thead>
 <tr>
 <th width="55%">Title</th>
 <th width="27%">Created at</th>
 <th width="*">Action</th>
 </tr>
 </thead>
 <tbody>
 <?php while($cursor->hasNext()):
 $article = $cursor->getNext();?>
 <tr>
 <td>
 <?php echo substr($article['title'], 0, 35)
 . '...'; ?>
 </td>
 <td>
 <?php print date('g:i a, F j',
 $article[saved_at']->sec);?>
 </td>
 <td class="url">
 <a href="blog.php?id=<?php echo $article['_id'];
?>">View
 </a>
 </td>
 </tr>
 <?php endwhile;?>
 </tbody>
 </table>
 </div>
 <div id="navigation">
 <div class="prev">
 <?php if($currentPage !== 1): ?>
 <a href="<?php echo
 $_SERVER['PHP_SELF'].'?page='.($currentPage - 1);
 ?>">Previous </a>
 <?php endif; ?>
 </div>
 <div class="page-number">
 <?php echo $currentPage; ?>
 </div>
 <div class="next">
 <?php if($currentPage !== $totalPages): ?>
 <a href="<?php echo
 $_SERVER['PHP_SELF'].'?page='.($currentPage + 1);
 ?>">Next</a>
 <?php endif; ?>
 </div>
 <br class="clear"/>
 </div>
 </div>
 </body>
</html>

Then create edit.php and delete.php pages. edit.php

</div>
<div class="separator" style="clear: both; text-align: left;">

<?php
 $action = (!empty($_POST['btn_submit']) &&
 ($_POST['btn_submit'] === 'Save')) ? 'save_article'
 : 'show_form';
 $id = $_REQUEST['id'];
 try {
 $mongodb = new Mongo();
 $articleCollection = $mongodb--->myblogsite->articles;
 } catch (MongoConnectionException $e) {
 die('Failed to connect to MongoDB '.$e->getMessage());
 }
 switch($action){
 case 'save_article':
 $article = array();
 $article['title'] = $_POST['title'];
 $article['content'] = $_POST['content'];
 $article['saved_at'] = new MongoDate();
 $articleCollection->update(array('_id' => new
 MongoId($id)),
 $article);
 break;
 case 'show_form':
 default:
 $article = $articleCollection->findOne(array('_id' =>
 new MongoId($id)));
 }
?>

<h1>Blog Post Creator</h1>
 <?php if ($action === 'show_form'): ?>
 <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
 <h3>Title</h3>
 <p><input type="text" name="title" id="title" value="<?php echo $article['title']; ?>"></p>
 <h3>Content</h3>
 <textarea name="content" rows="20"> <?php echo $article['content']; ?>
 </textarea>
 <input type="hidden" name="id" value="<?php echo
 $article['_id'];?>">
 <p>
 <input type="submit" name="btn_submit" value="Save">
 </p>
 </form>
 <?php else:?>
 <p>
 Article saved. _id: <!--?php echo $id;?-->.
 <a href="blog.php?id=<?php echo $id;?>">
 Read it.
 </a>
 </p>
 <?php endif;?>

Delete.php


<?php
 $id = $_GET['id'];
 try{

 $mongodb = new Mongo();
 $articleCollection = $mongodb--->myblogsite->articles;
 } catch (MongoConnectionException $e) {
 die('Failed to connect to MongoDB '.$e->getMessage());
 }
 $articleCollection->remove(array('_id' => new MongoId($id)));
?>

 <meta http-equiv="Content-Type" content="text/html;
 charset=utf-8">
 <link rel="stylesheet" href="css/style.css">
 <title>Blog Post Creator</title>

 <div id="contentarea">
 <div id="innercontentarea">
 <h1>Blog Post Creator</h1>
 <p>Article deleted. _id: <?php echo $id;?>.
 <a href="dashboard.php">Go back to dashboard?</a>
 </p>
 </div>
 </div>

Now go to dashboard.php

Advertisements
Categories: mongodb, NOSQL, php
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: