Codeigniter在视图中显示信息?

时间:2013-06-08 21:34:34

标签: mysql function codeigniter-2

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

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();
}

查看

<?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

1 个答案:

答案 0 :(得分:1)

在控制器中,您将函数 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的属性。
我认为这是从db查询中获取的属性,因此您应该尝试访问它allvehicledata->getPrice->cardescription_idallvehicledata->getOtherPrice->cardescription_id


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