2013-03-29 30 views
1

我有一个艰难的时间搞清楚如何做到这一点。我经历了几次在网络上的搜索,并尝试了一些例子,但无法做到这一点。我正在使用codeigniter框架,并且我想将我的视图文件中的foreach循环的数据库值传递给“类别”和“数据”部分中的javascript函数作为图形的参数,而不是静态值。我如何能够将数据库值$row->Time_Period;$row->Data_Value;从我的视图文件的主体传递给javascript函数部分“categories”和“data”?还是有其他方法来处理这个?有人可以帮助我吗?非常感谢。以下是我的代码。如何通过JavaScript函数内的数据库值传递php变量?

JavaScript函数

<script type="text/javascript"> 
$(function() { 
    $('#container').highcharts({ 

     chart: { 
      type: 'column' 
     }, 

     title: { 
      text: 'RDI SYSTEM' 
     }, 

     xAxis: { 
      categories: ['2012','2013'] 
     }, 

     yAxis: { 
      allowDecimals: false, 
      min: 0, 
      title: { 
       text: 'Values' 
      } 
     }, 

     tooltip: { 
      formatter: function() { 
       return '<b>'+ this.x +'</b><br/>'+ 
        this.series.name +': '+ this.y +'<br/>'+ 
        'Total: '+ this.point.stackTotal; 
      } 
     }, 

     plotOptions: { 
      column: { 
       stacking: 'normal' 
      } 
     }, 

     series: [ { 
      name: 'jun', 
      data: [3000,2000], 
      stack: 'male' 
     }] 
     }); 
    }); 


</script> 

查看文件:

<?php 
    $submit=array('class'=>'btn','name'=>'submit','id'=>'submit','value'=>'Submit'); 
    $indicator=array('name'=>'indicator','id'=>'indicator','value'=>'','placeholder'=>'Indicator Name'); 
    $description=array('name'=>'description','id'=>'description','value'=>'','placeholder'=>'Description'); 
    ?> 
    <?php 
    $subsector_dropdown=array('0'=>'Select Sub-Sector'); 
    $classes_dropdown=array('0'=>'Select Class'); 
    $indicator_dropdown=array('0'=>'Select Indicator'); 
    ?> 
<div class="hero-unit"> 
    <div class="row-fluid"> 

    <div class="span2"> <label><strong><h3>No.Of Rows:</h3></strong></label></div> 
    <div id="numrows" class="span1"> 
    <h3 class="numrows"><strong><?php echo $total_rows; ?></strong></h3> 
    </div> 
    <div class="span7"> <h1><?php echo "List of Infrastructure Data"; ?></h1></div> 

    </div> 
<div class="row-fluid"> 
    <div class="span3"><i class="icon-plus"></i>&nbsp;<a href='<?php echo site_url('rdi_controller/insert_infra')?>'><strong>Add Infrastructure Data</strong></a></div> 
<?php 
    $indicator_dropdown=array('0'=>'Select Indicators'); 
    $year_dropdown=array('0'=>'Select Years','1'=>'Select All'); 
    ?> 

    <div class="span4"> 
     <h2> <?php echo form_open('rdi_controller/infra');?> 

     <div class="input-append"> 
     <?php echo form_dropdown("area_name",$area_name_dropdown,'','id="area_id" style="width:50%;height:130%"'); ?>&nbsp;&nbsp; 
     <?php echo form_dropdown("indicator",$indicator_dropdown,'','id="indicator_id" style="width:50%;height:130%"'); ?>&nbsp;&nbsp; 
     <?php echo form_dropdown("year",$year_dropdown,'','id="year_id" style="width:30%;height:130%"'); ?>&nbsp;&nbsp; 
     <input type="submit" class="btn btn-primary btn-medium" id="submit" name="search" value="Search"><button class="btn btn-primary btn-xlarge" type="button" id="displayAll" onclick="reload()">Display All</button> 
     </div> 



     <?php echo form_close();?> </h2> 
<table class="table table-stripe"> 
     <tr> 
     <td><a href='<?php echo site_url('rdi_controller/Infra_export_to_pdf')?>'> <img src="<?php echo base_url('img/pdf.png'); ?> " width="35" height="35" title="Export to PDF File"/></a><a href='<?php echo site_url('rdi_controller/Infra_export_to_Excel')?>'> <img src="<?php echo base_url('img/spreadsheet.png'); ?>" width="35" height="35" title="Export to Excel File" class="excel"/> </a></td> 

     </tr> 

     </table> 
    </br> 

    <table id="tablesorter-demo" class="tablesorter"> 
    <thead> 
    <tr> 
     <th><strong>Select</strong></th> 
     <th><strong>Sector</strong></th> 
     <th><strong>Subsector</strong></th> 
     <th><strong>Class</strong></th> 
     <th><strong>Indicator</strong></th> 
     <th><strong>Data Value</strong></th> 
     <th><strong>Unit</strong></th> 
     <th><strong>Time Period</strong></th> 
     <th><strong>Area Name</strong></th> 
     </tr> 
    </thead> 
<?php foreach($infra as $row){?> 

     <tr> 
     <td><input type='checkbox' id='chk' name='infra_id[]' value="<?php echo $row->fusion_id;?>" ></td> 
     <td> <?php echo anchor("rdi_controller/infra_update_form/$row->infra_id/",$row->Sector); ?></td> 
     <td><?php echo $row->Subsector ;?></td> 
     <td><?php echo $row->Classes ;?></td> 
     <td><?php echo $row->Indicator ;?></td> 
     <td><?php echo $row->Data_Value ;?></td> 
     <td><?php echo $row->Unit ;?></td> 
     <td><?php echo $row->Time_Period ;?></td> 
     <td><?php echo $row->Area_Name ;?></td> 

     </tr> 
    <?php }?> 
    </table> 
    <div class='row'> 
     <div class='span2 '><strong>Total Rows</strong> </div> 
     <div class='span1 ' colspan=6><strong><?php echo $total_rows; ?></strong></div> 
    </div> 

输出:

enter image description here

回答

1

这就是我所做的关于如何访问PHP变量,包括含javascript函数数据库值数组变量。我也用implode函数来分隔数组值和逗号字符。这里是我的代码

<?php 



$arrVal=implode(",",$infra_data_values); 
$arrYears=implode(",",$infra_time_period); 
$arrIndicator=implode(" and ",$infra_indicator); 


?> 
    <?php echo '<script type="text/javascript"> 


    $(function() { 

    $(\'#container\').highcharts({ 

     chart: { 
      type: \'column\' 
     }, 

     title: { 
      text:[\''.$arrIndicator.'\'] 
     }, 

     xAxis: { 
      categories: ['. $arrYears . '] 
     }, 

     yAxis: { 
      allowDecimals: false, 
      min: 0, 
      title: { 
       text: \'Values\' 
      } 
     }, 

     tooltip: { 
      formatter: function() { 
       return \'<b>\'+ this.x +\'</b><br/>\'+ 
        this.series.name +\': \'+ this.y +\'<br/>\'+ 
        \'Total: \'+ this.point.stackTotal; 
      } 
     }, 

     plotOptions: { 
      column: { 
       stacking: \'normal\' 
      } 
     }, 

     series: [ { 
      name: \''.$arrIndicator.'\', 
      data: ['. $arrVal . '], 
      stack: \'male\' 
     }] 
    }); 

}); 

</script>'; ?> 

把包裹回声语句中的javascript功能,然后在js函数内的每个语句中的引号前拨回斜线。

0

为什么不使用下面的方法呢?

<script type="text/javascript"> 
var foo = "<?php echo $db['some_var']; ?>"; 
</script> 
+0

如何在上面显示的javascript函数中的“xAxis”和“data”标签中应用这个? – Eli

-1

我所知道的最简单方法是逗号分隔值存储在一个PHP变量,然后回显在你的JavaScript这些变量。这意味着您将不得不遍历您正在使用的任何数据库查询结果来设置这些值。一旦这些PHP变量被设置了,只是这样做:

xAxis: { 
    categories: [<?php echo $categoriesData; ?>] 
}, 

series: [ { 
    name: 'jun', 
    data: [<?php echo $dataData; ?>], 
    stack: 'male' 
}] 
+0

如果我有2或3个以上的值,该怎么办?也许这是很好的存储阵列。 – Eli

+0

php变量将存储这些Javascript变量的所有适用值。如果您需要为其他javascript变量输出更多值,则只需设置其他php变量即可。您可以将所有相关数据存储为一个数组,如果您选择,则可以使用以下代码:$ javascriptData ['categories'] = array('something','something else',a third value'); 然后迭代该数组以将它们回显到javascript中。您可以采取任何方式,两种方式都有效。 –

+0

这不适用于.js文件 –