Reputation: 37
I try to make a table html, with 2 table PHP, I don't manage to put my value which is in my 4th row, 3th column in the 4th row, 4th column.
My PHP/html:
<table BORDER=1px>
<tr>
<?php
foreach($tableau1 as $value)
{
echo '<th>'.$value.'</th>';
}
?>
</tr>
<?php
foreach($tableau as $valu)
{
echo '<tr>';
foreach($tableau1 as $val => $ke)
{
foreach($valu as $vava => $keke)
{
if($val==$vava){echo '<td>'.$keke.'</td>';}
}
}
echo '</tr>';
}
?>
</table>
My PHP table (first:table , second:table1)
array (size=3)
6 =>
array (size=2)
'marque' => string 'marque 6' (length=8)
'modele' => string 'modele 6' (length=8)
3 =>
array (size=4)
'marque' => string 'marque 3' (length=8)
'modele' => string 'modele 3' (length=8)
2 => string 'bois art3' (length=9)
4 => string 'beton art3' (length=10)
5 =>
array (size=3)
'marque' => string '-lepetit' (length=8)
'modele' => string 'modele 5' (length=8)
4 => string 'beton art5' (length=10)
array (size=4)
'marque' => string 'marque' (length=6)
'modele' => string 'modèle' (length=6)
2 => string 'bois' (length=4)
4 => string 'beton' (length=5)
Response to first answer: :) I already tried it but it's not good I get too much cell in all row and I don't understand why ...
Upvotes: 0
Views: 117
Reputation: 586
I think you need to print an empty <td></td>
when no data for the cell.
<?php
foreach($tableau as $valu)
{
echo '<tr>';
foreach($tableau1 as $val => $ke)
{
$found = false;
foreach($valu as $vava => $keke)
{
//echo '<td>' . $val . ' ' . $vava . ' ' . $keke . '</td>';
if($val==$vava){
echo '<td>'.$keke.'</td>';
$found = true;
}
}
if (!$found) { echo '<td></td>'; }
}
echo '</tr>';
}
?>
Added a boolean to flag when data was added to the column and then moved the
echo '<td></td>';
outside of the inner loop.
Upvotes: 2