By 04/05/2016

Generate Pi Chart From MySQL Database In PHP

  • It is easy to create different types of beautiful dynamic pi charts and graphs in PHP using MySQL database.
  • These different types of pi charts are generated automatically for us from MySQL database.
  • In this tutorial i create a pi chart in PHP from MySQL database, for that purpose i have already created a database table named “sm_attendance” which stores student’s attendance such as “present” or “absent”.
  • In this tutorial i generate a pi chart which simply displays the total number of “present” and “absent” days  from “sm_attendance” table and populate these data in pi chart automatically.

Step 1:

Step 2:

  • After download these library files lets create a simple program which will achieve our this task.

Example: attendance-graph.php

<?php 

 // Following are 2 PHP pi chart libraries which is included here.
 // Make sure that you have place these both files in same directory.
 require "phpgraphlib.php";
 require "phpgraphlib_pie.php";

 // Creating a pi chart object through a constructor by passing with,height parameters.
 $graph = new PHPGraphLibPie(425, 119);
 
 $data = array("Present" => $_GET['p'], "Absent" => $_GET['a']);
 $graph->addData($data);
 $graph->setTitle('Over All Attendance Chart');
 $graph->setLabelTextColor('black');
 $graph->setLegendTextColor('black');
 $graph->setLegendColor("#E9EAED");
 $graph->setLegendOutlineColor("silver");
 $graph->createGraph();
?>

Example: index.php

<!Doctype html>
 <head>
 <title>Generate Pi Chart From MySQL Database In PHP</title>
 </head>
 <html>
 <body>
 <?php
 $link = mysql_connect('localhost', 'root', '')
 or die('Could not connect: ' . mysql_error());
 mysql_select_db('smanagement') or die('Could not select database');
 
 $sql_p = "SELECT COUNT(*) AS 'p' 
 FROM sm_attendance
 WHERE status = 'p'";
 
 $sql_a = "SELECT COUNT(*) AS 'a' 
 FROM sm_attendance
 WHERE status = 'a'";
 
 $p = mysql_query($sql_p) or die('Query failed: ' . mysql_error());
 $a = mysql_query($sql_a) or die('Query failed: ' . mysql_error());

 $row_p = mysql_fetch_assoc($p);
 $row_a = mysql_fetch_assoc($a);
 
 $present = $row_p["p"];
 $absent = $row_a["a"];
 ?>
 <!--- NOTE this img tag which renders pi chart -->
 <img src="attendance-graph.php?p=<?php echo $present;?>&a=<?php echo $absent;?>"/>
 </body>
 </html>


Program Output:

Pi Chart From MySQL Database In PHP Output

Pi Chart From MySQL Database In PHP Output







← Previous Tutorial


Posted in: PHP, Solutions, Sql

Comments are closed.

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .