Welcome Guest, Not a member yet? Register   Sign In
extract() in the view?
#1

[eluser]deslacker[/eluser]
I'm having a problem understanding the disply of variables that to my understanding are extracted by CI when I include $data in the call to my view:

Code:
<?php
class Ctl_vehicle extends Controller {

function Ctl_vehicle()
    {
        parent::Controller();
    //    $this->load->library('table');
    //    $this->load->helper('array');

    }

    function index()
    {
        $this->load->model('mdl_vehicle');
        $data['vehicles'] = $this->mdl_vehicle->get_all_records();
        $this->load->view('view_vehicle',$data);
        $this->output->enable_profiler(TRUE);
    }
}
?>

I understand the array name is vehicles but isn't this array extracted into variables based on the keys in the array. In this case they are year, make, model, etc.

Why can't I echo these as $year, $make, $model, etc. from my view?

It doesn't make a difference whether I $query->result or query->result_array in my model.

Thanks everyone!
#2

[eluser]gtech[/eluser]
I am not sure how your data is returned from the model so assuming you are just doing somthing like

model:
Code:
..
$returnval = $this->db->getwhere($table,$data_array);
return $returnval
..

controller:
Code:
function index()
    {
        $this->load->model('mdl_vehicle');
        $retval = $this->mdl_vehicle->get_all_records();
        $data['vehicles'] = $retval->result_array();
        $this->load->view('view_vehicle',$data);
        $this->output->enable_profiler(TRUE);
    }
view:
Code:
<?php print_r($vehicles)?>

this should show you how the array is built up in the view, then you can work out how to itterate through it.



you will probably find the result_array can return more than 1 record so it will be somthing like:

echo $vehicles[0]['year'];
#3

[eluser]deslacker[/eluser]
Thanks! Here's my model code. I can foreach through the result whether I return an object or an array but why can't I address the extracted variables directly based on the key values?

Code:
<?php
class Mdl_vehicle extends Model {

    function Mdl_vehicle()
    {
     parent::Model();
    }
    
    function get_all_records()
    {
        $query = $this->db->get('vehicles');
        return $query->result_array();
    }
}
?>
#4

[eluser]gtech[/eluser]
in your example your passing an array called data to your view, with the key vehicles. you can pass other keys as well for example buildings;

Code:
$data['vehicles'] = $this->mdl_vehicle->get_all_records();
$data['buildings'] = $this->mdl_buildings->get_all_buildings();
$this->load->view->('viewname',$data);

in the view, the keys of the $data array become the extracted variables.

so

$vehicles will be the the result_array from get_all_vehicles();
$buildings will be the result_array from get_all_buildings();

so to access your keys you will have to do somthing like this in your veiw;

Code:
<?php foreach($vehicles as $row):?>
     <?=$row['Make'];?>
<?php endforeach;?>
<?php foreach($buildings as $row):?>
     <?=$row['Name'];?>
<?php endforeach;?>

this is because result_array() can return more than one row eg.
Code:
<?php
echo $vehicles[0]['Make'];
echo $vehicles[0]['Year'];
echo $vehicles[1]['Make'];
?>

if you only want to return one row from the database use row_array() instead, then you access Make as follows in your view

Code:
echo $vehicles['Make'];




Theme © iAndrew 2016 - Forum software by © MyBB