Saturday 13 February 2016

php - Continue loop after 8 results - separating into whole new table



I need to alter my code to display 1 table containing 8 results, stop, then produce another whole new table with result number 9 and above.



I've got the idea that the break; and continue; may be of use, but how I should wrap the whole table in a foreach loop, and prevent it displaying 8+ tables I don't know.



I am determining that there is more than 8 columns by the HEADER count. In this example there is 9 headers. Including the first blank one.




enter image description here




$table3 = get_field( 'bottom_chart' );

if ( $table3 ) {

if($table3['header']) {

$theader3 = 1;
foreach ( $table3['header'] as $th1 ) {
//echo $theader3;
$theader3++;
}
}

echo '';

if ( $table3['header'] ) {


echo '';

echo '';

foreach ( $table3['header'] as $t3 ) {

echo '';

}

echo '';

echo '';
}

echo '';

$first_td_bottomchart = 0;


foreach ( $table3['body'] as $tr3 ) {

echo '';

foreach ( $tr3 as $td3 ) {

if(($first_td_bottomchart %8) == 0) {
echo '';
} elseif(!empty($td3['c'])) {

echo '';
}
$first_td_bottomchart++;
} ?>

';


}

echo '';

echo '
';
echo $t3['c'];
echo '
' . $td3['c'] . '';
echo '
';
echo $td3['c'];
echo '
';

}

?>


Answer



Ok so if we assume that $table3['body'] has the same count of elements as $table3['header'] your code basically starts building the table header fine, but when we get to the body during the first 8 loops you have the table create
rowspans?



Should this not be colspans?
https://plnkr.co/edit/B31QPDamCDHiQANAYpdx?p=preview






































Also after the %8 the same row carries on with rowspan depending on the amount of elements in $tr3 as $td3. (if this is consistent with the amount of elements in $table3['header'];)




Another problem is that if for some reason $td3['c'] is empty the whole Table structure is thrown out the window.



You are relying on too many unknown variables to build the table structure for youself.



I would only loop through $table3['header'] once and build your table logic inside this loop to prevent confusion and mismatched counts of elements.



It is unclear which cells you want to span over which cells: could you please merge the cells you want inside an Excel sheet as an example and attach that with your question. Then I can provide a simpler solution for you.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...

1 Rowspan Table 2 3 4 5 6 7 8 9
$td3['c']              
$td3['c']    

Blog Archive