在笨打印阵列

问题描述 投票:0回答:2

很抱歉,如果问题接缝笨但我在控制器中创建一个数组并予它传递给带有$数据[“样本”]数组的图。

这里是我的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

若f 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 [类型] =>面积[达卡南] => Jatrabari [达卡北] => 0 [萨瓦] => 0 [Narayangonjh] => 0 [迈门辛] => 0 [巴里萨尔] => 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
    )

我想在一个数据表,其中编号,类型,达卡南,北达卡,萨瓦,Narayangonjh,迈门辛,巴里萨尔里得普,杰索尔将在THEAD作为标题和值TBODY作为价值利用这些数据。如果可以的话请帮忙。

codeigniter datatables codeigniter-3
2个回答
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>

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>

© www.soinside.com 2019 - 2024. All rights reserved.