从数据库获取数据

时间:2011-07-16 11:24:55

标签: php mysql codeigniter fusioncharts

我已将Fusion图表实施到Codeigniter框架中,并具有以下视图。我正在创建包含视图中提供的数据的折线图,但是,我想从相同的结构化数据库表中检索此数据。无论如何要做到这一点?如果有人可以帮助我,我将非常感激。

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

类图表扩展了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] = 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);
}

}

1 个答案:

答案 0 :(得分:2)

首先,您只需要从表中的数据而不是现有代码中的数组构建XML。

示例代码可以来自:

$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);

以下是对同一问题的讨论:

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

但是,您也可以使用随FusionCharts pakck提供的更好的数据构建器和图表生成器PHP类。

有关详细阅读,请参阅FusionCharts Documehtation:

http://www.fusioncharts.com/docs/&gt; Web开发人员指南&gt; FusionCharts PHP类

http://www.fusioncharts.com/docs/&gt; Web开发人员指南&gt;使用PHP类