0

I am currently despairing on a for me very strange behaviour when echoing php values to html table cells.

I simply want to loop through an array and print out the values into a table but only some information is shown under special conditions.

Outside of the table the values are shown.

<?php

foreach ($mpbs_overview_tab4_bookings as $tab4_arr) {
 echo "<tr>";
  $tab4startDate = new DateTime($tab4_arr->starttime);
  $tab4endDate = new DateTime($tab4_arr->endtime);
  echo '<td>' . $tab4startDate->format('d.m.Y H:i') .'</td>';
  echo '<td>' . $tab4endDate->format('d.m.Y') .'</td>';
  echo '<td>' . $tab4_arr->username .'</td>';
 echo "</tr>";
}
?>

Using this code, $tab4startDate->format('d.m.Y H:i') is shown, $tab4endDate->format('d.m.Y') not. Adding H:i to $tab4endDates format results to the fact, that it is shown again.

$tab4_arr->username is also not shown. If I change content to

echo "<td>";
 echo $tab4startDate->format('d.m.Y H:i');
 echo $tab4_arr->username;
echo "</td>";

for testing purposes, also the username is visible...

Has anybody an idea, what I am doing wrong?

This is my array:

{"id":"185","starttime":"2017-11-26 00:00:00","endtime":"2017-11-29 00:00:00","username":"lsgit"},
{"id":"186","starttime":"2017-11-26 00:00:00","endtime":"2017-11-29 00:00:00","username":"lsgit"},
{"id":"187","starttime":"2017-11-19 00:00:00","endtime":"2017-11-22 00:00:00","username":"lsgit"},
{"id":"188","starttime":"2017-11-30 00:00:00","endtime":"2017-12-03 00:00:00","username":"lsgit"},
{"id":"189","starttime":"2017-12-05 00:00:00","endtime":"2017-12-08 00:00:00","username":"lsgit"},
{"id":"190","starttime":"2017-12-05 00:00:00","endtime":"2017-12-08 00:00:00","username":"lsgit"},
{"id":"191","starttime":"2017-12-05 00:00:00","endtime":"2017-12-08 00:00:00","username":"lsgit"}
  • add your array to your question – Shibi Nov 17 '17 at 12:49
0

You have an error here echo '<td>' . $tab4_arr->username; .'</td>';

Remove the ;

  • Yeah, you are right. I removed the semicolon. I accidentally added it, as I changed my code. But that was not the reason... Still not working. – Gardinero Nov 17 '17 at 13:09
  • 1
    Its working fine for me without the semicolon eval.in/902320 – Shibi Nov 17 '17 at 13:13
0

Elements of $mpbs_overview_tab4_bookings array are json encoded. Your code should be:

<?php
echo '<table>';
foreach ($mpbs_overview_tab4_bookings as $tab4_arr) {
  $tab4_arr = json_decode($tab4_arr);
  echo '<tr>';
  $tab4startDate = new DateTime($tab4_arr->starttime);
  $tab4endDate = new DateTime($tab4_arr->endtime);
  echo '<td>' . $tab4startDate->format('d.m.Y H:i') .'</td>';
  echo '<td>' . $tab4endDate->format('d.m.Y') .'</td>';
  echo '<td>' . $tab4_arr->username .'</td>';
  echo '</tr>';
}
echo '</table>';
?>
-1

I'm really embarrassed about that now, but the reason was some javascript, I had working on the page...

Sorry. I did not recognize that!

Thanks a lot for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.