johannheyne / advanced-custom-fields-table-field

Advanced Custom Fields: Table Field
15 stars 4 forks source link

Error when placed inside a foreach loop #6

Closed jirodearmas closed 5 years ago

jirodearmas commented 6 years ago

I tried to place the table inside a foreach loop and it stops working. Error (Invalid argument supplied for foreach()) is in the line foreach ( $table['header'] as $th ) Help fix this issue

foreach ( $children as $child ) {

  $table = get_post_meta($child->ID,'product_table',true);
  if ( $table ) {
    $table_contents = '<table border="0">';
    if ( $table['header'] ) {
      $table_contents .= '<thead>';
      $table_contents .= '<tr class="header-column">';
      foreach ( $table['header'] as $th ) {
        $table_contents .= '<th>';
        $table_contents .= $th['c'];
        $table_contents .= '</th>';
      }
      $table_contents .= '</tr>';
      $table_contents .= '</thead>';
    }
    $table_contents .= '<tbody>';
    foreach ( $table['body'] as $tr ) {
      $table_contents .= '<tr>';
      foreach ( $tr as $td ) {
        $table_contents .= '<td>';
        $table_contents .= $td['c'];
        $table_contents .= '</td>';
      }
      $table_contents .= '</tr>';
    }
    $table_contents .= '</tbody>';
    $table_contents .= '</table>';
  }

  echo $table_contents;
}
johannheyne commented 6 years ago

Hi,

please try… if ( is_array( $table['header'] ) and count( $table['header'] ) > 0 ) {

instead of… if ( $table['header'] ) {

jirodearmas commented 6 years ago

Hi,

Replaced the line with the one you send. And now i'm getting error on the second foreach This line: foreach ( $table['body'] as $tr ) { Can you help find another way to display table from child pages to a parent page. That's what i'm trying to do with my post above.

johannheyne commented 6 years ago

replace $table = get_post_meta($child->ID,'product_table',true); with $table = get_field( 'product_table', $child->ID );