user3631086
user3631086

Reputation:

How to get a sum of the value which I get in the if condition

First of all, I am new in this programming area, so I don't know how to do this?

I have a database table video_master in that I have ID, REC_ID, VIDEO_DATE, VIDEO_COUNT, IS_REDIM fields. Now I want a count of that amount. I use if and get an amount as video, but now I don't know how to take the sum of that amount, i.e. total amount. Here, I am providing what I have done.

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

I get a $amount as a video, but now I want to calculate the total of that $amount. amount field is not in the database. I get the amount from video_count.

UPDATED PROBLEM

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

Upvotes: 2

Views: 114

Answers (3)

user3631086
user3631086

Reputation:

Final The code which work for me.

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


            ?>

Upvotes: 0

Himanshu
Himanshu

Reputation: 281

Try This:-

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

Upvotes: 1

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

In each row, you have to add the current amount to previous amount.

May be try this.

$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.

After your loop ends,

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

EDIT

One more final thing you have to do is,

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
 } 

Upvotes: 1

Related Questions