从数据库获取数据

发布于 2024-11-25 00:11:50 字数 2002 浏览 0 评论 0原文

我已使用以下视图将 Fusion 图表实现到 Codeigniter 框架中。我正在使用视图中提供的数据创建折线图,但是我想从相同的结构化数据库表中检索此数据。

有办法做吗?如果有人可以帮助我,我将非常感激。

<?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] = 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);
    }
}

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] = 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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱给你人给你 2024-12-02 00:11:50

首先,您只需要根据表中的数据而不是现有代码中的数组构建 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 提供的更好的数据生成器和图表生成器 PHP 类帕克克。

详细阅读请参阅 FusionCharts 文档:

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文