PHP如何通过while循环进行Whileloop

时间:2018-12-06 21:15:12

标签: php

我是PHP的新手,在下面说明了我的问题

我已经使用while循环来获取在瞳孔表中具有相同父级的瞳孔

我的问题是如何使用上面循环的class_id(学生表)中的结果来循环并从费用表中的class_id中获取结果

查看图片以获取清晰的插图

Apple docs

    <?php 

    $sql3= "select * from pupil where parent=$parent_get";
    $runsql3=mysqli_query($mycon,$sql3) or die ("no connection");


    while($getpupils3=mysqli_fetch_array($runsql3)){
    $sid=$getpupils3["id"];
    $sname=$getpupils3["name"];
    $sclass=$getpupils3["class_id"];

    $sql = "Select * from class where id = $sclass";
        $runquery = $mycon->query($sql);
        if($runquery->num_rows > 0){
            $fetchdata=mysqli_fetch_array($runquery);
            $c_name=$fetchdata['name'];
            $c_title=$fetchdata['title'];
            $class_info=$c_name.' '.$c_title;
        }

    $sql4= "select * from fee where class=$sclass AND term=$term_get AND session=$session_get";
    $runsql4=mysqli_query($mycon,$sql4) or die ("no connection");
    while($getpupils4=mysqli_fetch_array($runsql4)){    
        $fee_title=$getpupils4["fee_title"];
        $fee_amount=$getpupils4["fee_amount"];
        $fee_des=$getpupils4["fee_des"];

        echo'
                            <div class="col-lg-4">
                                <div class="card m-b-30 card-body">
                                    <h4 class="card-title font-20 mt-0">'.$sname.'</h4>
                                    <p class="card-text">'.$class_info.'</p>
                                    <p class="card-text">'.$fee_title.': '.$fee_amount.'</p>
                                    <a href="#" class="btn btn-primary waves-effect waves-light">Edit</a>
                                </div>
                            </div>
  ';}}?>

1 个答案:

答案 0 :(得分:0)

您只需要在while循环之前为学生回显html的第一部分以获取费用,而在while循环之后回显html的底部。

<?php 
$sql3= "select * from pupil where parent=$parent_get";
$runsql3=mysqli_query($mycon,$sql3) or die ("no connection");
while($getpupils3=mysqli_fetch_array($runsql3)){
    $sid=$getpupils3["id"];
    $sname=$getpupils3["name"];
    $sclass=$getpupils3["class_id"];

    $sql = "Select * from class where id = $sclass";
    $runquery = $mycon->query($sql);
    if($runquery->num_rows > 0){
        $fetchdata=mysqli_fetch_array($runquery);
        $c_name=$fetchdata['name'];
        $c_title=$fetchdata['title'];
        $class_info=$c_name.' '.$c_title;
    }
    // output the pupil details before the fee while loop
    echo'<div class="col-lg-4">
        <div class="card m-b-30 card-body">
            <h4 class="card-title font-20 mt-0">'.$sname.'</h4>
            <p class="card-text">'.$class_info.'</p>';

    $sql4= "select * from fee where class=$sclass AND term=$term_get AND session=$session_get";
    $runsql4=mysqli_query($mycon,$sql4) or die ("no connection");
    $fee_total = 0;
    while($getpupils4=mysqli_fetch_array($runsql4)){    
        $fee_title=$getpupils4["fee_title"];
        $fee_amount=$getpupils4["fee_amount"];
        $fee_des=$getpupils4["fee_des"];
        // output the fee detail
        echo '<p class="card-text">'.$fee_title.': '.$fee_amount.'</p>';
        // increment the fee total
        $fee_total += $fee_amount;
    }
    // output the fee total
    echo '<p class="card-text">Total '.$fee_total.'</p>';
    // output the bottom of the box after the fee while loop
    echo '<a href="#" class="btn btn-primary waves-effect waves-light">Edit</a>
        </div>
    </div>';
}