foreach循环中的PHP MVC查询

时间:2016-06-18 07:52:02

标签: php model-view-controller foreach controller

我正在使用mvc结构作为列表门户项目。 1.清单保存在表'目录'中 2.保存在表'类别'中的类别

我正在尝试获取类别名称foreach列表以将其传递给查看 我的控制器是这样的

class Listings extends Controller
{
    public function index()
    {
       $lists = $this->model->getAllLists();
        require APP . 'view/_templates/header.php';
        require APP . 'view/listings/index.php';
        require APP . 'view/_templates/footer.php';
    }
}

我的观点看起来像这样

<?php foreach ($lists as $list) { 
echo $list->id;
echo $list->name;
echo $list->category_id;
//and so on...
?>

这将列出DB中保存的所有列表。请帮助我如何获取类别名称fore 'category_id'

1 个答案:

答案 0 :(得分:0)

你应该试试这个

class Listings extends Controller
{
  public function index()
  {
     $data['lists'] = $this->model->getAllLists();
     $data['directory'] = $this->model->getAlldirectory();    
     $data['categories'] = $this->model->getAllcategories();    
     $this->load->view('header', $lists);
     $this->load->view('index', $lists);
     $this->load->view('footer', $lists);

  }
}

所以现在在视图文件.i.e index.php中你可以使用这样的方式获取所有数据:

<?php 
foreach ($lists as $list) { 
   echo $list->id;
   echo $list->name;
   echo $list->category_id;
  //and so on...
}

 foreach ($categories as $categories) { 
   echo $categories->category_id;
}

&GT;