0

我正在尝试将数据传递给我的视图:模型->控制器->视图。

我的模型:

class Records_model extends CI_Model {

public function get_records($array){

    $query = $this->db->select('*')->from('records')->where('type', $array['type'])->get();
    foreach ($query->result_array() as $row){
        $data = array(
            "id" => $row['id'],
            "country" => $row['country'],
            "event" => $row['event'],
            "date" => $row['date'],
            "selection" => $row['selection'],
            "odds" => $row['odds'],
            "result" => $row['result']
        );
    }

    return $data;
}
}

我的控制器功能:

public function basketball(){
    $data = array(
        "type" => 1
    );

    $this->load->model('records_model');
    $data['records'] = $this->records_model->get_records($data);
    $this->load->view('basketball', $data);
}

和我的观点:

<?php foreach($records as $record){ ?>

                    <tr class="gradeA" id="id_65">
                        <td class="center"><?=$record['id'];?></td>
                        <td><?=$record['selection'];?></td>            
                    </tr>

                <?php } ?>

毕竟我只收到没有任何意义的数字(1,1,1,2,4,R,N)那么这段代码有什么问题呢?

4

1 回答 1

1

像这样更改模型:

模型

class Records_model extends CI_Model {

public function get_records($type){

    $this->db->select('*'); // write the column between select like select('id, selection')
    $this->db->where('type', $type);
    return $this->db->get('records')->$result_array();
}

控制器

public function basketball(){
    $type = 1;

    $this->load->model('records_model');
    $data['records'] = $this->records_model->get_records($type);
    $this->load->view('basketball', $data);
}

看法

<?php foreach($records as $record): ?>

    <tr class="gradeA" id="id_65">
        <td class="center"><?php echo $record['id']; ?></td>
        <td><?php $record['selection']; ?></td>            
    </tr>

<?php endforeach;  ?>
于 2013-07-27T18:37:00.713 回答