2

table data on this type image My array is $otaDetails which prints

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] => [email protected] [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]

8
  • 2
    Except the fact that the <tbody> should be outside the foreach, do you have any error ? Commented Dec 3, 2015 at 12:27
  • but giving below error Undefined index: id [APP\Plugin\PropManagement\View\Otherotabookings\index.ctp, line 59] Commented Dec 3, 2015 at 12:27
  • You are having a three-dimensional Array, but you're treating it like a two-dimensional array. Commented Dec 3, 2015 at 12:30
  • To get the first ID, you have to do 0 => OtherOtaBooking => ID. What you're doing is 0 => ID. Commented Dec 3, 2015 at 12:31
  • What's with huge indentation? Commented Dec 3, 2015 at 12:35

6 Answers 6

3

try my code it's work $otaDetail is a multidimentional array so you need to use like $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>';
                                        }
                                        ?>
Sign up to request clarification or add additional context in comments.

Comments

2

If you are using CakePHP (as you mention), you can obtain the same result by issuing:

<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().

1 Comment

This is the most elegant way!
1

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.

2 Comments

its giving me Undefined offset: 0
Doh. My bad. I've edited the answer to correct for that.
0

check your last <tr> tag. close it </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>';
                                    }
                                    ?>

2 Comments

That's not what the problem is.
its giving me Undefined offset: 0
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>

Comments

0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.