gpt4 book ai didi

php - 如何在 javascript 函数内传递带有数据库值的 php 变量?

转载 作者:行者123 更新时间:2023-11-28 09:15:37 26 4
gpt4 key购买 nike

我很难弄清楚如何做到这一点。我在网上进行了多次搜索并尝试了一些示例,但无法得到正确的结果。我正在使用 codeigniter 框架,我想将数据库值从 View 文件中的 foreach 循环传递到“类别”和“数据”部分中的 javascript 函数,作为图形的参数而不是静态值。我如何能够传递数据库值$row->Time_Period;$row->Data_Value;从我的 View 文件正文到javascript函数部分“类别”和“数据”?或者还有其他方法来处理这个问题吗?有人可以帮我解决这个问题吗?多谢。下面是我的代码。

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

最佳答案

这就是我关于如何访问 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 函数包裹在 echo 语句内,然后在 js 函数内每个语句的引号前添加反斜杠。

关于php - 如何在 javascript 函数内传递带有数据库值的 php 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15703440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com