在Codeigniter中显示数组内容

时间:2014-03-18 22:05:41

标签: php codeigniter-2

我收到了错误,

消息:数组到字符串转换

var_dump()显示,

  array (size=2)
0 => 
  array (size=2)
    'id' => string '1' (length=1)
    'name' => string 'English' (length=7)
1 => 
  array (size=2)
    'id' => string '2' (length=1)
    'name' => string 'Kannada' (length=7)

我有以下代码,

//model
    public function get_all() {
    $data = array();
    $q = $this->db->get('languages');
    if($q->num_rows() > 0) {
        foreach($q->result_array() as $row) {
            $data[] = $row;     
        }
    } 
    $q->free_result();
    return $data;   
}

//controller

   public function list_all(){
    $data['content'] = $this->m_languages->get_all();
    $data['title'] = 'Language';

    $data['main'] = 'language_list';
    $this->load->view('admin_template', $data);
}


//view

   <table>
    <?php

        var_dump($content);

        foreach($content as $id => $name) {
            echo "<tr>
                    <td>
                        $name
                    </td>
                  </tr>";
        }       
    ?>  
</table>

我想在表格中显示结果。在模式中,如果我将$ data []更改为$ data,我将只获得最后一条记录。

1 个答案:

答案 0 :(得分:0)

更简单。你可以这样做:

//model
public function get_all() {
    return $this->db->get('languages')->result_array();  
}

//controller
public function list_all(){
    $data['content'] = $this->m_languages->get_all();
    $data['title'] = 'Language';

    $data['main'] = 'language_list';
    $this->load->view('admin_template', $data);
}


//view

   <table>
    <?php foreach($content as $c): ?>
          <tr><td><?php echo $c['name']; ?></td></tr> 
         <!-- name is example if column name. You can change it like your table -->
    <?php endforeach; ?>       
</table>