Codeigniter - 将查询结果传递给私有函数并返回

时间:2015-08-17 22:23:48

标签: php mysql codeigniter

我正在运行一个返回116结果的查询。然后我尝试通过另一个查询循环这些结果并在表中显示结果。我似乎无法将它们全部放在同一张桌子上。

function average_battery_life_by_truck() {

    $truck_num = $this->mhedash_model->select_truck();
    $result = $truck_num->result_array();


    foreach($result as $row){
    $data = $this->_average_truck($row['truck_num']);
        }
}

function _average_truck($array) {
    $data[] = $array;

    $query = $this->mhedash_model->select_battery_life_truck($array);
    $result1 =& $query->result_array();

    $this->table->set_heading('Average','Truck');
    $data['table'] = $this->table->generate_table($result1);

    $data['array2'] = $result1;

    $data['main_content'] = 'mhe/average_truck_bat';
    return $this->load->view('includes/template',$data);



}

////// VIEW

<?php foreach($array2 as $row): ?>

    <TR >
        <TD><?php echo $row['average_bat_life']; ?></TD>
        <TD><?php echo $row['truck_num']; ?></TD>

    </TR>
    <?php $count++; ?>

1 个答案:

答案 0 :(得分:1)

此代码只会为您提供$data中循环中处理的最后结果,因为您每次都在编写它

foreach($result as $row){
    $data = $this->_average_truck($row['truck_num']);
}

执行此操作,您将获得一系列结果

foreach($result as $row){
    $data[] = $this->_average_truck($row['truck_num']);
}