1

How can I display data in a tablerow inside a foreach function? The agenda variable works fine, the rows variable is the one making problems, I need it inside a tag

<?php
$agenda = $days = json_decode(get_field( "field_uc_content_json" ));
unset($agenda[0]); ?>



<table class="table">
    <thead>
        <tr>
          <th> Day </th>
          <th>Description</th>
          <th>Hours</th>
        </tr>
    </thead>
<tbody>
   <?php foreach($agenda as $column) { ?>
   <tr>
    <td><?php echo $column[0]; ?></td>
    <td><?php
    $rows = explode( "\n", $column[1]);
    foreach ($rows as $row) { ?>
    <tr> <?php echo $row; ?> </tr>
    <?php    } ?>
   </td>
</tbody>
</table>

The second foreach

vardump agenda

enter image description here

vardump rows

enter image description here

10
  • Can you var_dump($agenda); and var_dump($rows);? I'd like to know what's inside these arrays. So I can help you displaying the values. The whole code of your <tbody> would also be nice. Commented Nov 13, 2017 at 15:20
  • @RonnieOosting sure, I just edited my question Commented Nov 13, 2017 at 15:25
  • Can you also edit the <tbody> code? Commented Nov 13, 2017 at 15:26
  • @RonnieOosting how>? Commented Nov 13, 2017 at 15:27
  • 1
    @RonnieOosting Cheers m8. Hopefully OP still reads this and marks one of the answers as accepted. +1 From me :) Commented Nov 14, 2017 at 13:14

3 Answers 3

3

This will do it:

<tbody>
<?php foreach($agenda as $column):
    $rows = explode("\n", $column[1]);?>
    <tr>
        <td rowspan="<?php count($rows) + 1?>"><?php $column[0];?></td>
    </tr>

    <?php foreach($rows as $row): ?>
        <tr>
            <td><?php echo $row ;?></td>
            <td><?php echo $row ;?></td>
        </tr>
    <?php endforeach;

endforeach; ?>
</tbody>

As we (and icecub) discussed in the chatroom.

Good luck friend.

Sign up to request clarification or add additional context in comments.

Comments

2

I think it is work fine now.I tested it.

<table class="table">
    <thead>
        <tr style="background: #8da80c;">
          <th> Day </th>
          <th>Description</th>
          <th>Hours</th>
        </tr>
    </thead>
<tbody>
   <?php foreach($agenda as $column) { ?>
   <tr style="background: #e0e545;">
    <td style="padding: 14px;"><?php echo $column[0]; ?></td>
    <td style="padding: 14px;"><?php echo $column[1]; ?></td>
     <td style="padding: 14px;"><?php echo $column[2]; ?></td>
   </tr>
   <?php } ?>
</tbody>
</table>

I tested with these values

  <pre>
<?php
$agenda = array (array("montag","Lorem ipsum dolor sit amet","08:00"),array("montag","Lorem ipsum dolor sit amet","08:00"),array("montag","Lorem ipsum dolor sit amet","08:00"));

print_r($agenda);
?>
     </pre>

2 Comments

Nah. OP isn't very clear about the actual problem. In the chat it became more clear. It's a HTML / CSS issue mostly.
Mostly. The issue is that when the data inside a column becomes to long and continues on another line, the data in the next column doesn't line up. But he's not even responding in chat anymore..
1

It because you variable

$agenda = $days = json_decode(get_field( "field_uc_content_json" ));
unset($agenda[0]); ?>

it's a object and this you dont use like a array, you need manage like object in the explode, like this:

$rows = explode( "\n", $column->1);

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.