php and Mongodb powered blog — Part 1

Supun Dharmarathne
technodyne
Published in
4 min readJun 2, 2012

Mongodb is not a relational database. It uses JSON documents to store the data. Download mongodb from here. Extract it and place in a folder where you need. Then run the server.

Then run the monogodb client.

Since i am using php, i have downloaded mongodb php driver. Make sure to download the appropriate driver. I have used 5.3 ts VC6 because my php settings are as following.

Copy the php_mongo.dll file from the extracted folder to the PHP extension directory; this is usually the folder name ext inside your PHP installation. Then update the php.ini file with following.”extension=php_mongo.dll” and save the file and restart the server.

Now you are ready to create the application. create blogpost.php as following.

[sourcecode language=”php”]

<?php
$action = (!empty($_POST[‘btn_submit’]) &&
($_POST[‘btn_submit’] === ‘Save’)) ? ‘save_article’
: ‘show_form’;
switch($action){
case ‘save_article’:
try {
$connection = new Mongo();
Chapter 2
[ 31 ]
$database = $connection->selectDB(‘myblogsite’);
$collection = $database->
selectCollection(‘articles’);
$article = array{
‘title’ => $_POST[‘title’],
‘content’ => $_POST[‘content’],
‘saved_at’ => new MongoDate()
};
$collection->insert($article);
} catch(MongoConnectionException $e) {
die(“Failed to connect to database “.
$e->getMessage());
}
catch(MongoException $e) {
die(‘Failed to insert data ‘.$e->getMessage());
}
break;
case ‘show_form’:
default:
}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=”http://www.w3.org/1999/xhtml" xml:lang=”en”
lang=”en”>
<head>
<meta http-equiv=”Content-Type” content=”text/html;
charset=utf-8"/>
<link rel=”stylesheet” href=”style.css”/>
<title>Blog Post Creator</title>
</head>
<body>
<div id=”contentarea”>
<div id=”innercontentarea”>
<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/”>
</p>
<h3>Content</h3>
<textarea name=”content” rows=”20"></textarea>
<p>
<input type=”submit” name=”btn_submit”
value=”Save”/>
</p>
</form>
<?php else: ?>
<p>
Article saved. _id:<?php echo $article[‘_id’];?>.
<a href=”blogpost.php”>
Write another one?</a>
</p>
<?php endif;?>
</div>
</div>
</body>
</html>

[/sourcecode]

Now go to blogpost.php page in your browser.

Now you can add the post in to the blog. Then create blogs.php to view all the post in the blog.

[sourcecode language=”php”]

<?php
try {
$connection = new Mongo();
$database = $connection->selectDB(‘myblogsite’);
$collection = $database->selectCollection(‘articles’);
} catch(MongoConnectionException $e) {
die(“Failed to connect to database “.$e->getMessage());
}
$cursor = $collection->find();
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=”http://www.w3.org/1999/xhtml" xml:lang=”en”
lang=”en”>
<head>
<meta http-equiv=”Content-Type” content=”text/html;
charset=utf-8"/>
<link rel=”stylesheet” href=”style.css” />
<title>My Blog Site</title>
</head>
<body>
<div id=”contentarea”>
<div id=”innercontentarea”>
<h1>My Blogs</h1>
<?php while ($cursor->hasNext()):
$article = $cursor->getNext(); ?>
<h2><?php echo $article[‘title’]; ?></h2>
<p>
<?php echo substr($article[‘content’], 0,
200).’…’; ?>
</p>
<a href=”blog.php?id=<?php echo $article[‘_id’];
?>”>Read more</a>
<?php endwhile; ?>
</div>
</div>
</body>
</html>

[/sourcecode]

The output as follows.

Now create a blog.php to view individual post.

[sourcecode language=”php”]

<?php
$id = $_GET[‘id’];
try {
$connection = new Mongo();
$database = $connection->selectDB(‘myblogsite’);
$collection = $database->selectCollection(‘articles’);
} catch(MongoConnectionException $e) {
die(“Failed to connect to database “.$e->getMessage());
}
$article = $collection->findOne(array(‘_id’ =>
new MongoId($id)));
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=”http://www.w3.org/1999/xhtml" xml:lang=”en”
lang=”en”>
<head>
<meta http-equiv=”Content-Type” content=”text/html;
charset=utf-8"/>
<link rel=”stylesheet” href=”style.css” />
<title>My Blog Site</title>
</head>
<body>
<div id=”contentarea”>
<div id=”innercontentarea”>
<h1>My Blogs</h1>
<h2><?php echo $article[‘title’]; ?></h2>
<p><?php echo $article[‘content’]; ?></p>
</div>
</div>
</body>
</html>

[/sourcecode]

Here is the CSS code.

[sourcecode language=”css”]

body {
background-color: #e1ddd9;
font-size: 12px;
font-family: Verdana, Arial, Helvetica, SunSans-Regular,
Sans-Serif;
color:#564b47;
padding:20px;
margin:0px;
text-align: center;
}
div#contentarea {
text-align: left;
vertical-align: middle;
margin: 0px auto;
padding: 0px;
width: 550px;
background-color: #ffffff;
border: 1px #564b47;
}
div#innercontentarea{ padding: 10px 50px; }
div#innercontentarea form input[type=text]{ width: 435px; }
div#innercontentarea form textarea[name=content] { width: 435px; }

[/sourcecode]

--

--