我正在从模型的函数中提取两组不同的数据(下面的语法)。我正在尝试显示我的数据。我将变量放在var_dump中,并且var_dump正在显示所请求的信息,但是我很难访问该信息。我也收到两组不同的错误消息。他们在下面。如何在我的视图中显示信息?感谢大家。
网站管理员
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);
}
型号数据
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();
}
视图
<?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;
}
?>
错误讯息
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
最佳答案
在控制器中,您正在将函数getJoinInformation的结果分配给变量allvehicledata。然后将此变量分配给视图。
函数getJoinInformation返回带有以下内容的数组
$data = array(
'getPrice' => $this->getPrice($year,$make,$model),
'getOtherPrice' => $this->getOtherPrice($year,$make,$model)
);
因此,在您的视图中,您可以像访问对象
getPrice
中的属性getOtherPrice
和$allvehicledata
$allvehicledata->getPrice;
$allvehicledata->getOtherPrice;
在第7行中,您尝试访问属性
cardescription_id
,它不是对象$allvehicledata
的属性。我认为这是从数据库查询中获取的属性,因此您应该尝试访问它
allvehicledata->getPrice->cardescription_id
或allvehicledata->getOtherPrice->cardescription_id
。在第9行中,您尝试访问存储在数组
$model = $allvehicledata[0]->model;
中的某些数据,但是$allvehicledata
不是数组。