Welcome Guest, Not a member yet? Register   Sign In
db->get Error
#1

[eluser]Unknown[/eluser]
I have 2 functions that return an array.

Code:
/* Returns an array containing provinces */
function get_provinces($db){
    $query = $db->get('provinces');
    $provinces[0] = 'Not in US or Canada';    

    foreach($query->result_array() as $row) {
        $province_id = $row['province_id'];
        $province = $row['province'];    
        $provinces[$province_id]= $province;
    }
    return $provinces;
}

/* Returns an array containing countries */
function get_countries($db){
    $query = $db->get('countries');
    $countries[0] = 'choose one';
    foreach($query->result_array() as $row) {
        echo "$country_id <br>";
        $country_id = $row['country_id'];
        $country = $row['country'];    
        $countries[$country_id] = $country;
    }
    return $countries;
}

Whenever I call these functions back to back (as seen below), the script dies. If I comment out either of the below lines, the script runs normal, minus of course the variable that I comment out.
Code:
$data['provinces'] = get_provinces($this->db);            
    $data['countries'] = get_countries($this->db);

What am I missing here???
#2

[eluser]Unknown[/eluser]
I have fixed my own issue. Instead of passing $db to the function I used
Code:
$obj = get_instance();

The resulting code for the functions is:
Code:
/* Returns an array containing provinces */
function get_provinces(){
    $obj = get_instance();
    $query=$obj->db->get('provinces');
    $provinces[0] = 'Not in US or Canada';    
    foreach($query->result_array() as $row) {
        $province_id = $row['province_id'];
        $province = $row['province'];    
        $provinces[$province_id]= $province;
    }
    $query->free_result();
    return $provinces;
}

/* Returns an array containing countries */
function get_countries(){
    $obj = get_instance();
    $query=$obj->db->get('countries');
    $countries[0] = 'choose one';
    foreach($query->result_array() as $row) {
        $country_id = $row['country_id'];
        $country = $row['country'];    
        $countries[$country_id] = $country;
    }
    $query->free_result();
    return $countries;
}
#3

[eluser]warrennz[/eluser]
Instead of creating 2 instances, if you create the instance as an object property and then you can reuse $obj throughout the library.

Code:
class someclassname {
    function someclassname()
    {
        //Create obj as an object property.
        $this->obj =& get_instance();    
    }
    
    /* Returns an array containing provinces */
    function get_provinces($db){
        
        /**
         *  OLD
         *  $obj = get_instance();
         *  $query=$obj->db->get('countries');
         **/
        
        /** NEW **/
        $query = $this->obj->db->get('provinces');
        $provinces[0] = 'Not in US or Canada';  
        foreach($query->result_array() as $row) {
            $province_id = $row['province_id'];
            $province = $row['province'];    
            $provinces[$province_id]= $province;
        }
        return $provinces;
    }
    
    /* Returns an array containing countries */
    function get_countries($db){
        
        /** This way you're not creating a new instance the 2nd, 3rd, or 99th time you use it in this library/class **/
        $query = $this->obj->db->get('countries');
        $countries[0] = 'choose one';
        foreach($query->result_array() as $row) {
            echo "$country_id <br>";
            $country_id = $row['country_id'];
            $country = $row['country'];    
            $countries[$country_id] = $country;
        }
        return $countries;
    }
}

All that said, I'm certainly no expert. Just a thought ^_^




Theme © iAndrew 2016 - Forum software by © MyBB