0

テーブルからレコード全体を取得し、それらをhtmlテーブルにロードするという指示に従っていました。これがモデルです

private $namatabel;

public function __construct() {
    parent::__construct();
    $namatabel='ms_kategori_material';
}

function read()
{        
   $sql = $this->db->get($this->namatabel);  

   if($sql->num_rows() > 0)
    {     
        foreach($sql->result() as $row)
        {  
            $data[] = $row;  
        }     
        return $data;  
    } 
    else 
    {  
        return null;  
    }  

}

次に、read()コントローラーで関数を使用します

public function __construct() {
    parent::__construct();
    $this->load->model('m_kategorimaterial');
}

function index()
{
    $data['c_row'] = $this->m_kategorimaterial->read();
//pass the c_row into the views  
    $this->load->view('v/vkategorimaterial', $data);  
}

それらをビューに表示するには

<?php  
$no = 1;   
foreach ($c_row as $row) { ?>  
    <tr id="row">  
        <td id="no"><?php echo $no;?></td>  
        <td id="judul"><?php echo $row->Kode_Kategori_Material_Jasa;?></td>  
        <td id="kategori"><?php echo $row->Nama_Material_Jasa;?></td>                 
    </tr>  
<?php  
    $no++;  
}  
?>  

c_rowしかし、その後、未定義の変数と無効な引数が提供されたというエラーが発生しましたforeach()c_row私は変数をc_kategorimaterial/indexand copy pasteforeachステートメントで送信したと思いました。何が悪かったのか ?

4

2 に答える 2

0

1) を使用して、データが正しく取得されているかどうかを確認しますprint_r($data)。それがうまくいかなかった場合は、クエリに何かが欠けている可能性があります。

2)データベースデータを完全に取得した場合は、モデルで返される配列の名前を変更するだけです。

function read()
{        
   $sql = $this->db->get('ms_kategori_material');  

   if($sql->num_rows() > 0)
    {     
        foreach($sql->result() as $row)
        {  
            $c_row[] = $row;  
        }     
        return $c_row;  //comment this return part while debugging
        // print_r($c_row);
        //exit;
    } 
    else 
    {  
        return null;  
    }  

}

于 2012-10-11T13:03:59.303 に答える
0

データ配列を印刷して、その配列で何が得られているかを正確に把握することをお勧めします。次の形式を試してください。役立つ場合があります

function getPosts() {
$query = $this->db->get('posts');
$posts = array();

foreach ($query->result() as $row) {
    $posts[] = array(
        'id' => $row->id,
        'title' => $row->title,
        'content' => $row->content
    );
}

return $posts;
}
于 2012-10-11T13:17:48.417 に答える