【发布时间】:2017-03-03 13:06:00
【问题描述】:
我在数据库中有表。
tbl_parameter专栏:
parameter_ID | parameter_name | parameter_unit | parameter_method
------------ | -------------- | -------------- | ----------------
3001 | Salt | % | Titrimetri
3002 | pH | - | pH meter
3003 | Visco | poise | Viscometer
接下来是tbl_coa,用于存储 COA 页面输入。 parameter_ID 来自多选输入,所以以逗号分隔存储。
coa_ID | coa_number | coa_year | product_ID | customer_ID | parameter_ID
------ | ----------- | -------- | ---------- | ----------- | ------------
4001 | 001/01/2017 | 2017 | 10001 | 2111 | 3001,3002,3003
接下来,我正在处理显示 COA 详细信息的视图页面,该视图保存为 view_coa.php。代码如下。我正在定义 {PRE} 来替换 tbl_。
型号Coa_model.php:
function get_coa_detail($coa_ID){
$this->db->select('*');
$this->db->from('{PRE}coa');
$this->db->join('{PRE}product', '{PRE}product.product_ID = {PRE}coa.product_ID', 'LEFT');
$this->db->join('{PRE}customer', '{PRE}customer.customer_ID = {PRE}coa.customer_ID', 'LEFT');
$this->db->join('{PRE}parameter', '{PRE}parameter.parameter_ID = {PRE}coa.parameter_ID', 'LEFT');
$this->db->where('{PRE}coa.coa_ID', $coa_ID);
$query = $this->db->get();
if($query->num_rows() > 0){
$data = $query->row();
$query->free_result();
}
else{
$data = NULL;
}
return $data;
}
控制器Coa.php:
public function view_coa($coa_ID=0){
if($coa_ID != 0 && !empty($coa_ID)){
$data = array(
'record' => $this->Coa_model->get_coa_detail($coa_ID)
);
//print_r($data);
$this->site->view('view_coa', $data);
}
else{
redirect(set_url('coa/data_table/'.date('Y')));
}
}
问题是,当我想显示参数详细信息(以逗号分隔的parameter_ID 存储)时,它只显示了来自第一个parameter_ID 的数据。在上面的示例中,它只显示了parameter_ID = 3001 的详细信息。我想显示来自所有parameter_ID(3001、3002 和 3003)的数据(parameter_name、parameter_unit、parameter_method)。
print_r 数据结果:
Array ( [record] => stdClass Object ( [coa_ID] => 4001 [coa_number] => 001/01/2017 [coa_year] => 2017 [product_ID] => 10001 [product_type] => Sweet Soy [product_name] => Product A [Salt] => 5.5 [pH] => 4.4 [Visco] => 12.5 [customer_ID] => 2111 [customer_name] => Company 1 [parameter_ID] => 3001 [parameter_name] => Salt [parameter_unit] => % [parameter_method] => Titrimetri ) )
我的问题是,如何显示[parameter_ID] => 3002 和[parameter_ID] => 3003 的所有参数详细信息?
当前视图view_coa 显示来自数据库的数据:
<!-- COA title and number row -->
<div class="row">
<div class="col-xs-12 coa-title-number">
<h3>Certificate of Analysis</h3>
<p>NO. <?php echo $record->no_coa;?></p>
</div>
</div>
<!-- COA detail row -->
<div class="row">
<div class="col-xs-1">
</div>
<div class="col-xs-11">
<table class="table-coa-detail">
<tr class="text-nowrap">
<th>Product Name</th>
<td><strong>: <?php echo $record->product_name;?></strong></td>
</tr>
<tr class="text-nowrap">
<th>Customer Name</th>
<td><strong>: <?php
if($record->customer_ID == "0" || $record->customer_ID == NULL){
echo "-";
}
else{
echo $record->customer_name;
}
?></strong></td>
</tr>
<tr class="text-nowrap">
<th>Analysis Results</th>
<td>:</td>
</tr>
</table>
</div>
</div>
<!-- COA results row -->
<div style="padding-right: 50px;" class="row">
<div class="col-xs-1">
</div>
<div class="col-xs-11">
<table class="table-results">
<thead>
<tr>
<th>No</th>
<th>Parameter</th>
<th>Unit</th>
<th>Results</th>
<th>Method</th>
<th>Conclusion</th>
</tr>
</thead>
<tbody class="text-nowrap">
<tr>
<td style="text-align: center;">1</td>
<td><strong>Analytical</strong>
<?php
if($record->product_type == "Sweet Soy"){
echo "<p>pH</p>"; // it should "<p>".$record->parameter_name."</p>"; from [parameter_ID] => 3002
echo "<p>Visco</p>"; // it should "<p>".$record->parameter_name."</p>"; from [parameter_ID] => 3003
echo "<p>Salt</p>"; // it should "<p>".$record->parameter_name."</p>"; from [parameter_ID] => 3001
}
else{
echo "<p>pH</p>"; // it should "<p>".$record->parameter_name."</p>"; from [parameter_ID] => 3002
echo "<p>Salt</p>"; // it should "<p>".$record->parameter_name."</p>"; from [parameter_ID] => 3001
}
?>
</td>
<td style="text-align: center;">
<?php
if($record->product_type == "Sweet Soy"){
echo "<p>-</p>"; // it should "<p>".$record->parameter_unit."</p>"; from [parameter_ID] => 3002
echo "<p>poise</p>"; // it should "<p>".$record->parameter_unit."</p>"; from [parameter_ID] => 3003
echo "<p>%</p>"; // it should "<p>".$record->parameter_unit."</p>"; from [parameter_ID] => 3001
}
else{
echo "<p>-</p>"; // it should "<p>".$record->parameter_unit."</p>"; from [parameter_ID] => 3002
echo "<p>%</p>"; // it should "<p>".$record->parameter_unit."</p>"; from [parameter_ID] => 3001
}
?>
</td>
<td style="text-align: center;">
<?php
if($record->product_type == "Sweet Soy"){
echo "<p>".$record->pH."</p>";
echo "<p>".$record->Visco."</p>";
echo "<p>".$record->Salt."</p>";
}
else{
echo "<p>".$record->pH."</p>";
echo "<p>".$record->Salt."</p>";
}
?>
</td>
<td style="text-align: center;">
<?php
if($record->product_type == "Sweet Soy"){
echo "<p>pH meter</p>"; // it should "<p>".$record->parameter_method."</p>"; from [parameter_ID] => 3002
echo "<p>Viscometer</p>"; // it should "<p>".$record->parameter_method."</p>"; from [parameter_ID] => 3003
echo "<p>Titrimetri</p>"; // it should "<p>".$record->parameter_method."</p>"; from [parameter_ID] => 3001
}
else{
echo "<p>pH meter</p>"; // it should "<p>".$record->parameter_method."</p>"; from [parameter_ID] => 3002
echo "<p>Titrimetri</p>"; // it should "<p>".$record->parameter_method."</p>"; from [parameter_ID] => 3001
}
?>
</td>
<td style="text-align: center;">
<p>OK</p>
<p>OK</p>
<p>OK</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
【问题讨论】:
-
检查这个答案请stackoverflow.com/a/2322511/6068342我认为它会帮助你
-
@ArmKh 我实际上知道
implode和explode函数,我使用implode将多选值存储到数据库(如上所述),我使用explode在编辑页面中。它对查看页面没有帮助.. -
@Firman 请按照Minimal, Complete, and Verifiable example 将其缩小到特定问题的最小表示。然后更新问题陈述,使其与实际代码相关。很难理解你的问题是什么
-
@AbdullaNilam 我已经简化了代码和问题。这可以解释我的问题吗?
标签: php mysql database codeigniter