[英]How to transform an associative array into a set of HTML table rows in PHP / CakePHP?
table data on this type image My array is $otaDetails
which prints 这种类型的图像上的表数据我的数组是$otaDetails
,它打印
Array ( [0] => Array ( [OtherOtaBooking] => Array ( [id] => 1 [listing_id] => 0 [event_id] => 123testing [first_name] => [last_name] => [email_id] => [phone_number] => 0 [title] => [checkin] => 0000-00-00 [checkout] => 0000-00-00 [no_of_guests] => 0 [amount_paid] => 0 [amount_rec] => 0 [reservation_id] => 0 [booking_src] => ) ) [1] => Array ( [OtherOtaBooking] => Array ( [id] => 2 [listing_id] => 0 [event_id] => 123testing [first_name] => testamar 12 [last_name] => [email_id] => aaaaa@gmail.com [phone_number] => 2147483647 [title] => CasaMelhor: 2BHK in Candolim:CM001 [checkin] => 2015-12-16 [checkout] => 2015-12-17 [no_of_guests] => 21 [amount_paid] => 2147483647 [amount_rec] => 111111 [reservation_id] => 0 [booking_src] => asdddddddd ) )
I want to print it in table format 我想以表格格式打印
<?php
foreach ($otaDetails as $otaDetail){
echo'<tbody>';
echo'<tr>';
echo'<td>'. $otaDetail['id']."</td>";
echo'<td>'. $otaDetail['listing_id'].'</td>';
echo'<td>'. $otaDetail['first_name'].'</td>';
echo'<td>'. $otaDetail['last_name'].'</td>';
echo'<td>'. $otaDetail['email_id'].'</td>';
echo'<td>'. $otaDetail['phone_number'].'</td>';
echo'<td>'. $otaDetail['title'].'</td>';
echo'<td>'. $otaDetail['checkin'].'</td>';
echo'<td>'. $otaDetail['checkout'].'</td>';
echo'<td>'. $otaDetail['no_of_guests'].'</td>';
echo'<td>'. $otaDetail['amount_paid'].'</td>';
echo'<td>'. $otaDetail['amount_rec'].'</td>';
echo'<td>'. $otaDetail['reservation_id'].'</td>';
echo'<td>'. $otaDetail['booking_src'].'</td>';
echo'<tr>';
echo'</tbody>';
}
?>
However, this gives the error:- 但是,这给出了错误:
Undefined index: id [APP\\Plugin\\PropManagement\\View\\Otherotabookings\\index.ctp, line 59] 未定义的索引:id [APP \\ Plugin \\ PropManagement \\ View \\ Otherotabookings \\ index.ctp,第59行]
try my code it's work $otaDetail is a multidimentional array so you need to use like $otaDetail['OtherOtaBooking'][index]
试试我的代码,它的工作原理$ otaDetail是一个多维数组,因此您需要像$otaDetail['OtherOtaBooking'][index]
<?php foreach ($otaDetails as $otaDetail){
echo'<tr>';
echo'<td>'. $otaDetail['OtherOtaBooking']['id']."</td>";
echo'<td>'. $otaDetail['OtherOtaBooking']['listing_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['first_name'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['last_name'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['email_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['phone_number'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['title'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['checkin'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['checkout'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['no_of_guests'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['amount_paid'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['amount_rec'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['reservation_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['booking_src'].'</td>';
echo'</tr>';
}
?>
If you are using CakePHP (as you mention), you can obtain the same result by issuing: 如果您使用的是CakePHP(如前所述),则可以通过发出以下命令获得相同的结果:
<tbody>
<?=$this->Html->tableCells(Hash::extract($otaDetails,'{n}.OtherOtaBooking'))?>
</tbody>
You can filter the columns you wish to print by using the $fields
option in $this->OtherOtaBooking->find()
. 您可以使用$this->OtherOtaBooking->find()
的$fields
选项过滤希望打印的列。
Your problem is that your array is multidimensional, and you're trying to reference keys of the child array while iterating the parent array. 您的问题是您的数组是多维的,并且您尝试在迭代父数组时引用子数组的键。
Try this: 尝试这个:
<?php foreach ($otaDetails as $otaDetail){
echo'<tr>';
echo'<td>'. $otaDetail['OtherOtaBooking']['id']."</td>";
echo'<td>'. $otaDetail['OtherOtaBooking']['listing_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['first_name'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['last_name'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['email_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['phone_number'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['title'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['checkin'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['checkout'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['no_of_guests'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['amount_paid'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['amount_rec'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['reservation_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['booking_src'].'</td>';
echo'<tr>';
}
?>
Of course, if you have multiple child arrays, simply run a foreach
loop within the existing loop. 当然,如果您有多个子数组,只需在现有循环中运行一个foreach
循环。
check your last <tr>
tag. 检查最后一个<tr>
标记。 close it </tr>
关闭它</tr>
<?php foreach ($otaDetails as $otaDetail){
echo'<tbody>';
echo'<tr>';
echo'<td>'. $otaDetail['id']."</td>";
echo'<td>'. $otaDetail['listing_id'].'</td>';
echo'<td>'. $otaDetail['first_name'].'</td>';
echo'<td>'. $otaDetail['last_name'].'</td>';
echo'<td>'. $otaDetail['email_id'].'</td>';
echo'<td>'. $otaDetail['phone_number'].'</td>';
echo'<td>'. $otaDetail['title'].'</td>';
echo'<td>'. $otaDetail['checkin'].'</td>';
echo'<td>'. $otaDetail['checkout'].'</td>';
echo'<td>'. $otaDetail['no_of_guests'].'</td>';
echo'<td>'. $otaDetail['amount_paid'].'</td>';
echo'<td>'. $otaDetail['amount_rec'].'</td>';
echo'<td>'. $otaDetail['reservation_id'].'</td>';
echo'<td>'. $otaDetail['booking_src'].'</td>';
echo'</tr>';
echo'</tbody>';
}
?>
Replace your code line with this line 用此行替换您的代码行
echo'<td>'. $otaDetail['OtherOtaBooking']['id']."</td>"
you will get your answer you forgot add one more index that is [OtherOtaBooking] 您会得到答案的,您忘记再添加一个[OtherOtaBooking]索引
<tbody>
<?php foreach ($otaDetails as $otaDetail){
echo'<tr>';
echo'<td>'. $otaDetail['OtherOtaBooking']['id']."</td>";
echo'<td>'. $otaDetail['OtherOtaBooking']['listing_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['first_name'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['last_name'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['email_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['phone_number'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['title'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['checkin'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['checkout'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['no_of_guests'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['amount_paid'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['amount_rec'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['reservation_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['booking_src'].'</td>';
echo'</tr>';
}
?>
</tbody>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.