I have the following php array and I am trying to print a table with 4 columns. All is OK until the array has 4, 8, 12 and etc. keys /values but when array has length that can not be divided by 4 becomes the problem. Here is the sample array:
$newarray =array(
"make"=> "Ford" ,
"model"=> "S-MAX" ,
"model_year"=> "2009" ,
"made"=> "2010-01-01" ,
"manufacturer"=> "Ford Werke AG" ,
"manufacturer_address"=> "Koeln-Niehl, Germany" ,
"body"=> "Sedan/Saloon" ,
"engine_power_kw"=> "142" ,
"engine_displacement_ccm"=> "2000" ,
"engine_full"=> "2.0L Duratorq-TDCi (143PS) - DW"
);
and the code that prints the table:
$rama_result.='<table class="table w100 customtable">';
$i=1;
foreach($newarray as $key=>$value){
$rama_result1.= '<th>'.$key.'</th>';
$rama_result2.= '<td>'.$value.'</td>';
if($i % 4 == 0){
$rama_result.='</tr><tr>'.$rama_result1.'</tr><tr>'.$rama_result2.'</tr><tr>';
$rama_result1 = '';$rama_result2 = '';
}
$i++;
}
$rama_result.='</tr></table>';
Can you please help and show me the way to print the missing 2 in the last row so the code to work correct. Thank you for your help
The code above prints this table
<table class="table w100 customtable"><tbody>
<tr><th>make</th><th>model</th><th>model_year</th><th>made</th></tr>
<tr><td>Ford</td><td>S-MAX</td><td>2009</td><td>2010-01-01</td></tr><tr></tr>
<tr><th>manufacturer</th><th>manufacturer_address</th><th>body</th><th>engine_power_kw</th></tr>
<tr><td>Ford Werke AG</td><td>Koeln-Niehl, Germany</td><td>Sedan/Saloon</td> <td>142</td></tr>
<tr></tr></tbody></table>
<table class="table w100 customtable"><tbody> <tr><th>make</th><th>model</th><th>model_year</th><th>made</th></tr> <tr><td>Ford</td><td>S-MAX</td><td>2009</td><td>2010-01-01</td></tr><tr></tr> <tr><th>manufacturer</th><th>manufacturer_address</th><th>body</th><th>engine_power_kw</th></tr> <tr><td>Ford Werke AG</td><td>Koeln-Niehl, Germany</td><td>Sedan/Saloon</td> <td>142</td></tr> <tr></tr></tbody></table>
Instead of looping each value use array_chunk to split the array then implode the headers and values.
$arr = array_chunk($newarray, 4, true);
echo "<table>\n";
foreach($arr as $sub){
echo "<tr><th>" . implode("</th><th>", array_keys($sub)) . implode("</th><th>", array_slice([" ", " ", " "],0 , 4-count($sub))) . "</th></tr>\n";
echo "<tr><td>" . implode("</td><td>", $sub) . implode("</td><td>", array_slice([" ", " ", " "],0 , 5-count($sub))) ."</td></tr>\n";
}
echo "</table>";
Output:
<table>
<tr><th>make</th><th>model</th><th>model_year</th><th>made</th></tr>
<tr><td>Ford</td><td>S-MAX</td><td>2009</td><td>2010-01-01 </td></tr>
<tr><th>manufacturer</th><th>manufacturer_address</th><th>body</th><th>engine_power_kw</th></tr>
<tr><td>Ford Werke AG</td><td>Koeln-Niehl, Germany</td><td>Sedan/Saloon</td><td>142 </td></tr>
<tr><th>engine_displacement_ccm</th><th>engine_full </th><th> </th></tr>
<tr><td>2000</td><td>2.0L Duratorq-TDCi (143PS) - DW </td><td> </td><td> </td></tr>
</table>
Edited to include the empty cells.
After the loop, you could check that there is something still in $rama_result1
- which would mean there is data left to be added into the table. This code pads the existing content with enough empty cells to make it up to the 4 columns (you can tweak the content if desired in the str_repeat()
calls).
if ( $rama_result1 != '' ) {
$i--;
$rama_result1 .= str_repeat("<th />", 4-($i%4));
$rama_result2 .= str_repeat("<td />", 4-($i%4));
$rama_result.='<tr>'.$rama_result1.'</tr><tr>'.$rama_result2.'</tr>';
}
Also (as Nick pointed out) there are extra <tr>
tags in various places. I've updated a few other parts of the code to try and tidy up the generated HTML...
$i=1;
foreach($newarray as $key=>$value){
$rama_result1.= '<th>'.$key.'</th>';
$rama_result2.= '<td>'.$value.'</td>';
if($i % 4 == 0){
$rama_result.='<tr>'.$rama_result1.'</tr><tr>'.$rama_result2.'</tr>'.PHP_EOL;
$rama_result1 = '';
$rama_result2 = '';
}
$i++;
}
if ( $rama_result1 != '' ) {
$i--;
echo ($i%4).PHP_EOL;
$rama_result1 .= str_repeat("<th />", 4-($i%4));
$rama_result2 .= str_repeat("<td />", 4-($i%4));
$rama_result.='<tr>'.$rama_result1.'</tr><tr>'.$rama_result2.'</tr>'.PHP_EOL;
}
$rama_result.='</table>';
You could just extend the array to have a multiple of 4 elements before you start:
for ($c = count($newarray); $c % 4 != 0; $c++) {
$newarray[str_repeat("\n", $c % 4)] = '';
}
$rama_result.='<table class="table w100 customtable">';
$i=1;
// ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.