Daniele Pais
Daniele Pais

Reputation: 151

Displaying php inside html table

Long story short. Our Wordpress imports data via xml and here is the PHP code to view the output:

<?php
// check if the flexible content field has rows of data
if( have_rows('epigenetics', 'user_'.get_current_user_id()) ):

   // loop through the rows of data
  while ( have_rows('epigenetics', 'user_'.get_current_user_id()) ) : the_row();
                              the_sub_field('category');
                              the_sub_field('item_name');
                              the_sub_field('value');
                              the_sub_field('status');
                                    //add more field you desire here

  endwhile;

else :

  // no layouts found

endif;
?>

Unfortunately, the data imported is unpresentable, I was wondering if there is a way to have the PHP output for "the sub fields" in a table format such as the one below.

<table style="border-color: #396833; border-width: 1px; width: 90%; border-style: solid; background-color: #fefefe;" border="1" cellspacing="3" cellpadding="2" align="center">
<tbody>
<tr>
<td style="text-align: center;">
<p><span style="font-size: medium;">Category</span></p>
</td>
</tr>
<tr>
<td style="background-color: #92c38e; text-align: center;">
<p><span style="color: #ffffff; font-size: medium;">Item</span></p>
</td>
</tr>
<tr class="tablerow1">
<td style="text-align: center; background-color: #92c38e;">
<p><span style="font-size: medium; color: #ffffff;">Value</span></p>
</td>
</tr>
<tr class="tablerow1">
<td style="background-color: #92c38e; text-align: center;">
<p><span style="color: #ffffff; font-size: medium;">Status</span></p>
</td>
</tr>
</tbody>
</table>

So, in other words, I need the correct way to insert the PHP in the HTML table.

Upvotes: 0

Views: 1577

Answers (2)

Sebastian
Sebastian

Reputation: 525

You would just need to echo HTML alongside your field values inside the while loop. For example, you could do something along the lines of:

<?php

if (have_rows('epigenetics', 'user_'.get_current_user_id())) {
    echo '
        <table>
            <thead>
                <tr>
                    <th>Category</th>
                    <th>Item Name</th>
                    <th>Value</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
    ';

    while (have_rows('epigenetics', 'user_'.get_current_user_id())) {
        echo '
            <tr>
                <td>'.htmlentities(get_sub_field('category')).'</td>
                <td>'.htmlentities(get_sub_field('item_name')).'</td>
                <td>'.htmlentities(get_sub_field('value')).'</td>
                <td>'.htmlentities(get_sub_field('status')).'</td>
            </tr>
        ';
    }

    echo '
            </tbody>
        </table>
    ';
}

?>

Upvotes: 0

Vladut
Vladut

Reputation: 121

Not sure if I understood correctly but I guess you are looking for something like this:

<table style="border-color: #396833; border-width: 1px; width: 90%; border-style: solid; background-color: #fefefe;" border="1" cellspacing="3" cellpadding="2" align="center">
<thead>
<tr>
    <td style="text-align: center;">
        <p><span style="font-size: medium;">Category</span></p>
    </td>
    <td style="background-color: #92c38e; text-align: center;">
        <p><span style="color: #ffffff; font-size: medium;">Item</span></p>
    </td>
    <td style="text-align: center; background-color: #92c38e;">
        <p><span style="font-size: medium; color: #ffffff;">Value</span></p>
    </td>
    <td style="background-color: #92c38e; text-align: center;">
        <p><span style="color: #ffffff; font-size: medium;">Status</span></p>
    </td>
</tr>
</thead>
<?php
// check if the flexible content field has rows of data
if( have_rows('epigenetics', 'user_'.get_current_user_id()) ): ?>
  <tbody>
      <?php $count = 0;
      while ( have_rows('epigenetics', 'user_'.get_current_user_id()) ) : the_row(); 
        $count++; ?>
        <tr class="tablerow<?php echo $count; ?>">
            <td style="background-color: #92c38e; text-align: center;">
                <p><span style="font-size: medium;"><?php  the_sub_field('category'); ?></span></p>
            </td>
            <td style="background-color: #92c38e; text-align: center;">
                <p><span style="color: #ffffff; font-size: medium;"><?php the_sub_field('item_name'); ?></span></p>
            </td>
            <td style="text-align: center; background-color: #92c38e;">
                <p><span style="font-size: medium; color: #ffffff;"><?php the_sub_field('value'); ?></span></p>
            </td>                     
            <td style="background-color: #92c38e; text-align: center;">
                <p><span style="color: #ffffff; font-size: medium;"><?php the_sub_field('status'); ?></span></p>
            </td>     
      <?php endwhile; ?>
  </tbody> 
<?php else :

endif;
?>
</table>

https://jsfiddle.net/9k7tL3uu/#&togetherjs=LlKoxukHuo

Hope this helps

Upvotes: 1

Related Questions