开发者

Get Data from Database

开发者 https://www.devze.com 2023-03-20 14:21 出处:网络
I have implemented Fusion charts into Codeigniter framework with following view. I am creating line chart with provided data in view but, I would like to retrieve this data from same structured databa

I have implemented Fusion charts into Codeigniter framework with following view. I am creating line chart with provided data in view but, I would like to retrieve this data from same structured database table.

Is there anyway to do it? I will be really appreciated if someone can help me.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>
<?php

class Chart extends CI_Controller {

    function __construct()
       {
          session_start();
          parent::__construct();
          if ( !isset($_SESSION['username']) ) {
             redirect('admin');
          }
       }

    public function index() {


        $this->load->helper(array('url','fusioncharts')) ;

        $graph_swfFile      = base_url().'public/flash/Line.swf' ;
        $graph_caption      = 'Results' ;
        $graph_numberPrefix = '€ ' ;
        $graph_title        = 'Results' ;
        $graph_width        = 600 ;
        $graph_height       = 250 ;

        // Store Name of Products
        $arrData[0][1] = "Novomatic";
        $arrData[1][1] = "Atronic";
        $arrData[2][1] = "Williams";
        $arrData[3][1] = "Roulettes";
        $arrData[4][1] = "IGT";
        $arrData[5][1] = "Interblock";

        //Store sales data
        $arrData[0][2] = 567500;
        $arrData[1][2] = 815300;
        $arrData[2][2] 开发者_如何学JAVA= 556800;
        $arrData[3][2] = 734500;
        $arrData[4][2] = 676800;
        $arrData[5][2] = 648500;

        $strXML = "<graph caption='".$graph_caption."' numberPrefix='".$graph_numberPrefix."' formatNumberScale='0' decimalPrecision='0'>";

        //Convert data to XML and append
    foreach ($arrData as $arSubData) {
            $strXML .= "<set name='" . $arSubData[1] . "' value='" . $arSubData[2] . "' color='".getFCColor()."' />";
        }
    //Close <chart> element
    $strXML .= "</graph>";

        $data['graph']  = renderChart($graph_swfFile, $graph_title, $strXML, "div" , $graph_width, $graph_height);

        //$this->load->view('chart_view',$data) ;
        $this->template->load('includes/template', 'chart_view' ,$data);
    }
}


Primarily, you would just need to build the XML from the data from your table instead of the array in your existing code.

Sample code can be derived from :

$strXML = "<chart caption='Factory Output report' subCaption='By Quantity' pieSliceDepth='30' showBorder='1' formatNumberScale='0' numberSuffix=' Units'>";
//Fetch all factory records
$strQuery = "select * from Factory_Master";
$result = mysql_query($strQuery) or die(mysql_error());
//Iterate through each factory
if ($result) {
      while($ors = mysql_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 = mysql_query($strQuery) or die(mysql_error()); 
       $ors2 = mysql_fetch_array($result2); 
      //Generate <set label='..' value='..'/>
       $strXML .= "<set label='" . $ors['FactoryName'] . "' value='" . $ors2['TotOutput'] . "' />";
      //free the resultset
       mysql_free_result($result2);
       }
  }
  mysql_close($link);
  //Finally, close <chart> element
  $strXML .= "</chart>";
  //Create the chart - Pie 3D Chart with data from $strXML
  echo renderChart("../../FusionCharts/Pie3D.swf", "", $strXML, "FactorySum", 600, 300, false, true);

Here is a discussion on the same issue:

http://codeigniter.com/forums/viewthread/136095/#671946

However, you can also use a better data builder and chart generator PHP class provided with FusionCharts pakck.

For detailed reading please refer to FusionCharts Documehtation:

http://www.fusioncharts.com/docs/ > Guide For Web Developers > FusionCharts PHP Class

or

http://www.fusioncharts.com/docs/ > Guide For Web Developers > Using PHP Class

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号