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

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)
{
 
 $this->db->where('email'$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(){
 
       $this->load->model('model_users');
 
       $clienteemail $this->session->userdata('email');
 
       $cliente $this->model_users->lettura_dati($clienteemail);

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

 
             $this->load->view('auth/template/auth_header'$data);
 
             $this->load->view('auth/template/auth_nav'$cliente);
 
       $this->load->view('auth/clienti/auth_sidebar');
 
             $this->load->view('auth/clienti/client_dash');
 
             $this->load->view('auth/template/auth_footer');
 
     

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:

Quote:
PHP Code:
$this->load->model('model_users');
 
       $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
Reply
#2

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

hi

You can do that with this example

PHP Code:
class Home extends CI_Controller {

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

    public function 
__construct()
    {
        
parent::__construct();
        
$this->load->model('model_users');
        
$this->clienteemail $this->session->userdata('email');
        
$this->cliente $this->model_users->lettura_dati($this->clienteemail);

    }

    public function 
index(){
        
// [...]
        
$this->load->view('auth/template/auth_nav'$this->cliente);
        
    }


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

hope it's helpfull Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB