Advertisements

Articles about PHP

PHP is a server-side scripting language designed for web development and a very popular one. It powers many of the popular websites that had became part and parcel of our lives.

In this page, you can find articles on PHP references, PHP coding and application setup.

How I create a page in my WordPress blog that lists all my posts and groups them by month and year

I realised that there was something missing in my WordPress blog; a page that lists all the posts that I had written so far. This post documents what I had explored to give my blog a page that lists all my posts, starting from the most recent post down to the oldest post, and groups them by month and year.

Advertisements

Emulating the web production environment with an Apache HTTP server running on a Windows 7 development machine

To minimize overhead costs of my website, I would restrain myself from buying a hosted plan until my website is launch-able. In the process of developing my website, I will want my tests to be as close to the real thing as possible.

This meant that when I type in the real domain of my website in my browser location bar, my browser will connect to my development web server, instead of the web server which my domain is being parked at.

I was using Windows 7 and an instance of Apache HTTP server as my development environment when I achieved that. My Apache HTTP server was listening at port 80 for HTTP requests.

There are two main steps to achieving my objective:

  • Routing HTTP requests, made to the actual domain, to my local machine
  • Configuring Apache HTTP server to serve HTTP requests directed at the actual domain

Getting documents from MongoDB collections with PHP

We have a MongoDB server running as a windows service on the same machine as our web server. Over time, this MongoDB server had been listening on port 33333 to help us remember information that we had collected about our friends.

As time passes, our memory can hardly rival that of our MongoDB server, which was designed to help us recollect information efficiently. This post documents some proof of concept that I did for querying documents from MongoDB collection via the PHP driver.

Inserting a document into a collection in MongoDB with PHP

Assume that we have a MongoDB server installed as a windows service on the same machine as our web server. The server listens on port 33333.

We met a new friend, and we want to save some details about her. That night, before bidding farewell, she said: “Remember me, I am Mary Jane. You can write to me at mary.jane@gmail.com“.

Although that’s all we have about her, this is sufficient for us to insert a document about Mary into our MongoDB database.

Inserting documents into collections in MongoDB with PHP consists of the following steps:

  • Derive an instance of the MongoCollection class that represents the MongoDB collection to insert documents.
  • Define the document to insert.
  • Define additional options for the insert.
  • Execute the insert function of the MongoCollection instance.

Connecting to and disconnecting from a MongoDB server in PHP

The first step to manipulate data in the MongoDB ecosystem is to connect to a MongoDB server. In php, we can use the Mongo class to help us connect to one or more MongoDB servers.

For the purpose of this demonstration, let’s assume that we had installed a MongoDB server instance as a windows service which listens on port 12345. In addition, our PHP web server runs on the same machine as the MongoDB instance.