Morten Hagh Morten Hagh - 1 month ago 11
PHP Question

New tr after 5 td in php and tcpdf

I have a little problem with getting a new

<tr>
after every 5
<td>
when making a PDF using TCPDF:

$bricks = "";
$bricks .= '<table>';
$bricks .= '<tr>';

foreach($bricks_selected as $key => $value) {
$num_check = substr($value, 4);

if($num_check != "0") {
$bricks .= '<td width="90" height="80"><img src="/var/www/brickmixer/bricks/tavler/RT'.substr($value, 0, 3).'.jpg" width="80" height="55"><br><b>RT '. substr($value, 0, 3) .' - '. $num_check .'%</b></td>';
}

}


I have tried with division etc. but it gives me some funny results.

The
$bricks_selected
variable are set in the top of the script:

foreach($_POST as $key => $value) {
if($key == "bricks-selected") {
$bricks_selected = explode("&", $value);
}
}

Answer

Add an incremental variable and check every 5 td's:

$i = 1;
foreach($bricks_selected as $key => $value) {
    $num_check = substr($value, 4);
    if($num_check != "0") {                
        $bricks .= '<td width="90" height="80"><img src="/var/www/brickmixer/bricks/tavler/RT'.substr($value, 0, 3).'.jpg" width="80" height="55"><br><b>RT '. substr($value, 0, 3) .' - '. $num_check .'%</b></td>';       
    } 
    if($i%5 == 0){
         echo '</tr><tr>';
    }
    $i++;
}