Welcome Guest, Not a member yet? Register   Sign In
Problem with $this->load->vars
#1

[eluser]RJ[/eluser]
Hello,

I'm trying to utilize the $this->load->vars method to enable use of vars in the view. Below in a Model I am first grabbing all db data for a specific dealer, then adding to session(probably remove that eventually), then trying to make those variables returned to database usable in my views.

Code:
if (isset($dealer) && $dealer !='')
{
  $this->vars = $this->grab_vars($dealer);    // use dealer var to grab info from db,
  $this->session->set_userdata('dealer',$this->vars);    // add dealer info to session
            
  // makes all dealer vars available in views where var['name'] is seen in view as $name
  $this->load->vars($this->vars);
}

And here is my grab_vars method
Code:
function grab_vars($dealer)
    {
        
        $this->db->select('*')->where('num', $dealer);
        $query = $this->db->get('fw_dealers');            
        
        // change to array
        $res = $query->result_array();
        
        // log array
        log_message('debug',  $this->db->last_query());
        
        if (!$query)
        {
            show_error('Can not find dealer in our database, please contact customer support.');
            $this->load->model('shop_model');                            // load model
            $this->shop_model->logging($res, 'DEALER-CLASS');            // write to log file
        }
        else
        {        
            return $res;
        }
    }

and this is what my array looks like
Code:
Array
(
    [0] => Array
        (
            [num] => 999
            [name] => FindWhere, Inc.
            [countries] => US,BR,DE,FR,MX,NL,UK,US
            [vatnum] => NLxxxxxx
            [bcolor] => cccccc
            [acolor] => ffffff
            [hcolor] => ace0f1
            [rr1] =>
            [rr2] =>
            [rr3] =>
            [rr_title_color] =>
            [image] =>
            [s_image] =>
            [instid] => 88858
        )

)


Loading those vars does not make them usable in a view. Any suggestions?
#2

[eluser]TheFuzzy0ne[/eluser]
You need to pass back the row_array() if you're only expecting a single row to be returned, or load your vars like this:

Code:
$this->load->vars($this->vars[0]);
#3

[eluser]RJ[/eluser]
Thanks @TheFuzzy0ne! So any time i expect only a single row return, use row_array().

Thank you
#4

[eluser]TheFuzzy0ne[/eluser]
Something like that yes. Basically, row_array will return the topmost row in your results, even if more than one row was retrieved.




Theme © iAndrew 2016 - Forum software by © MyBB