Распечатать массив в Codeigniter - PullRequest
0 голосов
/ 11 февраля 2019

Извините, если вопросы кажутся глупыми, но я создал массив в своем контроллере, и я передаю его в представление с массивом $ 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

Если 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

Array ([0] => stdClass Object ([id] => 15 [type] => area [Дакка Юг] => Джатрабари [Дакка Север] => 0 [Савар] => 0 [Нараянгондж] => 0 [Мименсингх] => 0 [Барисал] => 0 [Фаридпур] => 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
    )

Я хочу использовать эти данные в таблице данных, в которой в качестве заголовка будет указан идентификатор, тип, Дакка Юг, Дакка Север, Савар, Нараянгондж, Мименсингх, Барисал, Фаридпур, Джессор.значения в tbody как значения. Пожалуйста, помогите, если можете.

Ответы [ 2 ]

0 голосов
/ 11 февраля 2019

Может ли это помочь вам

контроллер

$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 голосов
/ 11 февраля 2019

Делай как это

$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>

...