msc msc
msc msc

Reputation: 75

Codeigniter Displaying Information In A View?

I'm extracting two different sets of data from a function in my model (syntax below). I'm trying to display the data my view. I put the variable in var_dump and var_dump is displaying the requested information but I'm having a hard time accessing that information. I'm getting two different sets of error messages as well. They are below. How would I display the information in my view? Thanks everyone.

Site Controller

   public function getAllInformation($year,$make,$model)
   {
     if(is_null($year)) return false;
     if(is_null($make)) return false;
     if(is_null($model)) return false;
     $this->load->model('model_data');
     $data['allvehicledata'] = $this->model_data->getJoinInformation($year,$make,$model);
     $this->load->view('view_show_all_averages',$data);
   }

Model_data

function getJoinInformation($year,$make,$model)
{
 $data['getPrice'] = $this->getPrice($year,$make,$model);
 $data['getOtherPrice'] = $this->getOtherPrice($year,$make,$model);
 return $data;

}


function getPrice($year,$make,$model)
{
 $this->db->select('*');
 $this->db->from('tbl_car_description d');
 $this->db->join('tbl_car_prices p', 'd.id = p.cardescription_id');
 $this->db->where('d.year', $year);
 $this->db->where('d.make', $make);
 $this->db->where('d.model', $model);
 $query = $this->db->get();
 return $query->result();
}

function getOtherPrice($year,$make,$model)
{
 $this->db->select('*');
 $this->db->from('tbl_car_description d');
 $this->db->where('d.year', $year);
 $this->db->where('d.make', $make);
 $this->db->where('d.model', $model);
 $query = $this->db->get();
 return $query->result();
}

View

<?php
var_dump($allvehicledata).'<br>';

//print_r($allvehicledata);
if(isset($allvehicledata) && !is_null($allvehicledata))
{
    echo "Cities of " . $allvehicledata->cardescription_id . "<br />";
    $id = $allvehicledata['getPrice']->id;
    $model = $allvehicledata[0]->model;
    $make = $allvehicledata->make;
    echo "$id".'<br>';
    echo "$make".'<br>';
    echo "$model".'<br>';
    echo $allvehicledata->year;
}

?>

Error Messages

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/view_show_all_averages.php

Line Number: 7

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 0

Filename: views/view_show_all_averages.php

Line Number: 9

Upvotes: 0

Views: 110

Answers (1)

And-y
And-y

Reputation: 1524

In your controller you are assigning the result of function getJoinInformation to the variable allvehicledata. This variable is then assigned to the view.

The function getJoinInformation is returning an array with the following

$data = array(
  'getPrice' => $this->getPrice($year,$make,$model),
  'getOtherPrice' => $this->getOtherPrice($year,$make,$model)
);

So in your view you can access the attributes getPrice and getOtherPrice in the object $allvehicledata like

$allvehicledata->getPrice;
$allvehicledata->getOtherPrice;

In line 7 you try to access the attribute cardescription_id, which is not an attribute of the object $allvehicledata.
I think this is an attribute which is get from the db query, so you should try to access it allvehicledata->getPrice->cardescription_id or allvehicledata->getOtherPrice->cardescription_id.


In line 9 you try to access some data stored in an array $model = $allvehicledata[0]->model;, but $allvehicledata is not an array.

Upvotes: 1

Related Questions