0

我正在从模型中的函数中提取两组不同的数据(语法如下)。我正在尝试显示我的视图的数据。我将变量放在 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
4

1 回答 1

1

在您的控制器中,您将函数getJoinInformation的结果分配给变量allvehicledata。然后将此变量分配给视图。

函数getJoinInformation正在返回一个数组,其中包含以下内容

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

因此,在您看来,您可以访问属性getPricegetOtherPrice对象,$allvehicledata例如

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

在第 7 行中,您尝试访问cardescription_id不是对象的属性的属性$allvehicledata
我认为这是从 db 查询中获取的属性,因此您应该尝试访问它allvehicledata->getPrice->cardescription_idallvehicledata->getOtherPrice->cardescription_id.


在第 9 行中,您尝试访问存储在数组中的一些数据$model = $allvehicledata[0]->model;,但$allvehicledata不是数组。

于 2013-06-11T10:46:28.500 回答