• Resolved athep

    (@athep)


    Hello, I’ve been trying to display the repeated data into a table but couldn’t find any example on your youtube channel or here, maybe I missed something.

    I have the data in an array

    $array = array(
    	get_post_meta( get_the_ID(), 'data1', true ),
     	get_post_meta( get_the_ID(), 'data2', true  ),
     	get_post_meta( get_the_ID(), 'data3', true  ),
    );

    and I’m trying to put it into a table:

    echo '<table class="table-group">';
    foreach ( $array as $row ) {
    	echo('<tr>');
    	echo('<td>');
    	echo( implode( '</td><td>', $row ) );
    	echo('</td>');
    	echo('</tr>');
    }
    echo '</table>';

    The table works, it shows the data but not in the order I want.. it puts all the results of data1…data3 in a column instead of a row. The problem is I’m not sure how I can select the first entry in the array to correspond with the second array’s first entry and so forth. I hope it makes sense?

    If this is all wrong kindly refer to me how I would be able to display repeated fields data in a table. Thank you again.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter athep

    (@athep)

    Hello, I managed to get this working but with code you may not agree with because I think it’s really inefficient..

    So I realized that the meta key for repeated fields start with an empty key ” ” and then an increment of “_row-1” “_row-2” etc.. with that in mind I came up with this:

    $array = array(
    	get_post_meta( get_the_ID(), 'data1', true ),
    	get_post_meta( get_the_ID(), 'data2', true  ),
    	get_post_meta( get_the_ID(), 'data3', true  ),
    );
    
    echo '<table class="table-group">';
    
    //the following shows the first group
    echo('<tr>');
    echo('<td>');
    echo $array[0][''];
    echo('</td>');
    echo('<td>');
    echo $array[1][''];
    echo('</td>');
    echo('<td>');
    echo $array[2][''];
    echo('</td>');
    echo('</tr>');
    
    //a counter that checks the length of the first array
    $count = count( $array[0] );
    $i = 1;
    
    while( $i < $count ) {
            //a number is appended to _row- 
    	$str = '_row-';
    	$x = $str . $i;
    	
    	echo('<tr>');
    	echo('<td>');
    	echo $array[0][$x];
    	echo('</td>');
    	echo('<td>');
    	echo $array[1][$x];
    	echo('</td>');
    	echo('<td>');
    	echo $array[2][$x];
    	echo('</td>');
    	echo('</tr>');
    	
    	$i = $i + 1;
    }
    
    echo '</table>';
    Plugin Author Aurovrata Venet

    (@aurovrata)

    So the data is stored by columns, now rows, ie each field represents a column.

    So if you have a table with 3 fields ( and I am assuming you’re using this plugin with the Smart Grid extension) then you need to do the following,

    1. retrieve your 3 fields ($field1, $field2, $field3)

    2. loop through each row 0, 1, 2 which are the field array indeces,

    
    for($idx = 0; $idx < sizeof($field1); $idx++){
      //row($idx) would be $field1[$idx], $field2[$idx], $field3[$idx]
    }
    
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Display repeated fields’ is closed to new replies.