数据库表包含以下数据。我按验船师名称选择收货总数和分组。
这很好用:
surveyor bookingdate receipt_no
raj 19-03-2015 55
raj 19-03-2015 55
raj 19-03-2015 55
raj 19-03-2015 55
raj 19-03-2015 55
raj 19-03-2015 55
raj 19-03-2015 56
raj 19-03-2015 56
raj 19-03-2015 56
raj 19-03-2015 56
raj 19-03-2015 56
raj 19-03-2015 56
sudnyesh 20-03-2015 700
sudnyesh 20-03-2015 700
sudnyesh 20-03-2015 700
sudnyesh 20-03-2015 700
sudnyesh 20-03-2015 700
sudnyesh 20-03-2015 700
RAJ 20-03-2015 701
RAJ 20-03-2015 701
RAJ 20-03-2015 701
RAJ 20-03-2015 701
RAJ 20-03-2015 701
RAJ 20-03-2015 701
angel 24-03-2015 702
angel 24-03-2015 702
angel 24-03-2015 702
angel 24-03-2015 702
angel 24-03-2015 702
angel 24-03-2015 702
当前输出:
surveyorname 19-03-2015 20-03-2015 24-03-2015
raj 2 1 -
sudnyesh - 1 -
angel - - 1
我只需要显示如下例子的总和。
预期产出:
surveyorname 19-03-2015 20-03-2015 24-03-2015 TotalSUM
raj 2 1 - 3
Sudnyesh - - 1 1
angel - - 1 1
TotalSum 2 1 2 -
代码:
<?php $book = $database->getRows("SELECT DISTINCT bookingdate FROM receipt_entry"); ?>
<?php $data = $database->getRows("select surveyor_name, count(DISTINCT receipt_no) As total,bookingdate from receipt_entry group by surveyor_name,bookingdate"); ?>
<table border="1px solid #666" style="text-align:center;" cellpadding='0' cellspacing='0'>
<thead>
<tr>
<th>Surveyor Name</th>
<?php foreach($book as $date):?>
<?php $dates[] = $date['bookingdate'];?>
<th><?php echo $date['bookingdate'];?></th>
<?php endforeach;?>
</tr>
</thead>
<tbody>
<?php $j = 0;?>
<?php foreach($data as $key => $value):?>
<?php $names[] = $value['surveyor_name'];?>
<?php $uniValues = array_count_values($names);?>
<?php if($uniValues[$value['surveyor_name']] == 1):?>
<tr>
<td>
<?php echo $value['surveyor_name'];?>
</td>
<?php $i = 0;?>
<?php foreach($book as $date):?>
<td align="center">
<?php if($names[$i] == $data[$j]['surveyor_name']):?>
<?php echo $data[$j]['total'];?>
<?php else:?>
<?php foreach($data as $dat):?>
<?php if($dat['surveyor_name'] == $names[$j] && $dates[$i] == $dat['bookingdate']):?>
<?php echo $dat['total'];?>
<?php endif;?>
<?php endforeach;?>
<?php endif;?>
</td>
<?php ++$i;?>
<?php endforeach;?>
</tr>
<?php endif;?>
<?php ++$j;?>
<?php endforeach;?>
</tbody>
</table>
答案 0 :(得分:0)
假设您的数据格式如下:
$book = array(
array('bookingdate' => '20-03-2015'),
array('bookingdate' => '21-03-2015'),
array('bookingdate' => '22-03-2015'),
array('bookingdate' => '23-03-2015'),
);
$data = array(
array(
'surveyor_name' => 'raj',
'total' => '2',
'bookingdate' => '20-03-2015'
),
array(
'surveyor_name' => 'angel',
'total' => '1',
'bookingdate' => '21-03-2015'
),
array(
'surveyor_name' => 'raj',
'total' => '1',
'bookingdate' => '22-03-2015'
),
array(
'surveyor_name' => 'test',
'total' => '3',
'bookingdate' => '23-03-2015'
),
array(
'surveyor_name' => 'test',
'total' => '2',
'bookingdate' => '20-03-2015'
),
array(
'surveyor_name' => 'test',
'total' => '3',
'bookingdate' => '22-03-2015'
),
);
然后,让您的HTML <table>
布局如下:
<?php
// total calculation
$totalSurveyor = $totalBook = array();
foreach($data as $key => $value) {
$totalSurveyor[$value['surveyor_name']] += $value['total'];
$totalBook[$value['bookingdate']] += $data[$key]['total'];
}
?>
<table border="1px solid #666" style="text-align:center;" cellpadding='0' cellspacing='0'>
<thead>
<tr>
<th>Surveyor Name</th>
<?php foreach($book as $date):?>
<?php $dates[] = $date['bookingdate'];?>
<th><?php echo $date['bookingdate'];?></th>
<?php endforeach;?>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php $j = 0;?>
<?php foreach($data as $key => $value):?>
<?php $names[] = $value['surveyor_name'];?>
<?php $uniValues = array_count_values($names);?>
<?php if($uniValues[$value['surveyor_name']] == 1):?>
<tr>
<td>
<?php echo $value['surveyor_name'];?>
</td>
<?php $i = 0;?>
<?php foreach($book as $date):?>
<td align="center">
<?php if(array_key_exists($i, $names) && $names[$i] == $data[$j]['surveyor_name']):?>
<?php echo $data[$j]['total'];?>
<?php else:?>
<?php foreach($data as $dat):?>
<?php if($dat['surveyor_name'] == $names[$j] && $dates[$i] == $dat['bookingdate']):?>
<?php echo $dat['total'];?>
<?php endif;?>
<?php endforeach;?>
<?php endif;?>
</td>
<?php ++$i;?>
<?php endforeach;?>
<td>
<strong><?php echo $totalSurveyor[$value['surveyor_name']];?></strong>
</td>
</tr>
<?php endif;?>
<?php ++$j;?>
<?php endforeach;?>
<tr>
<td><strong>Total</strong></td>
<?php foreach($book as $value):?>
<td>
<strong><?php echo $totalBook[$value['bookingdate']];?></strong>
</td>
<?php endforeach;?>
</tr>
</tbody>
</table>
所以你可以获得预期的输出: