Welcome Guest, Not a member yet? Register   Sign In
access variables from other function within same class?
#1

[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
#2

[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);    
    }
}
#3

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

[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.
#5

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

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




Theme © iAndrew 2016 - Forum software by © MyBB