在PHP Codeigniter视图中循环 [英] looping in php codeigniter views
问题描述
我得到这样的数组:-
$query['data'] = $this->flights->checkflight($form_data); //getting data
$this->load->view("payment",$query); //sending to view
我正在视图中浏览这样的数据。而且我收到诸如从,到之类的非法字符串偏移之类的错误。
I am looping through data like this in the view. And i get errors like illegal string offset 'from','to' etc.
<tbody>
<?php
foreach($data as $flight):
?>
<tr class="danger">
<td> <?= $flight['from'] ?> </td>
<td> <?= $flight['to'] ?> </td>
<td><?= $flight['date'] ?> </td>
<td> <?= $flight['time'] ?> </td>
<td> <?= $flight['seatsF'] ?> <?= $flight['seatsB'] ?> <?= $flight['seatsE'] ?> </td>
<td> <?= $flight['priceF'] ?> <?= $flight['priceB'] ?> <?= $flight['priceE'] ?> </td>
</tr>
<?php
endforeach;
?>
</tbody>
但是在我看来,如果我这样做:-
But in my view if I do:-
print_r($data);
我得到这样的东西:-
Array ( [id] => 12348 [from] => asd [to] => as [date] => 2017-01-01 [time] => 01:00:00 [seatsF] => 0 [seatsB] => 1 [seatsE] => 1 [priceF] => 1000 [priceB] => 1000 [priceE] => 1000 [approved] => 0 [payment] => 1000 )
请告诉我如何正确遍历$数据,我看到了非法偏移的答案,并应用了这些答案,但您的答案却没有解决。
Please tell me how to correctly loop through $data, I have seen answers of "illegal offset" and applied those but thy didn't work out.
推荐答案
只有从数据库中获取一行数据。
就像[id,from,to,date,time ..]等
现在,如果在此数组上使用 foreach,则将获得每个单独的列。例如 id, from, to ...等
在每次迭代的foreach循环中,$ flight变量将保存任何单个列的值,例如 id, from, 到等。因此$ flight [key]不存在。
There is only one row of data getting fetched from the database. it's like [id, from, to, date, time ..] etc now if you use "foreach" on this array then you will get each individual columns. like "id", "from" , "to" ... etc Inside your foreach loop on every iteration, your $flight variable will hold the value of any one single column like "id", "from", "to" etc. So $flight[key] does not exist.
这篇关于在PHP Codeigniter视图中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!