CodeIgniter Forums
access variables from other function within same class? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: access variables from other function within same class? (/showthread.php?tid=23035)



access variables from other function within same class? - El Forum - 09-28-2009

[eluser]Kristof[/eluser]
Code:
class M_airports extends model {    
    function addAirport() {
         $data = array(
            'airportname' => $this->input->post('airportname'),
            'airportwebsite' => $this->input->post('airportwebsite'),
            'airportphone' => $this->input->post('airportphone'),            
        );
        $this->db->insert('aiports', $data);
        $ID = $this->db->insert_id();    
    }
    function addAirportpics($filename,$filelocation) {      
        $data = array(
            'AirportpicsAirportid' => $ID,
            'AirportpicsFolder' => $filelocation,
            'AirportpicsFilename' => $filename
        );
        $this->db->insert('airportpics', $data);    
    }
}

I need the $ID in the addAirportpics function but i don't know how to get to it. Could someone please help me out with this? Thanks in advance


access variables from other function within same class? - El Forum - 09-28-2009

[eluser]pistolPete[/eluser]
You need to use a class variable:

Code:
class M_airports extends model {    
    var $id;
    
     function addAirport() {
         $data = array(
            'airportname' => $this->input->post('airportname'),
            'airportwebsite' => $this->input->post('airportwebsite'),
            'airportphone' => $this->input->post('airportphone'),            
        );
        $this->db->insert('aiports', $data);
        $this->id = $this->db->insert_id();    
    }
    function addAirportpics($filename,$filelocation) {      
        $data = array(
            'AirportpicsAirportid' => $this->id,
            'AirportpicsFolder' => $filelocation,
            'AirportpicsFilename' => $filename
        );
        $this->db->insert('airportpics', $data);    
    }
}



access variables from other function within same class? - El Forum - 09-28-2009

[eluser]Kristof[/eluser]
ow that's easy thank you for your help !!!!!


access variables from other function within same class? - El Forum - 09-28-2009

[eluser]kurucu[/eluser]
That's a bit messy though, as depending on when addAirportpics is called, all sorts of funny behaviour could result.

Perhaps return $ID from addAirport, and then pass it back into addAirportpics as a function variable. This makes the function more portable, and also you can do some error checking in the controlling function before calling it.


access variables from other function within same class? - El Forum - 06-04-2011

[eluser]mehwish[/eluser]
I have the same issue and I tried the way you mentioned but
Code:
echo $this->id
is not showing anything


access variables from other function within same class? - El Forum - 06-04-2011

[eluser]mehwish[/eluser]
please help me out in solving this