用于在报告中显示datewise total的php代码

时间:2015-03-26 13:53:13

标签: php

我在此处发布的更新代码包含我的更改...

plz建议我是正确的..以下代码..

我使用我的php查询更改和更新您的代码,所有..plz建议需要更改以下代码....根据您..

如果我错了,请帮我纠正这个......

请告诉我在哪里放我的php查询。

早期代码

<?php   
$book = $database->getRows("SELECT DISTINCT bookingdate FROM receipt_entry");       
?>

<table border="1px solid #666" style="text-align:center;" cellpadding='0' cellspacing='0'>

    <tr>
    <th>Surveyor Name</th>
    <?php   foreach($book as $dt)   { ?>
    <td><?php echo $dt['bookingdate']; ?></td>  
    </tr>       
    <?php   
$book = $dt['bookingdate'];
$data = $database->getRows("select surveyor_name,bookingdate, count(DISTINCT receipt_no) As total from receipt_entry where bookingdate = '$book' group by surveyor_name,bookingdate");
?>
<?php   foreach($data as $db)   { ?>
    <tr>
    <td><?php echo $db['surveyor_name']; ?></td>        
    <td><?php echo $db['total']; ?></td>

    </tr>
<?php } }?>


    </table>



<table border=1>
    <thead>
        <tr>
            <th>Surveyor Name</th>
            <?php $book = $database->getRows("SELECT DISTINCT bookingdate FROM receipt_entry"); ?>
            <?php foreach($book as $date):?>
                <?php $dates[] = $date['bookingdate'];?>
                <th><?php echo $date['bookingdate'];?></th>
            <?php endforeach;?>
        </tr>
    </thead>
    <tbody>
    <?php $data = $database->getRows("select surveyor_name, count(DISTINCT receipt_no) As total,bookingdate from receipt_entry group by surveyor_name,bookingdate"); ?>
        <?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>

预期输出

     surveyor name  19-03-2015    20-03-2015   24-03-2015
      angel                                        1
      raj             2            
      sudnyesh                       1


   array

0 =&gt;     排列       'bookingdate'=&gt;字符串'19 -03-2015'(长度= 10)   1 =&gt;     排列       'bookingdate'=&gt;字符串'20 -03-2015'(长度= 10)   2 =&gt;     排列       'bookingdate'=&gt;字符串'24 -03-2015'(长度= 10) 排列   0 =&gt;     排列       'surveyor_name'=&gt;字符串'天使'(长度= 5)       'total'=&gt;字符串'1'(长度= 1)       'bookingdate'=&gt;字符串'24 -03-2015'(长度= 10)   1 =&gt;     排列       'surveyor_name'=&gt;字符串'raj'(长度= 3)       'total'=&gt;字符串'2'(长度= 1)       'bookingdate'=&gt;字符串'19 -03-2015'(长度= 10)   2 =&gt;     排列       'surveyor_name'=&gt;字符串'sudnyesh'(长度= 8)       'total'=&gt;字符串'1'(长度= 1)       'bookingdate'=&gt;字符串'20 -03-2015'(长度= 10)

数据存在于我的数据库中,如下所示

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

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

1 个答案:

答案 0 :(得分:1)

假设您以该格式提取数据(根据您的问题):

$book = array(
    array('bookingdate' => '20-03-2015'),
    array('bookingdate' => '21-03-2015'),
    array('bookingdate' => '22-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'
    ),
);

然后,您可以像这样构建HTML table

<?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>

并将您的数据显示为:

enter image description here