Go through an array from a controller in Codeigniter

0

good I'm starting with php and codeigniter, I have the following problem I'm going through an array from my model, which is the result of a query, how can I read my records in the controller one by one?

function getClientes(){
      $sql = "SELECT * FROM clientes";
      $query = $this->db->query($sql);
      if($query->num_rows()>0){
      	$i=0;
		foreach ($query->result() as $row)
        {
            $img[$i]['id'] = $row->id;
            $img[$i]['nombre'] = $row->nombre;
            $img[$i]['ubicacion'] = $row->ubicacion;
            $img[$i]['telefono'] = $row->telefono;
            $i++;
        }
        return $img;
	  }

	}   
    
asked by FeRcHo 10.06.2017 в 06:34
source

1 answer

2

As I mentioned, I still do not have very clear what you want to do but I hope I can help you, and if you add a little more information I can help you in your problem.

Model:

function getClientes(){
      $sql = "SELECT * FROM clientes";
      $query = $this->db->query($sql);
      if($query->num_rows()>0){
        return $query;
      }

    } 

Controller:

function procesaCliente(){
    $getClientes = $this->myModel->getClientes();
    foreach ($getClientes->result() as $row){
        $id = $row->id;
        $nombre = $row->nombre;
        $ubicacion = $row->ubicacion;
        $telefono = $row->telefono;
    }
}

What I do is in the function of the model return the entire object $query for in the controller assign the method of the model to a variable in the controller to be able to process the array that returns the function.

In your case you could grab the function to a variable in the following way:

$getClientes = $this->myModel->getClientes();

When trying to get the index like this:

$getClientes[0]['id']

Edition 1:

What you want is to go through the entire array in the view you do the following in your controller:

function procesaCliente(){
    $getClientes = $this->myModel->getClientes();
    //Paso el array completo a la vista.
    $this->load->view('vista', $getClientes);
}

And in the view you go through the array in the following way:

foreach ($getClientes->result() as $row){
    $id = $row->id;
    $nombre = $row->nombre;
    $ubicacion = $row->ubicacion;
    $telefono = $row->telefono;
}
    
answered by 10.06.2017 в 07:01