Reputation: 4187
I have a list product
Invoice No. | Product Name | Quantity
INV001 | iPhone | 1
INV001 | iPad | 2
INV001 | iPod | 1
INV002 | iMac | 2
INV002 | iPhone | 1
After fetch all data to array, how ideas to create table auto rowspan when Invoice No. same value.
Invoice No. | Product Name | Quantity
INV001 | iPhone | 1
| iPad | 2
| iPod | 1
INV002 | iMac | 2
| iPhone | 1
<table>
<tr>
<th>Invoice No.</th>
<th>Product Name</th>
<th>Quantity</th>
</tr>
<tr>
<td rowspan="3">INV001</td>
<td>iPhone</td>
<td>1</td>
</tr>
<tr>
<td>iPad</td>
<td>2</td>
</tr>
<tr>
<td>iPod</td>
<td>1</td>
</tr>
<tr>
<td rowspan="2">INV001</td>
<td>iMac</td>
<td>2</td>
</tr>
<tr>
<td>iPhone</td>
<td>1</td>
</tr>
</table>
Upvotes: 1
Views: 2760
Reputation: 69
$data = // query result
$last_key = null;
$dum_arr = array();
foreach ($data as $key => $row) {
if($row->name == $last_key) {
$dum_arr[] = $key;
}
if(!in_array($key, $dum_arr)) {
// echo rowspan here...
}
$last_key = $row->name;
}
Upvotes: 1
Reputation: 1792
You need to make sure your rows are sorted by invoice number, then you need to count each occurence of an invoice number, that value will be your rowspan number.
Here is an example :
$invoiceNbOccurences = array();
foreach($rows as $row){
$invoiceNb = $row['invoice_nb'];
if(!isset($invoiceNbOccurences[$invoiceNb])){
$invoiceNbOccurences[$invoiceNb] = 0;
}
$invoiceNbOccurences[$invoiceNb]++;
}
And how to use it :
<td rowspan="<?php echo $invoiceNbOccurences['INV001'];?>"></td>
<td rowspan="<?php echo $invoiceNbOccurences['INV002'];?>"></td>
Upvotes: 1