0

I have two methods in my model. One is selecting and returning the data to the controller (public) and the other method (private) is selecting data from the database and returning it to the public method.

This is my public method;

public function get_template()
{
    // Get the active config.
    $config_id = $this->get_config();

    // Prepare the SQL query.
    $this->db->select('template');
    $this->db->from('tms_config');
    $this->db->where('config_id',$config_id);
    $this->db->limit(1);

    // Execute the query.
    $query = $this->db->get();

    // Get the result.
    $result = $query->row_array();

    // Make sure a template is set.
    if ( ! empty($result['template']))
    {
        return $result['template'];
    }
    else
    {
        // Return the default template.
        return DEFAULT_TEMPLATE;
    }
}

Then the private method.

private function get_config()
{
    // Prepare the SQL query.
    $this->db->select('config_id');
    $this->db->from('tms_config');
    $this->db->where('active',1);
    $this->db->limit(1);

    // Execute the query.
    $query = $this->db->get();

    // Get the result.
    $result = $query->row_array();

    // Return the configuration ID.
    return $result['config_id'];
}

Apparently something is going wrong, as $config_id in the public method is empty. This error appears;

A PHP Error was encountered
Severity: Notice
Message: Undefined index: config_id
Filename: models/tms_config.php
Line Number: 140

Line 140 is the return-line of the get_config() method.

Can anybody please help me out on this one? Thanks in advance.

2
  • 1
    Is $result['config_id'] defined? Try print_r($result) after $result = $query->row_array(); Commented Feb 2, 2012 at 13:46
  • The array was empty. The problem was that I had to change 1 to '1' in my query. Commented Feb 2, 2012 at 14:00

1 Answer 1

2

try this

$result = $query->result_array();
return $result[0]['config_id'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.