【问题标题】:looping in php codeigniter views在 php codeigniter 视图中循环
【发布时间】:2025-12-21 12:10:07
【问题描述】:

我得到一个这样的数组:-

$query['data'] = $this->flights->checkflight($form_data);  //getting data
$this->load->view("payment",$query);   //sending to view

我在视图中循环浏览这样的数据。我得到错误,比如非法字符串偏移 'from'、'to' 等。

<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'] ?> &nbsp; <?=  $flight['seatsB'] ?> &nbsp; <?=  $flight['seatsE'] ?>  </td>
     <td> <?=  $flight['priceF'] ?> &nbsp; <?=  $flight['priceB'] ?> &nbsp; <?=  $flight['priceE'] ?>  </td>
  </tr>      
  <?php
  endforeach;
  ?>
</tbody>

但在我看来,如果我这样做的话:-

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 )

请告诉我如何正确循环 $data,我已经看到了“非法偏移”的答案并应用了这些答案,但没有成功。

【问题讨论】:

  • 好吧,当你执行 print_r($flight); 时你会得到什么?在你的视野内你的循环?
  • 如果我做 print_r($flight); 我得到了我想要的数组但我想要单独的值作为键值对。

标签: php codeigniter view foreach


【解决方案1】:

只有一行数据从数据库中获取。 就像 [id, from, to, date, time ..] 等 现在,如果您在此数组上使用“foreach”,那么您将获得每个单独的列。像“id”、“from”、“to”...等 在每次迭代的 foreach 循环中,您的 $flight 变量将保存任何单个列的值,例如“id”、“from”、“to”等。因此 $flight[key] 不存在。

【讨论】: