<?php
//We've included ../Includes/FusionCharts.php and ../Includes/DBConn.php, which contains
//functions to help us easily embed the charts and connect to a database.
include("../Includes/FusionCharts.php");
include("../Includes/DBConn.php");
?>
<HTML>
   <HEAD>
      <TITLE>FusionCharts Free - Database Example</TITLE>
      <SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT>
   </HEAD>
   <BODY>
   <CENTER>
   <?php   
   //In this example, we show how to connect FusionCharts to a database.
   //For the sake of ease, we've used a MySQL database containing two
   //tables.

   //Connect to the DB
   $link = connectToDB();

   //$strXML will be used to store the entire XML document generated
   //Generate the graph element 
   $strXML = "<graph caption='Factory Output report' subCaption='By Quantity' decimalPrecision='0' showNames='1' numberSuffix=' Units' pieSliceDepth='30' formatNumberScale='0'>";

   //Fetch all factory records
   $strQuery = "select * from Factory_Master";
   $result = mysqli_query($link, $strQuery) or die(mysqli_error($link));

   //Iterate through each factory
   if ($result) {
      while($ors = mysqli_fetch_array($result)) {
         //Now create a second query to get details for this factory
         $strQuery = "select sum(Quantity) as TotOutput from Factory_Output where FactoryId=" . $ors['FactoryId'];
         $result2 = mysqli_query($link, $strQuery) or die(mysqli_error($link));
         $ors2 = mysqli_fetch_array($result2);
         //Generate <set name='..' value='..'/>
         $strXML .= "<set name='" . $ors['FactoryName'] . "' value='" . $ors2['TotOutput'] . "' />";
         //free the resultset
         mysqli_free_result($result2);
      }
   }
   mysqli_close($link);

   //Finally, close <graph> element
   $strXML .= "</graph>";

   //Create the chart - Pie 3D Chart with data from $strXML
   echo renderChart("../../FusionCharts/FCF_Pie3D.swf", "", $strXML, "FactorySum", 650, 450);
?>
</BODY>
</HTML>