Venkadesh Nagarajan Venkadesh Nagarajan - 1 month ago 6
PHP Question

Table row count confusion PHP Foreach array

I have array like this

Array ([0] => 8 [1] => 8 [2] => 1 [3]=> 7 [4] => 8 )


and I formed table row with above array using ForEach on PHP. But when you look at the above array will create 7 TDs on 3rd ROW and and 8 TDs on 4th ROW. I need to move that blank TD on 3rd Row at start of row instead showing it at end of row.. Is it possible??

<table border="1"><tbody>
<tr align="right">
<td align="right" colspan="0" rowspan="0" id="td_02">02</td>
<td align="right" colspan="0" rowspan="0" id="td_06">06</td>
<td align="right" colspan="0" rowspan="0" id="td_10">10</td>
<td align="right" colspan="0" rowspan="0" id="td_14">14</td>
<td align="right" colspan="0" rowspan="0" id="td_18">18</td>
<td align="right" colspan="0" rowspan="0" id="td_22">22</td>
<td align="right" colspan="0" rowspan="0" id="td_26">26</td>
<td align="right" colspan="0" rowspan="0" id="td_30">30</td>
</tr>
<tr align="right">
<td align="right" colspan="0" rowspan="0" id="td_01">01</td>
<td align="right" colspan="0" rowspan="0" id="td_05">05</td>
<td align="right" colspan="0" rowspan="0" id="td_09">09</td>
<td align="right" colspan="0" rowspan="0" id="td_13">13</td>
<td align="right" colspan="0" rowspan="0" id="td_17">17</td>
<td align="right" colspan="0" rowspan="0" id="td_21">21</td>
<td align="right" colspan="0" rowspan="0" id="td_25">25</td>
<td align="right" colspan="0" rowspan="0" id="td_29">29</td>
</tr>
<tr align="right">
<td>XX</td>
<td>XX</td>
<td>XX</td>
<td>XX</td>
<td>XX</td>
<td>XX</td>
<td>XX</td>
<td>XX</td>
</tr>
<tr align="right">
<td align="right" colspan="0" rowspan="0" id="td_03">03</td>
<td align="right" colspan="0" rowspan="0" id="td_07">07</td>
<td align="right" colspan="0" rowspan="0" id="td_11">11</td>
<td align="right" colspan="0" rowspan="0" id="td_15">15</td>
<td align="right" colspan="0" rowspan="0" id="td_19">19</td>
<td align="right" colspan="0" rowspan="0" id="td_23">23</td>
<td align="right" colspan="0" rowspan="0" id="td_27">27</td>
</tr>
<tr align="right">
<td align="right" colspan="0" rowspan="0" id="td_31">31</td>
<td align="right" colspan="0" rowspan="0" id="td_04">04</td>
<td align="right" colspan="0" rowspan="0" id="td_08">08</td>
<td align="right" colspan="0" rowspan="0" id="td_12">12</td>
<td align="right" colspan="0" rowspan="0" id="td_16">16</td>
<td align="right" colspan="0" rowspan="0" id="td_20">20</td>
<td align="right" colspan="0" rowspan="0" id="td_24">24</td>
<td align="right" colspan="0" rowspan="0" id="td_28">28</td>
</tr>
</tbody></table>



Answer

I've got absolutely no idea how you are generating your table, but..

One way to avoid the "blank" td issue is to make sure all rows have the same amount of td's, which can be done by finding the max value of the array.

For example:

<?php
    $row_array = array(8, 8, 1, 7, 8);
    $num_tds = max($row_array);
?>
<table border="1" cellpadding="3" cellspacing="3">
    <?php foreach($row_array as $row) : ?>
    <tr>
        <?php for ($i = 0; $i < $num_tds; $i++) : ?>
        <td>&nbsp;</td>
        <?php endfor; ?>
    </tr>
    <?php endforeach; ?>
</table>

Without actually seeing your code I am just offering blind solutions but I hope it helps.

Comments