Go through foreach in controller codeigniter

1

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

function index()
    {
    $user = $this->session->userdata("id");
        $consulta = $this->cliente_model->get_roles_usuario($user);

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

    }

The query works well, I've already checked it, it gives me back what I want it to be:

  • Url
  • product / index
  • provider / index
  • client / index

If within the result of my query I have the same value as the one I placed in the if , in this case it is, I would have to load the view. Otherwise redirect me at the beginning of my application, am I failing in foreach ?

    
asked by Fernando Banegas 22.01.2017 в 01:06
source

2 answers

1

In addition to the comments and optimization, within the if you should put a continue; so that when you print the views you want to finish spinning in the foreach (imagine if instead of 3 results were thousands you would do unnecessary checks). Greetings.

foreach ($consulta as $row) {
           //row es un array;
   if ($row['Url'] === 'cliente/index'){
        $this->load->view('guest/head');
        $this->load->view('guest/nav');
        $this->load->view('guest/section');
        $this->load->view('cliente/cliente_view');
        continue;
  }
}
    
answered by 03.11.2017 в 20:23
0

Without seeing the implementation of the model and assuming that you have already loaded the model in the controller, what happens very possibly is that the model is returning an array, therefore the code would be as follows:

function index()
    {
    $user = $this->session->userdata("id");
        $consulta = $this->cliente_model->get_roles_usuario($user);

        foreach ($consulta as $row) {
           //row es un array;
          if ($row['Url'] === 'cliente/index'){
        $this->load->view('guest/head');
        $this->load->view('guest/nav');
        $this->load->view('guest/section');
        $this->load->view('cliente/cliente_view');
            }
        }
        redirect(base_url());

    }
    
answered by 02.02.2017 в 19:51