汇总特定学生支付的所有金额

时间:2014-11-28 17:14:56

标签: php mysql

我有一个名为:

的表名
Fee_payment
payment_id  student_id  name    class_id  term         year     amount creation_timestamp
         1           7  john    JHS2A     second_term  20014    34     1416610800
         2           5  Grace   JHS2B     second_term  20014    34     1416610856
         3           6  john    JHS2A     second_term  20014    56     1416610800

如果用户点击费用记录,我想总结约翰支付的总金额。我只能获取当前支付的金额,但无法将学生支付的所有金额相加。

我正在寻找的是,例如,如果john出现两次,我应该能够根据term和creation_timestamp将john的付款加起来。

我尝试过:

 <div class="box-content">
        <?php foreach($edit_data as $row):?>


        <div class="pull-left">

            <span style="font-size:20px;font-weight:100;">

            <?php echo get_phrase('payment_to');?>
            <img width="50" height="30" src="<?php echo base_url();?>uploads/logo.png" style="max-height:100px;margin:20px 0px;" />
            </span>
            <br />
            <?php echo $system_name;?>
            <br />
            <?php echo $this->db->get_where('settings' , array('type'=>'address'))->row()->description;?>
        </div>
        <div class="pull-right">
            <span style="font-size:20px;font-weight:100;">
                <?php echo get_phrase('credited_account:');?>
            </span>
            <br />
                <?php echo $this->db->get_where('student' , array('student_id'=>$row['student_id']))->row()->name;?>
            <br />
                <?php echo get_phrase('student_id');?> : 
                <?php echo "FAVECSID0000", $this->db->get_where('student' , array('student_id'=>$row['student_id']))->row()->student_id;?>
            <br />
                <?php echo get_phrase('class');?> : 
                <?php 
                $class_id   =   $this->db->get_where('student' , array('student_id'=>$row['student_id']))->row()->class_id;
                echo $this->db->get_where('class' , array('class_id'=>$class_id))->row()->name;
                ?>
        </div>
        <div style="clear:both;"></div>
        <hr />
        <table width="100%" background="http://localhost/schoolmanager/uploads/backlogos.png">
            <tr style="background-color:#7087A3; color:#fff; padding:5px;">
                <td style="padding:5px;"><?php echo get_phrase('payment_details');?></td>
                <td width="30%" style="padding:5px;">
                    <div class="pull-right">
                        <?php echo get_phrase('amount');?>
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    <span style="font-size:20px;font-weight:100;">
                        <?php echo get_phrase('payment_made');?>
                    </span>
                    <br />

                </td>
                <td width="30%" style="padding:5px;">
                    <div class="pull-right">
                        <span style="font-size:20px;font-weight:100;">
                        <?php echo "Gh", $row['amount'];?>  
                        </span>
                    </div>
                </td>
            </tr>

            <tr>

            <tr>
                <td>
                    <span style="font-size:20px;font-weight:100;">
                        <?php echo get_phrase('balance');?>
                    </span>
                    <br />
                    <?php echo $row['description'];?>
                    <?php 
                                $exams = $this->db->get_where('fee_payment', array('student_id'=>$row['student_id']))->result_array();
                                foreach($exams as $row):
                                ?>
                                <?php 

                                $ttpaid = sum($row['amount']);
                                echo $ttpaid;?>
                                 <?php
                                endforeach;
                                ?>
                </td>
                <td width="30%" style="padding:5px;">
                    <div class="pull-right">
                        <span style="font-size:20px;font-weight:100;">
                <?php 

                $tuition    =   $this->db->get_where('student' , array('student_id'=>$row['student_id']))->row()->tuition_fee;
                $status =   $this->db->get_where('student' , array('student_id'=>$row['student_id']))->row()->status_fee;
                $total_fees = $tuition + $status;
                $balance_fee = $total_fees - $row['amount'];
                echo "Gh", $balance_fee;
                ?>          
                        </span>
                    </div>
                </td>
            </tr>

            <tr>

                <td></td>
                <td width="30%" style="padding:5px;">
                    <div class="pull-right">
                    <hr />
                    <?php echo get_phrase('status');?> : <?php echo $row['status'];?>
                    <br />
                    <?php echo get_phrase('receipt_no:');?> : <?php echo "FAVECSFP0000",$row['payment_id'];?>
                    <br />
                    <?php echo get_phrase('date');?> : <?php echo date('m/d/Y', $row['creation_timestamp']);?>
                    </div>
                </td>
            </tr>
      </table>
<br />
<br />


        <?php endforeach;?>
    </div>
</div>

输出:

Payment To
FAIRVIEW EDUCATIONAL CENTER
P O BOX 126
Credited Account:
john
Student Id : FAVECSID00007
Class : JHS2A
Payment Details     
Amount
Payment Made Gh34
Balance 

2 个答案:

答案 0 :(得分:2)

MySQL的优点在于,与真正的纸张不同,没有理由将自己局限于单个表格。在这种情况下,您有Fee_payment表格,但似乎最好添加第二个表格,例如Students

在这种情况下,您可能会有一个如下所示的表:

Students
id   name            account_created
 1   John            1416610800
 2   Grace           1416609200

因此,在Fee_payment表中,您不再需要存储学生的姓名。您只需使用student_id列来引用id表中的Students列。然后,当John登录时(或者有人根据Students表中存在的行查找John),您需要做的就是在Fee_payment表中搜索{{1}与John的匹配。

在这种情况下,查询将类似于:

student_id

这将返回John支付的总金额,并准确地为您提供所需内容。

答案 1 :(得分:1)

只需将SQL查询执行到特定名称的总和。

select name,sum(amount) from table_name where name like 'john'