Hello! Gentlemen, dear gurus, please tell me. There is a table:

enter image description here

This whole thing is generated like this:

<table style="border:1px solid #ccc;width:300px;"> <?php $t = 0; $bg = 0; for($i=0;$i<35;$i++){ if($bg < 5){ $bgr = 'fff'; }else{ $bgr = 'ccc'; } ?> <tr style="background:#<?php echo $bgr; ?>;text-align:center;"> <?php for($j=0;$j<3;$j++){ ?><td><? echo $t; ?></td><? $t++; } ?> </tr> <?php $bg++; if($bg == 10){ $bg = 0; } } ?> </table> 

How to modify the above code to add cells to this table, vertically combining 5 horizontal ones generated in a loop. The result should be like this:

enter image description here

I just can't catch up with how to organize it in a running cycle. Tell me please. Thank!

    1 answer 1

    Try this:

     for($j=0; $j<3; $j++) { if (($i === 0 || $i % 5 === 0) && $j === 0) { ?><td rowspan="5"><? echo $letters[$i/5]; ?></td><? }?> <td> <? echo $t; ?> </td> $t++; } 

    Somewhere before the first loop, create an array

     $letters = ['A', 'B', 'C', ...]; 
    • Thank! Already something: i73.fastpic.ru/big/2016/0324/cf/ ... maybe you need somewhere else to thrust an exception? - Incognito
    • I did it like this, it helped: for ($ j = 0; $ j <3; $ j ++) {if ($ i === 0 || $ i% 5 === 0) {if ($ j === 0 ) {?> <td rowspan = "5"> 11 </ td> <? }} Tell me, can this be the right decision? - Incognito
    • In principle, everything suits. Thanks for the tip! - Incognito
    • @Incognito, yes, this is the right decision, because You need to add a cell only in the 0th column, and not in all. I recommend that you look in the direction of smarty, twig or another template engine, since html mixed with php looks awful). This code is very difficult to maintain, and over time it will become impossible. - Sviderskiy Dmitriy
    • Thank! I'll try to dig in the direction of your recommendations. - Incognito