mysql - Codeigniter 在 View 中显示信息?

标签 mysql function codeigniter-2

我正在从模型中的函数中提取两组不同的数据(语法如下)。我正在尝试显示我的 View 中的数据。我将变量放入 var_dump 中,var_dump 正在显示请求的信息,但我很难访问该信息。我也收到两组不同的错误消息。他们在下面。我将如何在我的 View 中显示信息?谢谢大家。

站点 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);
   }

模型数据

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

最佳答案

在 Controller 中,您将函数 getJoinInformation 的结果分配给变量 allvehicledata。然后将该变量分配给 View 。

函数getJoinInformation返回一个具有以下内容的数组

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

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

$allvehicledata->getPrice;
$allvehicledata->getOtherPrice;
<小时/>

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

<小时/>

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

关于mysql - Codeigniter 在 View 中显示信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17004074/

相关文章:

php - Codeigniter Mysql 没有完成循环

php - 分页不正确显示页码 Codeigniter

function - 计算使用相同参数调用多个函数的结果的最大乘积

mysql - 在 where 子句中向 unix 时间戳添加天数?

尝试使用 UTF-8 字符串时 PHP htmlspecialchars() 函数出错

MySql Query-日期范围内的日期范围

python - Python 中的局部函数

python - 在函数内运行异常 - Python

php - 是否可以将所有类型的错误重定向到 codeigniter 中的公共(public)页面?

mysql - 在加入 MySQL 时从主表中选择特定数据