我正在使用php和mysql - [codeigniter framework] - 这是我的表结构:
Please visit this image to find out table structure
每件商品都分为三个或四个部分,这些部件使这个商品成为价格。
这些项目属于使用refid
指向的发票。 refid
实际上是invoice_id
。
现在我需要获取特定的发票项目并将其显示在表格中。
这就是我需要的:
Please visit this image to find out my need
从数据库获取值没有任何问题,我将使用codeigniter active records
。
您可能知道,codeigniter数据库result_array
函数将为我们提供一个关联数组。现在我想将每个项目与其他项目分开并在html表格中显示它们,我必须对每个项目依赖子项目使用rowspan
。
请您帮我解决一下如何使用结果数组在html表中显示结果?
答案 0 :(得分:1)
所以我们假设你得到一个像这样的结果数组,
$result = array(
array(
'id' => 26,
'price' => 100,
'parent' => 0
),
array(
'id' => 27,
'price' => 100,
'parent' => 26
),
array(
'id' => 28,
'price' => 150,
'parent' => 26
),
array(
'id' => 29,
'price' => 100,
'parent' => 26
),
array(
'id' => 30,
'price' => 100,
'parent' => 0
),
array(
'id' => 31,
'price' => 99,
'parent' => 30
),
array(
'id' => 32,
'price' => 75,
'parent' => 30
),
array(
'id' => 33,
'price' => 600,
'parent' => 0
),
array(
'id' => 34,
'price' => 100,
'parent' => 33
),
array(
'id' => 35,
'price' => 50,
'parent' => 33
),
array(
'id' => 36,
'price' => 100,
'parent' => 33
),
);
我无法从图像中手动插入数组中的所有数据,所以我只插入了一次所需的数据,
所以现在你可以做这样的事情来对同一项目的数据进行分组,
$new_array = array();
$invoice_price = 0;
foreach ($result as $key => $value) {
$invoice_price += $value['price'];
if($value['parent'] == 0){
$new_array[$value['id']]['other_data'][] = $value;
$new_array[$value['id']]['final_price'] = $value['price'];
}else{
$new_array[$value['parent']]['other_data'][] = $value;
$new_array[$value['parent']]['final_price'] += $value['price'];
}
}
现在如果你打印这个$ new_array,你将获得以下输出,
Array
(
[26] => Array
(
[other_data] => Array
(
[0] => Array
(
[id] => 26
[price] => 100
[parent] => 0
)
[1] => Array
(
[id] => 27
[price] => 100
[parent] => 26
)
[2] => Array
(
[id] => 28
[price] => 150
[parent] => 26
)
[3] => Array
(
[id] => 29
[price] => 100
[parent] => 26
)
)
[final_price] => 450
)
[30] => Array
(
[other_data] => Array
(
[0] => Array
(
[id] => 30
[price] => 100
[parent] => 0
)
[1] => Array
(
[id] => 31
[price] => 99
[parent] => 30
)
[2] => Array
(
[id] => 32
[price] => 75
[parent] => 30
)
)
[final_price] => 274
)
[33] => Array
(
[other_data] => Array
(
[0] => Array
(
[id] => 33
[price] => 600
[parent] => 0
)
[1] => Array
(
[id] => 34
[price] => 100
[parent] => 33
)
[2] => Array
(
[id] => 35
[price] => 50
[parent] => 33
)
[3] => Array
(
[id] => 36
[price] => 100
[parent] => 33
)
)
[final_price] => 850
)
)
希望您可以看到数据已经按项目分组。 现在只需在视图文件中发送数据,
$this->load->view('yourviewfilename',array('data'=>$new_array,'invoice_price'=>$invoice_price));
并按照屏幕截图打印表格,使用以下代码
<table border="1">
<tr>
<th>ID</th>
<th>parent</th>
<th>price</th>
<th>final price</th>
</tr>
<?php $count = 0; ?>
<?php foreach ($data as $key => $value) { ?>
<?php $count++; ?>
<?php foreach ($value['other_data'] as $k => $v) { ?>
<tr>
<?php if ($k == 0) { ?>
<td rowspan="<?php echo count($value['other_data']); ?>"><?php echo $count ?></td>
<?php } ?>
<td> <?php echo $v['parent']; ?></td>
<td> <?php echo $v['price']; ?></td>
<?php if ($k == 0) { ?>
<td rowspan="<?php echo count($value['other_data']); ?>"><?php echo $value['final_price']; ?></td>
<?php } ?>
</tr>
<?php } ?>
<?php } ?>
</table>
你会得到这样的输出,
希望你明白这一点,并打印出你需要的其他数据。