0

I have the next array

$arreglo=[1,2,3,4,5..50]

I want to show that array in a table but I want that table have 10 columns, for example:

1   2  3  4  5  6  7  8  9 10
11 12 13 14 15 16 17 18 19 20
etc...

the number of rows depends on the size of the array divided by 10, for example if I have 50 elements that means that I will have 5 rows (50/10)

I have the next code but doesn't work:

$html2 = 
    '<br>
    <br>
    <br>
    <table style="border: 1px solid #333; text-align:center; line-height: 20px; font-size:10px">';
    for($j=1;$j<=$rows;$j++){
         $html2.='<tr>';
            for($k=1;$k<=$cols;$k++){
                $html2.='<td style="border: 1px solid #666;">Caja#1'.$datos[$k].'</td>';
            }
        $html2.='</tr>';    
    }
    $html2.='</table>';

How could I do this?

Baker1562
  • 337
  • 3
  • 20

1 Answers1

0

Modify your code like below code, hope you will get your desired result. Now we are starting $j from 0, then we have used another loop for getting array values for 10 times. At the same time, we are increasing $j's value & using $j's index to find array's data. After 10 times, it will break that loop & will end your <tr> & initiate another <tr>.

for($j = 0; $j < $rows;){
         $html2.='<tr>';
            for($k = 0;$k < 10; $k++){
                $html2.='<td style="border: 1px solid #666;">Caja#1'.$datos[$j].'</td>';
             $j++;
            }
        $html2.='</tr>';    
    }
Jobayer
  • 1,221
  • 1
  • 14
  • 22