[Closed] Create Table rows with a foreach loop

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

This topic contains 3 replies, has 2 voices.

Last updated by Adriano 5 years, 10 months ago.

Assigned support staff: Adriano.

Author
Posts
#206999

Hi

I am using the Types plugin and it almost does what I want. I need to able to have a table that can be added to depending on the needs of individual posts. I am building a website for condos and I need to be able to give unit info per project. Some projects have 2 units, others have 4-5 or more. I can create a table with the plugin, but when I run a foreach loop to add new rows to the table, the table gets messed up. Here is the code I have written:

<?php 

echo '<table class="table-striped table-bordered table-responsive table">';
echo
'<tr>
	<th>Unit Name</th>
  	<th>Number of Beds</th>
  	<th>Size M²</th>
  	<th>Price from THB</th>
  	<th>Price up to THB</th>
</tr>';
echo '<tr>';
$unit_types=get_post_meta($post->ID,'wpcf-unit-type');

foreach ($unit_types as $unit_type) {

        echo '<td>'.$unit_type.'</td>';

}
$num_beds=get_post_meta($post->ID,'wpcf-number-of-beds');

foreach ($num_beds as $num_bed) {

        echo '<td>'.$num_bed.'</td>';

}
$size_sq_mtrs=get_post_meta($post->ID,'wpcf-size-square-meters');

foreach ($size_sq_mtrs as $size_sq_mtr) {

        echo '<td>'.$size_sq_mtr.'</td></tr>';

}
$prices_from=get_post_meta($post->ID,'wpcf-price-from');

foreach ($prices_from as $price_from) {

        echo '<td>'.$price_from.'</td>';

}
$prices_to=get_post_meta($post->ID,'wpcf-price-to');

foreach ($prices_to as $price_to) {

        echo '<td>'.$price_to.'</td>';

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

?>

The problem is that the <tr> surrounds the foreach loop and rather than a new row being created on each iteration, it just extends the row for as long as there are new fields.

How can I modify this code to create a new row on each iteration of those 5 fields?

#207161

Adriano
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Ryan,

That is not directly related with Toolset, but I will try to give you some help. The best way is to put <td> outside of the foreach. The thing is when some of those custom field has more than one value, then one more column is added, it is not right. See an example, on this case I use <td> outside of the foreach, so even this custom field has many values, the layout of the table will not be changed:

echo '<td>';
foreach ($prices_to as $price_to) {
        echo $price_to.' ';
}
echo '</td>';

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

#207233

Hello Adriano, thanks for your reply. This gave me some hope but didn't quite work. What I want is to use multiple instances of the same custom field and with each new instance it will go into a new column and new row without messing up the layout. This way you suggested puts the new instances in the same data cell.

Thank you so much for your help. This is making me crazy now! I think I have been trying to solve this for about 5 days now

#207420

Adriano
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Ryan,

Right, the code below should works:

<?php 

echo '<table class="table-striped table-bordered table-responsive table">';
echo
'<tr>
    <th colspan="'.count(get_post_meta($post->ID,'wpcf-unit-type'));.'">Unit Name</th>
    <th colspan="'.count(get_post_meta($post->ID,'wpcf-number-of-beds'));.'">Number of Beds</th>
    <th colspan="'.count(get_post_meta($post->ID,'wpcf-size-square-meters'));.'">Size M²</th>
    <th colspan="'.count(get_post_meta($post->ID,'wpcf-price-from'));.'">Price from THB</th>
    <th colspan="'.count(get_post_meta($post->ID,'wpcf-price-to'));.'">Price up to THB</th>
</tr>';
echo '<tr>';
$unit_types=get_post_meta($post->ID,'wpcf-unit-type');
 
foreach ($unit_types as $unit_type) {
 
        echo '<td>'.$unit_type.'</td>';
 
}
$num_beds=get_post_meta($post->ID,'wpcf-number-of-beds');
 
foreach ($num_beds as $num_bed) {
 
        echo '<td>'.$num_bed.'</td>';
 
}
$size_sq_mtrs=get_post_meta($post->ID,'wpcf-size-square-meters');
 
foreach ($size_sq_mtrs as $size_sq_mtr) {
 
        echo '<td>'.$size_sq_mtr.'</td></tr>';
 
}
$prices_from=get_post_meta($post->ID,'wpcf-price-from');
 
foreach ($prices_from as $price_from) {
 
        echo '<td>'.$price_from.'</td>';
 
}
$prices_to=get_post_meta($post->ID,'wpcf-price-to');
 
foreach ($prices_to as $price_to) {
 
        echo '<td>'.$price_to.'</td>';
 
}
echo '</tr>';
echo '</table>';     
 
?>

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

The topic ‘[Closed] Create Table rows with a foreach loop’ is closed to new replies.