I have 2 table : item_table
and transaction_table
:
item_table :
+-------+---------+
|id_item|item_name|
+-------+---------+
|i_1 |item_a |
+-------+---------+
|i_2 |item_b |
+-------+---------+
|i_3 |item_c |
+-------+---------+
|i_4 |item_d |
+-------+---------+
transaction_table
+--------------+----------------+-------+
|id_transaction|transaction_code|id_item|
+--------------+----------------+-------+
|1 |t_1 |i_4 |
+--------------+----------------+-------+
|2 |t_1 |i_1 |
+--------------+----------------+-------+
|3 |t_2 |i_2 |
+--------------+----------------+-------+
|4 |t_3 |i_1 |
+--------------+----------------+-------+
|5 |t_3 |i_4 |
+--------------+----------------+-------+
|6 |t_3 |i_3 |
+--------------+----------------+-------+
I have input those 2 table into 2 array i call item_array
and transaction_array
. From those 2 array, i want to make tabular output like below :
+----------------+---+---+---+---+
|transaction_code|i_1|i_2|i_3|i_4|
+----------------+---+---+---+---+
|t_1 |1 |0 |0 |1 |
+----------------+---+---+---+---+
|t_2 |0 |1 |0 |0 |
+----------------+---+---+---+---+
|t_3 |1 |0 |1 |1 |
+----------------+---+---+---+---+
my code below not output correctly
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>kd transaksi</th>
<?php for ($k = 0; $k < 1513; $k++) {
echo "<th>" . $item_array[$k]['item_name'] . "</th>";
}
?>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < 5; $i++) {
echo "<tr><td>" . $transaction_array[$i]['transaction_code'] . "</td>";
for ($j = 0; $j < 1513; $j++) {
if ($transaction_array[$i]['id_item'] == $item_array[$j]['id_item']) {
echo "<td>1</td>";
}
else {
echo "<td>0</td>";
}
}
echo "</tr>";
}
?>
</tbody>
</table>
this is the result that incorrect :
+----------------+---+---+---+---+
|transaction_code|i_1|i_2|i_3|i_4|
+----------------+---+---+---+---+
|t_1 |0 |0 |0 |1 |
+----------------+---+---+---+---+
|t_1 |1 |0 |0 |0 |
+----------------+---+---+---+---+
|t_2 |0 |1 |0 |0 |
+----------------+---+---+---+---+
|t_3 |0 |0 |0 |1 |
+----------------+---+---+---+---+
|t_3 |1 |0 ||0 |0 |
+----------------+---+---+---+---+
|t_3 |0 |0 |1 |0 |
+----------------+---+---+---+---+