很抱歉,如果问题愚蠢,但我在控制器中创建了一个数组,并使用$ data ['sample']数组将其传递给视图。
这是我的MYSQL查询:
SELECT workplace.id, workplace.type,
MAX(IF(workplace.region_id = 1, workplace.name,0)) 'Dhaka South',
MAX(IF(workplace.region_id = 2, workplace.name,0)) 'Dhaka North',
MAX(IF(workplace.region_id = 3, workplace.name,0)) 'Savar',
MAX(IF(workplace.region_id = 4, workplace.name,0)) 'Narayangonjh',
MAX(IF(workplace.region_id = 5, workplace.name,0)) 'Mymensingh',
MAX(IF(workplace.region_id = 6, workplace.name,0)) 'Barisal',
MAX(IF(workplace.region_id = 7, workplace.name,0)) 'Faridpur',
MAX(IF(workplace.region_id = 8, workplace.name,0)) 'Jessore'
FROM workplace
WHERE workplace.type='area'
GROUP BY workplace.id
如果是print_r示例,则为
SELECT workplace.id, workplace.type, MAX(IF(workplace.region_id = 1, workplace.name,0)) 'Dhaka South', MAX(IF(workplace.region_id = 2, workplace.name,0)) 'Dhaka North', MAX(IF(workplace.region_id = 3, workplace.name,0)) 'Savar', MAX(IF(workplace.region_id = 4, workplace.name,0)) 'Narayangonjh', WHERE workplace.type='area' GROUP BY workplace.id
数组 ( [0] => stdClass对象 ( [id] => 15 [类型] =>区域 [达卡南部] =>贾特拉巴里 [北达卡] => 0 [Savar] => 0 [Narayangonjh] => 0 [Mymensingh] => 0 [Barisal] => 0 [Faridpur] => 0 [杰索尔] => 0 )
[1] => stdClass Object
(
[id] => 16
[type] => area
[Dhaka South] => Lalbag
[Dhaka North] => 0
[Savar] => 0
[Narayangonjh] => 0
[Mymensingh] => 0
[Barisal] => 0
[Faridpur] => 0
[Jessore] => 0
)
[2] => stdClass Object
(
[id] => 17
[type] => area
[Dhaka South] => Dhanmondi
[Dhaka North] => 0
[Savar] => 0
[Narayangonjh] => 0
[Mymensingh] => 0
[Barisal] => 0
[Faridpur] => 0
[Jessore] => 0
)
[3] => stdClass Object
(
[id] => 18
[type] => area
[Dhaka South] => 0
[Dhaka North] => Gulshan
[Savar] => 0
[Narayangonjh] => 0
[Mymensingh] => 0
[Barisal] => 0
[Faridpur] => 0
[Jessore] => 0
)
我想在数据表中使用这些数据,其中id,type,Dhaka South,Dhaka North,Savar,Narayangonjh,Mymensingh,Barisal,Faridpur,Jessore将作为标题,而tbody中的值作为值。 如果可以的话,请帮忙。
答案 0 :(得分:0)
这样做
$data['sample'] = $your_array;
//Getting column names here
foreach ($data['sample'] as $key){
$data['column_names'] = array_keys((array) $key);
break;
}
$this->load->view('file', $data);
查看
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr><!-- Showing column names here -->
<?php foreach($column_names as $column){
echo $column;
?>
</tr>
</thead>
<tbody>
<?php foreach($sample as $row){?>
<tr>
<td><?= $row->id ?> </td>
<td><?= $row->type ?> </td>
<td><?= $row->Dhaka Soutd ?> </td>
<td><?= $row->Dhaka Nortd ?> </td>
<td><?= $row->Savar ?> </td>
<td><?= $row->Narayangonjh ?> </td>
<td><?= $row->Mymensingh ?> </td>
<td><?= $row->Barisal ?> </td>
<td><?= $row->Faridpur ?> </td>
<td><?= $row->Jessore ?> </td>
</tr>
<?php } ?>
</tbody>
答案 1 :(得分:0)
这可以帮助您
控制器
$data['persons'] = '' // obj your query result
视图
<?php
$arrKey = array();
foreach ($persons[0] as $key => $person):
$arrKey[] = $key;
endforeach ?>
<table>
<thead>
<tr>
<?php foreach ($arrKey as $akey): ?>
<td><?php echo $akey; ?></td>
<?php endforeach ?>
</tr>
</thead>
<tbody>
<?php foreach ($persons as $key => $person): ?>
<tr>
<?php foreach ($arrKey as $akey): ?>
<td><?php echo $person->$akey; ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>