John Davidson

arrays - How do I add <tr> and complete </tr> tag in PHP foreach loop

0 comments
Message:


I have this kind of array in $form_all_data variable.


Array
(
[0] => stdClass Object
(
[id] => 111
[form_id] => 1
[entry_id] => 38
[meta_key] => 6
[meta_value] => Student 1
[item_index] =>
)

[1] => stdClass Object
(
[id] => 112
[form_id] => 1
[entry_id] => 38
[meta_key] => 3
[meta_value] => Not sure
[item_index] =>
)

[2] => stdClass Object
(
[id] => 113
[form_id] => 1
[entry_id] => 38
[meta_key] => 5
[meta_value] => 55
[item_index] =>
)

[3] => stdClass Object
(
[id] => 129
[form_id] => 1
[entry_id] => 43
[meta_key] => 6
[meta_value] => Student 3
[item_index] =>
)

[4] => stdClass Object
(
[id] => 130
[form_id] => 1
[entry_id] => 43
[meta_key] => 3
[meta_value] => Yes
[item_index] =>
)

[5] => stdClass Object
(
[id] => 131
[form_id] => 1
[entry_id] => 43
[meta_key] => 5
[meta_value] => 55
[item_index] =>
)

)

I am showing all this data inside <table>. These entries are differentiated as per entry_id.
I want to add <tr> tag when every time new entry_id is there in the loop and want to complete </tr> tag when every time same entry_id is last in the loop.


Something like this. Please check this image


This means I want to show entry_id 38 data in one <tr> and entry_id 43 data in another <tr>


How can I do this ?


In my code , it is adding <tr> with every new iteration


<?php 
$i = 1;
foreach ( $form_all_data as $check_form_all_data_key => $check_form_all_data_value ) { ?>
<tr>
<?php
if ( $check_form_all_data_value->form_id == 1 ) {

if ( $check_form_all_data_value->meta_key == 6 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }

if ( $check_form_all_data_value->meta_key == 3 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }

} else {

} ?>

</tr>

<?php $i++; } ?>


My Answer:
To achieve the desired output, you can keep track of the current entry_id and compare it with the next entry_id in the loop. If they are different, add the opening tag, and if they are the same, add the closing tag.

Here's an updated version of your code:

php
$current_entry_id = null;
foreach ($form_all_data as $check_form_all_data_key => $check_form_all_data_value) {
if ($current_entry_id != $check_form_all_data_value->entry_id) {
if ($current_entry_id !== null) {
echo '';
}
echo '';
$current_entry_id = $check_form_all_data_value->entry_id;
}
?>

if ($check_form_all_data_value->form_id == 1) {
if ($check_form_all_data_value->meta_key == 6) {
echo $check_form_all_data_value->meta_value;
}
if ($check_form_all_data_value->meta_key == 3) {
echo $check_form_all_data_value->meta_value;
}
}
?>

}
if ($current_entry_id !== null) {
echo '';
}
?>


This code will add the opening tag when a new entry_id is encountered and add the closing tag when the entry_id changes in the loop.

Rate this post

3 of 5 based on 3317 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us