如何获得if条件中得到的值的总和

时间:2015-10-30 05:14:19

标签: php mysql if-statement sum

首先,我是这个编程领域的新手,所以我不知道该怎么做?

我有一个数据库表ID, REC_ID, VIDEO_DATE, VIDEO_COUNT, IS_REDIM,我有<?php // If user not logged in then show login message if (!isset($_SESSION['userid'])) { ?> <div class="message"> To access this page, you must be logged.<br /> <a href="connexion.php">Log in</a> </div> <?php } else { //Video count values of the database if(isset($_REQUEST['date1']) && isset($_REQUEST['date2'])) { $dnn = mysqli_query($conn, 'select video_date,video_count from video_master where rec_id="'.$_SESSION['userid'].'" and video_date >= "' . $_REQUEST['date1'] . '" and video_date <= "' . $_REQUEST['date2'] . '"'); } else { $dnn = mysqli_query($conn, 'select id,video_date,video_count,is_redim from video_master where rec_id="'.$_SESSION['userid'].'"'); } if(mysqli_num_rows($dnn)>0) { ?> <table> <thead> <tr> <th>ID</th> <th>Date</th> <th>Video Count</th> <th>Total Amount</th> <th>Is Redim ?</th> </tr> </thead> <?php while($rowData = mysqli_fetch_array($dnn)) { //$video_date = date("Y-m-d","d-m-Y",$rowData['video_date']); $video_date = $rowData['video_date']; $video_count = $rowData['video_count']; ?> <tbody> <tr> <td><?php echo $rowData['id']; ?></td> <td><?php print($video_date); ?></td> <td><?php print($video_count); ?></td> <td><?php //echo $rowData['video_count']; if($rowData['video_count'] < 250){ $amount1 = $rowData['video_count']*0; echo $amount1; } else{ if($rowData['video_count'] <= 500){ $amount1 = $rowData['video_count']*0.25; echo $amount1; } else { if($rowData['video_count'] <= 750){ $amount1 = $rowData['video_count']*0.50; echo $amount1; } else { if($rowData['video_count'] <= 1000){ $amount1 = $rowData['video_count']*0.75; echo $amount1; } else { if($rowData['video_count'] <= 1250){ $amount1 = $rowData['video_count']*1; echo $amount1; } else { if($rowData['video_count'] < 1500){ $amount1 = $rowData['video_count']*1.50; echo $amount1; } else { } } } } } } ?></td> <td><?php echo $rowData['is_redim']; ?></td> </tr> </tbody> <?php } ?> </table> <?php } ?> <?php } ?> 个字段。 现在我想要一个这个数量的计数。我使用if并获得一定数量的视频,但现在我不知道如何获取该金额的总和,即总金额。在这里,我提供的是我所做的。

$amount

我获得$amount视频,但现在我想计算amount的总数。 <?php $total_amt = 0; // If user not logged in then show login message if (!isset($_SESSION['userid'])) { ?><div class="message"> To access this page, you must be logged.<br /> <a href="connexion.php">Log in</a> </div><?php } else{ //Video count values of the database if(isset($_REQUEST['date1']) && isset($_REQUEST['date2'])){ $dnn = mysqli_query($conn, 'select video_date,video_count from video_master where rec_id="'.$_SESSION['userid'].'" and video_date >= "' . $_REQUEST['date1'] . '" and video_date <= "' . $_REQUEST['date2'] . '"'); } else{ $dnn = mysqli_query($conn, 'select id,video_date,video_count,is_redim from video_master where rec_id="'.$_SESSION['userid'].'"'); } if(mysqli_num_rows($dnn)>0) { ?><table> <thead> <tr> <th>ID</th> <th>Date</th> <th>Video Count</th> <th>Total Amount</th> <th>Is Redim ?</th> </tr></thead><?php while($rowData = mysqli_fetch_array($dnn)){ //$video_date = date("Y-m-d","d-m-Y",$rowData['video_date']); $video_date = $rowData['video_date']; $video_count = $rowData['video_count']; ?><tbody><tr> <td><?php echo $rowData['id']; ?></td> <td><?php print($video_date); ?></td> <td><?php print($video_count); ?></td> <td><?php //echo $rowData['video_count']; if($rowData['video_count'] < 250){ $amount1 = $rowData['video_count']*0; echo $amount1; } else{ if($rowData['video_count'] <= 500){ $amount1 = $rowData['video_count']*0.25; echo $amount1; } else { if($rowData['video_count'] <= 750){ $amount1 = $rowData['video_count']*0.50; echo $amount1; } else { if($rowData['video_count'] <= 1000){ $amount1 = $rowData['video_count']*0.75; echo $amount1; } else { if($rowData['video_count'] <= 1250){ $amount1 = $rowData['video_count']*1; echo $amount1; } else { if($rowData['video_count'] < 1500){ $amount1 = $rowData['video_count']*1.50; echo $amount1; } else { } } } } } } $total_amt += $amount1; echo $total_amt; ?></td> <td><?php echo $rowData['is_redim']; ?></td> </tr></tbody><?php } ?></table><?php } ?> <?php } ?> 字段不在数据库中。我从video_count获得金额。

更新问题

b

3 个答案:

答案 0 :(得分:1)

在每一行中,您必须将当前金额添加到之前的金额。

可以试试这个。

$total_amt = 0;// declare outside the loop.

$total_amt += $amount1;// inside the condition where you are echoing the amount or just outside the main if else block.

循环结束后,

<?php echo $total_amt; ?>// will echo the total amount.


if($rowData['video_count'] < 250){
       $amount1 = $rowData['video_count']*0;
       echo $amount1;            
}  else{
         ...
         ...               
} 

 $total_amt += $amount1;// keep here

修改

你要做的最后一件事是,

if($rowData['video_count'] < 1500){
     $amount1 = $rowData['video_count']*1.50;
     echo $amount1;            
 } else {
     $amount1 = 0;// declare the variable here... last if else() in side main else
 } 

答案 1 :(得分:1)

试试这个: -

<?php

        // If user not logged in then show login message
        if (!isset($_SESSION['userid'])) {

            ?><div class="message">
                To access this page, you must be logged.<br />
                <a href="connexion.php">Log in</a>
            </div><?php
        }
        else{

//Video count values of the database
if(isset($_REQUEST['date1']) && isset($_REQUEST['date2'])){
$dnn = mysqli_query($conn, 'select video_date,video_count from video_master     where  rec_id="'.$_SESSION['userid'].'" and video_date >= "' .     $_REQUEST['date1'] . '" and video_date <= "' . $_REQUEST['date2'] . '"');
}
else{ 
$dnn = mysqli_query($conn, 'select id,video_date,video_count,is_redim from video_master where  rec_id="'.$_SESSION['userid'].'"');
}
if(mysqli_num_rows($dnn)>0)
{
?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Date</th>
<th>Video Count</th>
<th>Total Amount</th>
<th>Is Redim ?</th>
</tr></thead><?php
while($rowData = mysqli_fetch_array($dnn)){
//$video_date = date("Y-m-d","d-m-Y",$rowData['video_date']);
$video_date = $rowData['video_date'];
$video_count = $rowData['video_count'];
?>
<tbody>
<tr>
<td><?php echo $rowData['id']; ?></td>
<td><?php print($video_date); ?></td>
<td><?php print($video_count); ?></td>
<td><?php //echo $rowData['video_count'];
$total_amt=0;
if($rowData['video_count'] < 250){
$amount1 = $rowData['video_count']*0;
$total_amt = $total_amt + $amount1;

} 
else{
if($rowData['video_count'] <= 500){
$amount1 = $rowData['video_count']*0.25;
$total_amt = $total_amt + $amount1;

}
else
{
if($rowData['video_count'] <= 750){
$amount1 = $rowData['video_count']*0.50;
$total_amt = $total_amt + $amount1;

}
else
{
if($rowData['video_count'] <= 1000){
$amount1 = $rowData['video_count']*0.75;
$total_amt = $total_amt + $amount1;

} else
{
if($rowData['video_count'] <= 1250){
$amount1 = $rowData['video_count']*1;
$total_amt = $total_amt + $amount1;

} else
{
if($rowData['video_count'] < 1500){
$amount1 = $rowData['video_count']*1.50;
$total_amt = $total_amt + $amount1;

} else
{
}        
}        
}
}                      
} 
} 
?>
</td>
<td><?php echo $rowData['is_redim'];  ?></td>
</tr></tbody><?php }
  echo $total_amt;  ?></table><?php
}   
?>

答案 2 :(得分:0)

最终代码对我有用。

         <?php



            // $totalamount1 = mysqli_fetch_array(mysqli_query($conn, 'SELECT SUM(video_count) as total From video_master Where is_redim="N" AND rec_id="'.$_SESSION['userid'].'"' ));


                $amount_count = mysqli_query($conn, 'select video_count from video_master where is_redim="N" AND  rec_id="'.$_SESSION['userid'].'"');

                 $total_amtt=0;
                 //$amount = 0;

                if(mysqli_num_rows($amount_count)>0)
            {
                while($rowData = mysqli_fetch_array($amount_count))
                {


                    // $amount = 0;

                                            if($rowData['video_count'] < 250){
                                  $amount1 = $rowData['video_count']*0;
                                  //echo $amount1;
                                  $total_amtt = $total_amtt + $amount1;
                                   // echo $total_amtt;             
                        } 
                        else{
                                    if($rowData['video_count'] <= 500){
                                    $amount1 = $rowData['video_count']*0.25;
                                    //echo $amount1;            
                                    $total_amtt = $total_amtt + $amount1;
                                   // echo $total_amtt;  
                                    } else
                                    {
                                        if($rowData['video_count'] <= 750){
                                        $amount1 = $rowData['video_count']*0.50;
                                        //echo $amount1;            
                                        $total_amtt = $total_amtt + $amount1;
                                    //echo $total_amtt;  
                                        } else
                                        {
                                            if($rowData['video_count'] <= 1000){
                                            $amount1 = $rowData['video_count']*0.75;
                                            //echo $amount1;            
                                            $total_amtt = $total_amtt + $amount1;
                                   // echo $total_amtt;  
                                            } else
                                            {
                                                    if($rowData['video_count'] <= 1250){
                                                    $amount1 = $rowData['video_count']*1;
                                                    //echo $amount1;            
                                                    $total_amtt = $total_amtt + $amount1;
                                    //echo $total_amtt;  
                                                    } else
                                                    {

                                                        if($rowData['video_count'] < 1500){
                                                        $amount1 = $rowData['video_count']*1.50;
                                                        //echo $amount1; 
                                                        $total_amtt = $total_amtt + $amount1;


                                                        } else
                                                        {
                                                               //$amount1 = 0;      


                                                        }  



                                                    }



                                            }


                                        }                      



                                    } 



                            } 

                }
               echo $total_amtt;
            }


            ?>