foreach in controller codeigniter

时间:2017-01-22 13:08:54

标签: php codeigniter codeigniter-3

我有以下问题,我正在使用Codeigniter,我在我的控制器的索引中做了一个小验证,这是代码:

function index()
    {
        $user = $this->session->userdata("id");
        $data = $this->customer_model->get_rol_user($user);

        foreach ($data as $row) {
          if ($row->Url === 'customer/index'){
               $this->load->view('guest/head');
               $this->load->view('guest/nav');
               $this->load->view('guest/section');
               $this->load->view('customer/customer_view');
            }
        }
        redirect(base_url());

    }

查询工作正常,我已经检查过,它返回我需要的内容如下:

  • 网址
  • 产品/索引
  • 供应商/索引
  • 客户/索引

如果在我的查询结果中,我有与我的条件相同的值,如果在这种情况下,如果是,我将不得不加载视图,否则我重定向到我的应用程序的主页,我在foreach中失败了?

这是我的模特:

function get_rol_user($user){

    $query = $this->db-> query("SELECT p.Url
            FROM User AS u INNER JOIN rol AS r ON u.fk_rol = r.id_rol
            INNER JOIN rol_page AS n ON n.fk_rol = r.id_rol
            INNER JOIN page AS p ON n.fk_page = p.id_page
            WHERE u.id_user ='".$user."'");
      if ($query->num_rows() > 0) {
        return $query->result();
    }
  } 

2 个答案:

答案 0 :(得分:0)

喜欢这个..

data = json.loads(request.data)
user = data['user']

答案 1 :(得分:0)

仅在设置了标志时添加标志并重定向(在我的示例中,它设置为false):

$load_view = false;
foreach ($data as $row) {
  if ($row->Url === 'customer/index'){
       // flag tells us that `if` is executed
       $load_view = true;
       $this->load->view('guest/head');
       $this->load->view('guest/nav');
       $this->load->view('guest/section');
       $this->load->view('customer/customer_view');
  }
}

// no Url is found - do a redirect.
if (!$load_view) {
    redirect(base_url());
}