0

我有这样的代码

$this->load->model("m_crud");
$test= $this->m_crud->get_querry; //this model already have query and result() to return
foreach($test as $retest)
   {
    echo $retest->primary_id;
    $test2=$this->db->query("select * from table2 where id='$retest");
    foreach($test2 as $retest2)
       {
         echo $retest2->name;
        }
   }

将所有这些代码放入“视图”很容易,但我想使用 MCV 模型。我尝试使用这样的数组存储结果:

$data['test']=$test
foreach($test as $retest)
   {
    echo $retest->primary_id;
    $test2=$this->db->query("select * from table2 where id='$retest");
    $data['test2']=$test2
    foreach($test2 as $retest2)
       {
         echo $retest2->name;
        }
   }
$this->load->view("test_view",$data);

我在视图中得到的是我从 的回声中具有相同的值,每个人的回声$test2应该是不同的$test

谢谢,对不起我的英语不好

4

2 回答 2

0

您可以在视图页面中使用

foreach($test2 as $newarray)
{
     foreach($newarray->result() as $row)
     {
           echo $row->columnnames;//
     }
}
于 2013-02-26T06:50:15.980 回答
0

这很容易,这是一个例子

控制器方法

function getResult()
{
    $this->load->model('mymodel');
    $results    =   $this->mymodel->getRecords();
    $data['results']    =   $results;
    $this->load->view('myview',$data);
}

模型方法

function getRecords()
{
    return $this->db->query("select * from table2 where id='$retest")->result_array();
}

并查看

foreach($results as $row){
    echo $row['id'];
    echo '<br>';
    echo $row['othercolumn'];
}
于 2013-02-26T06:48:07.243 回答