I have this table: The data inside the table are results from mysql query. Each item has a total of 8 classes. The table will display each item and its classes that have a quantity only. If there is 0 quantity of item in a class, the class will not shown.(This explanation is just for your understanding.) What I have so far is something like this:
|--------------|------------|----------|------------|
| Item Name | Item Class | Quantity | Total |
|--------------|------------|----------|------------|
| | A | 10 |
| |------------|----------|
| Upper Bearing| B | 5 |
| |------------|----------|------------|
| | C | 4 | 19 |
|--------------|------------|----------|------------|
| | A | 8 |
| Lower Bearing|------------|----------|------------|
| | D | 2 | 10 |
|--------------|------------|----------|------------|
| | B | 2 |
| |------------|----------|
| Vane | D | 11 |
| |------------|----------|------------|
| | E | 2 | 15 |
|--------------|------------|----------|------------|
The Total column is the sum of all quantities for each item. For example, Upper bearing, Total=Qty(A)+Qty(B)+Qty(C)= 10+5+4= 19. Take note that the quantity for each class is a result of some calculation, and not selected directly from the database.
What I want to do is to rowspan the <td> for Total so that it fill the whole row like Item Name did.
The expected output should look like this:
|--------------|------------|----------|------------|
| Item Name | Item Class | Quantity | Total |
|--------------|------------|----------|------------|
| | A | 10 | |
| |------------|----------| |
| Upper Bearing| B | 5 | 19 |
| |------------|----------| |
| | C | 4 | |
|--------------|------------|----------|------------|
| | A | 8 | |
| Lower Bearing|------------|----------| 10 |
| | D | 2 | |
|--------------|------------|----------|------------|
| | B | 2 | |
| |------------|----------| |
| Vane | D | 11 | 15 |
| |------------|----------| |
| | E | 2 | |
|--------------|------------|----------|------------|
This is my code:
<table>
<tr>
<td>Item Name</td>
<td>Item Class</td>
<td>Item Quantity</td>
<td>Total</td>
</tr>
<?php
$result=mysql_query("SELECT * FROM tblitem INNER JOIN itemloc ON tblitem.itemId=itemloc.itemId INNER JOIN refitemclass ON tblitem.itemClassId=refitemclass.itemClassId");
$classqty=array();
while($row = mysql_fetch_array($result)){
$item=$row['itemNm'];
$class=$row['itemClassName'];
if(!isset($classqty[$item][$class]))
{
$classqty[$item][$class] = 0;
}
if(is_null($row['itemLocCheckOut'])){
$classqty[$item][$class] += $row['itemLocQty'];
}
else{
$classqty[$item][$class] -= $row['itemLocQty'];
}
}
$sum=array();
foreach($classqty as $k1=>$v1){
foreach($v1 as $k2=>$v2){
if(!isset($sum[$k1])){
$sum[$k1] = $v2;
}
else
{
$sum[$k1] += $v2; /*calculation for total*/
}
?>
<tr id="table2">
<td><?php echo $k1;?></td> /*output the item name*/
<td><?php echo $k2;?></td> /*output the item class*/
<td><?php echo $v2;?></td> /*output the quantity for item class*/
<?php
}
?>
<td><?php echo $sum[$k1];?></td> /*output the total*/
</tr>
<?php
}
?>
</table>
And here is the javascript to make rowspan for the item name.
<script>
$(document).ready(function() {
var span = 1;
var prevTD = "";
var prevTDVal = "";
$("#table2 td:first-child").each(function() { //for each first td in every tr
var $this = $(this);
if ($this.text() == prevTDVal) { // check value of previous td text
span++;
if (prevTD != "") {
prevTD.attr("rowspan", span); // add attribute to previous td
$this.remove(); // remove current td
}
} else {
prevTD = $this; // store current td
prevTDVal = $this.text();
span = 1;
}
});
});
</script>
I've tried things like <td rowspan=<?=count[$k2]?><?php echo $sum[$k1];?></td> and make the same javascript like for item name by just changing $("#table2 td:first-child") to $("#table2 td:last-child") but none of these works. The total is still in the same row with last row of item class.
P/S: Please don't suggest me to make any changes in the calculation part as the calculation works fine and the results are all correct. My problem is just the output structure of total quantity.
foreach($classqty as $k1=>$v1)loop, and then build your table after.the whole concept of [your] question. I was just wondering if you are open to doing it a different way that involved less code, and no need for javascript.rowspan. Using javascript would work, but will be a little more challenging, as you can't use your method that you did for theItem Name, as you may have times when siblingItem Names have the sameTotalso it wouldrowspantoo many rows. My javascript coding is a little slower than my php coding, but I could give it a try.