Site icon FusionBrew – The FusionCharts Blog

Create Charts using PHP and MongoDB

PHP is one of the most widely used server side languages. We’ve already got detailed documentation on how PHP can be easily integrated with FusionCharts using the FusionCharts-PHP wrapper. We also had a recent blog post on how you can use the Zend Framework (a cluster of PHP packages) with FusionCharts. PHP is fairly easy to work with, w.r.t its support for many database technologies. However, there is some work involved when using PHP with a NoSQL database. However, before we move to talking about that, it is important that we brush up on what is NoSQL, for there will be many of us who aren’t familiar with this database technology. Let’s first talk about: Here goes the answers… NoSQL also referred to as ‘NOT ONLY SQL’ or ‘non relational’ database, provides a data storage and retrieval mechanism that is modeled differently from the traditional tabular relations that are used in relational databases. NoSQL is used because it encompasses a wide variety of different database technologies that were developed in response to the demands presented in building modern applications. Top NoSQL databases used today include MongoDB, Redis, Cassandra, CouchDB HBase, Neo4j, and so on. In this blog post, we’ll explore how to integrate PHP with MongoDB and create a chart using FusionCharts. MongoDB is an open-source, document-oriented database developed by MongoDB Inc. It works on the concept of document and collections, stores data in the JSON format, and can vary in structure as per the requirement. Query access is fast through the MongoDB Query Language. It uses dynamic schemas, meaning that you can create records without first defining the structure, such as the fields or the type of values.

Now that we have the basics sufficiently covered, let’s get started…

To get the code in this blog working, we need to install the following components: Make sure that all the above mentioned components are present in the system and are working as expected.

Next, we move on to how you can create the database

Step 1: Execute the command “mongod” using the shell to run the mongodb server. Step 2: Create a json file that contains the data that goes into the database and will used as the source data for the chart. We’ll name this file as fuel_price.json.
[
  {
    "month": "Jan", "petrol" : 64.72, "diesel": 52.49
  },
  {
    "month": "Feb", "petrol" : 62.81, "diesel": 50.72
  },
  {
    "month": "Mar", "petrol" : 66.18, "diesel": 54.06
  },
  {
    "month": "Apr", "petrol" : 65.17, "diesel": 51.74
  },
  {
    "month": "May", "petrol" : 72.94, "diesel": 57.23
  },
  {
    "month": "Jun", "petrol" : 73.77, "diesel": 55.83
  },
  {
    "month": "Jul", "petrol" : 70.7, "diesel": 52.59
  },
  {
    "month": "Aug", "petrol" : 66.72, "diesel": 47.54
  },
  {
    "month": "Sept", "petrol" : 64.61, "diesel": 47.02
  }
]
Step 3: Import the .json file created in Step 2 into mongodb using the command shown below.
mongoimport -d database_name -c collection_name –type json –file complete path for the json file –jsonArray
Step 4: Next, open another shell to run the command “mongo”. Now we need to verify if our data has been imported correctly into the database or no. To do this, you need to execute the following commands: With this, we are done with creating the database. Now lets move to the coding part that is a three-step process with, multiple sub-steps.

STEP I (Configuration)

Create a file and save it as config.php inside the project folder. After completion of the configuration, the php code will look like this.
< ?php
    require 'vendor\autoload.php';
    $connection = new MongoDB\Client; 
?>

STEP II

STEP III (Main Page)

If you see any errors in your code, click here to download the complete source code of the sample project we have created for this tutorial.
Exit mobile version