[英]How can I pass results from one query into a second query and display the results in one table?
I am using Codeigniter and am trying to pass the results of one query into a second. 我正在使用Codeigniter,并试图将一个查询的结果传递给第二个。 I receive the error
我收到错误
"Fatal error: Cannot use object of type CI_DB_mysql_result as array".
“致命错误:不能将CI_DB_mysql_result类型的对象用作数组”。
Any suggestions would be appreciated. 任何建议,将不胜感激。 For my $truck_num query I am simply grabbing a list of trucks in a field called truck_num.
对于$ truck_num查询,我只是在一个名为truck_num的字段中获取卡车列表。 I am then trying to pass those results into a query that does some calculations.
然后,我试图将这些结果传递给进行一些计算的查询。
Controller /////////// 控制器///////////
function average_battery_life_by_truck() {
$truck_num = $this->mhedash_model->select_truck();
foreach($truck_num->result_array() as $row) {
$array = $row['truck_num'];
$results[] = $this->mhedash_model->select_battery_life_truck($array);
}
$data['array'] = $results;
$data['main_content'] = 'mhe/average_truck_bat';
return $this->load->view('includes/template',$data);
}
View ////// 查看//////
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<p><h1></h1></p>
<FORM
<></div>
<br />
<p><h4 id="date"></h4></p>
<h3></h3>
<table id="table" class="sortable">
<THEAD>
<TR>
<TH>Truck</TH>
<TH>Average Hours</TH>
</TR>
</THEAD>
<TBODY>
<?php $count = 0; ?>
<?php foreach($array as $row): ?>
<TR >
<TD><?php echo $row['average_bat_life']; ?></TD>
<TD><?php echo $row['truck_num']; ?></TD>
</TR>
<?php $count++; ?>
<?php endforeach;?>
</TBODY>
</table>
MODEL///// 模型/////
function select_truck() {
mysql_select_db('battery');
$this->table_name = 'truck';
$truck_num = "SELECT
truck_num
FROM
truck
WHERE
battery_type_id = '5'
or battery_type_id = '8'
or battery_type_id = '10'
or battery_type_id = '9'
ORDER BY truck_num
";
return $this->db->query($truck_num);
}
function select_battery_life_truck($data) {
mysql_select_db('battery');
$this->table_name = 'truck_hours';
$truck_num = $this->db->escape($data);
/*This query takes the current row's metered hours and subtracts it with the previous row's metered hours.
We then average out the time the battery was on the truck for the date range */
$query = "SELECT
ROUND(AVG(metered_hours-b),2) average_bat_life,
truck_num
FROM (SELECT
id,
metered_hours,
loc_id,
(SELECT MAX(metered_hours)
FROM
(SELECT @rownum:=@rownum+1 id, truck_hours.metered_hours,truck_hours.loc_id from truck_hours,truck, (SELECT @rownum:=0) r where truck_hours.loc_id = truck.loc_id and truck.truck_num = $truck_num and DATE_SUB(CURDATE(),INTERVAL 30 DAY) >= truck_hours.ctrl_date ) a
WHERE a.metered_hours < t.metered_hours)b
FROM
(SELECT @rownum:=@rownum+1 id, truck_hours.metered_hours,truck_hours.loc_id from truck_hours,truck, (SELECT @rownum:=0) r where truck_hours.loc_id = truck.loc_id and truck.truck_num = $truck_num and DATE_SUB(CURDATE(),INTERVAL 30 DAY) >= truck_hours.ctrl_date ) AS t) c,
truck
WHERE c.loc_id = truck.loc_id
ORDER BY average_bat_life";
return $this->db->query($query);
}
In your second query you are not fetching data from database. 在第二个查询中,您不是从数据库中获取数据。
So you need to add fetch method 所以你需要添加提取方法
In your controller 在您的控制器中
foreach($truck_num->result_array() as $row) {
$array = $row['truck_num'];
$result = $this->mhedash_model->select_battery_life_truck($array);
foreach($result->result_array() as $row_data) {
$results[] = $row_data;
}
}
$data['array'] = $results;
$data['main_content'] = 'mhe/average_truck_bat';
$this->load->view('includes/template',$data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.