-5

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

<?php

$table3 = get_field( 'bottom_chart' );

if ( $table3 ) {

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

    echo '<table border="0" class="mytable">';

        if ( $table3['header'] ) {

            echo '<thead>';

                echo '<tr>';

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

                        echo '<th class="tdtitle">';
                            echo $t3['c'];
                        echo '</th>';
                    }

                echo '</tr>';

            echo '</thead>';
        }

        echo '<tbody>';

            $first_td_bottomchart = 0;

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

                echo '<tr>';

                    foreach ( $tr3 as $td3 ) {

                        if(($first_td_bottomchart %8) == 0) {
                            echo '<td class="lefttitle bold tdtitle">' . $td3['c'] . '</td>';
                        } elseif(!empty($td3['c'])) {
                            echo '<td rowspan="4">';
                                echo '<div class="progress progress-striped"><div class="bottom_chart progress-bar progress-bar-danger" role="progressbar" aria-valuenow="' . $td3['c'] . '" aria-valuemin="0" aria-valuemax="100" style="width: 0%"></div></div>';
                                echo $td3['c'];
                            echo '</td>';
                        }
                        $first_td_bottomchart++;
                    } ?>

                <?php echo '</tr>';

            }

        echo '</tbody>';

    echo '</table>';

}

?>
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
wharfdale
  • 1,148
  • 6
  • 23
  • 53
  • You might be overthinking this. Just keep track of how many rows you've edited out - if it's 8, then re-set the counter, and print out code to close off the last table and start a new one. – andrewsi May 13 '16 at 01:34
  • 2
    Stackoverflow is for learning how to code or help with problems you cannot find a solution to, not having other people write your code for you. Andrewsi's comment is a sufficient and appropriate answer to this. I'm no moderator, but I believe it to be inappropriate to be using bounties to bribe people for code. – starshine531 May 16 '16 at 09:16

1 Answers1

1

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

<table width="100%" border="1" cellspacing="0" cellpadding="0">
<thead>
        <th>1 Rowspan Table</th>
        <th>2</th>
        <th>3</th>
        <th>4</th>
        <th>5</th>
        <th>6</th>
        <th>7</th>
        <th>8</th>
        <th>9</th>
  </thead>
<tbody>
<!-- **Invalid table structure and markup:** -->
<tr>
  <td class="lefttitle bold tdtitle">$td3['c']</td>
  <td rowspan="4">&nbsp;</td>
  <td rowspan="4">&nbsp;</td>
  <td rowspan="4">&nbsp;</td>
  <td rowspan="4">&nbsp;</td>
  <td rowspan="4">&nbsp;</td>
  <td rowspan="4">&nbsp;</td>
  <td rowspan="4">&nbsp;</td>
</tr>
<!-- **VALID table structure and markup:** -->
<tr>
  <td class="lefttitle bold tdtitle">$td3['c']</td>
  <td colspan="4">&nbsp;</td>
  <td colspan="4">&nbsp;</td>
</tr>

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.

johan
  • 998
  • 6
  • 20