Welcome Guest, Not a member yet? Register   Sign In
share array between function

Hi, if this question is too general I'm sorry =(

I have a conceptual problem.

I need to retrieve data from a db so I build a model function:
PHP Code:
public function lettura_dati($clienteemail)
 $query $this->db->get('user');
 if ($query && $query->num_rows() > 0) {
   $row $query->row();
   $cliente['nome'] = $row->nome;
   $cliente['cognome'] = $row->cognome;
   $cliente['id'] = $row->id;
   return $cliente;
 } else {
   echo "errore nella ricerca del nome";

This take a row based on the session email and store in an array all the data I need.
then on my controller I have:
PHP Code:
     public function index(){
       $clienteemail $this->session->userdata('email');
       $cliente $this->model_users->lettura_dati($clienteemail);

             $data['title']='La Giumenta Bardata Dashboard'//array per titolo e dati passati


the problem is that I need to build a series of functions that load different pages everypage has to show different data from the db.
I don't think that write for every function this code:

PHP Code:
       $clienteemail $this->session->userdata('email');
       $cliente $this->model_users->lettura_dati($clienteemail); 
Is really right...
So how can I make this three line of code, working for every functions of the same controller and share che $cliente array?

thank u

(This post was last modified: 02-04-2016, 08:37 AM by keulu.)


You can do that with this example

PHP Code:
class Home extends CI_Controller {

$cliente = array();
$clienteemail '';

    public function 
$this->clienteemail $this->session->userdata('email');
$this->cliente $this->model_users->lettura_dati($this->clienteemail);


    public function 
// [...]

__construct is called just before your index method and it's available for each methods in this class

hope it's helpfull Smile

Theme © iAndrew 2016 - Forum software by © MyBB