如何在PHP / CakePHP中将关联数组转换为一组HTML表行?

时间:2015-12-03 12:25:26

标签: php arrays cakephp

table data on this type image 我的数组是$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 ) )

我想以表格格式打印

<?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>';
}
?>

但是,这会产生错误: -

  

未定义的索引:id [APP \ Plugin \ PropManagement \ View \ Otherotabookings \ index.ctp,第59行]

6 个答案:

答案 0 :(得分:3)

尝试我的代码它的工作$ 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>';
                                        }
                                        ?>

答案 1 :(得分:2)

如果您正在使用CakePHP(如您所述),您可以通过发出以下内容获得相同的结果:

<tbody>
    <?=$this->Html->tableCells(Hash::extract($otaDetails,'{n}.OtherOtaBooking'))?>
</tbody>

您可以使用$fields中的$this->OtherOtaBooking->find()选项过滤要打印的列。

答案 2 :(得分:1)

您的问题是您的数组是多维的,并且您在迭代父数组时尝试引用子数组的键。

试试这个:

<?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>';
                                    }
                                    ?>

当然,如果您有多个子数组,只需在现有循环中运行foreach循环。

答案 3 :(得分:0)

检查您的上一个<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>';
                                    }
                                    ?>

答案 4 :(得分:0)

用此行替换您的代码行

echo'<td>'. $otaDetail['OtherOtaBooking']['id']."</td>"

你会得到答案 你忘了添加另一个索引[OtherOtaBooking]

答案 5 :(得分:0)

<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>