0
votes

I have the following problem, I am working on Codeigniter and I am doing a small validation in the index of my controller this is the code:

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());

    }

The query works fine, I have already checked, it returns me what I need is the following:

  • Url
  • product/index
  • supplier/index
  • customer/index

If in the result of my query I have the same value as my condition if, in this case if it is, I would have to load the view otherwise I redirected to the home of my application, I am failing in the foreach?

This is my model:

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
What is the returned error? - ShutUpMagda
Apparently does not enter if, it always redirects me: base_url - max
try below answer if not works post your model's code. - Hikmat Sijapati
Now I upload the code in my model - max
@max try edited answer. - Hikmat Sijapati

2 Answers

0
votes

Add a flag and redirect only if flag is set (in my example it it set as 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());
}
0
votes

Like this..

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

        foreach ($data as $key=>$value) {
          if ($row[$key]->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());

        }