How to update value of a column based on sum of previous id value of similar date and position, and subtract from different position

This is generated report sorted by date in a table..

This is my current bootstrap table with data from mysql dbase.
I havent done any codes to try the image 2 because I dont really know how to do it. I just know how to display the basic data to tables from mysql database.

Current Output based on my given codes

but this second image is the output or format I am expected to display.

Expected and Sample Output

this is what I wanted to display. base on the attached image.

  1. is the second image possible in PHP MYSQL ?
    given those values from my mysql dbase .. how can I get the output like in the second image? with PHP.

Image 2 Description:

  • the same dates with different values from LEFT Team and RIGHT Team should regroup to each given column and align in the same row.

Example october 18 2022. – LEFT Team has 4 different entries total with of 245 points.
but in the same date there is no entry from RIGHT Team

and so the Balance From left will show 245 and balance right 0 .

  • But since on the next day 10/19/2019
  • there are 3 entries from left total of 130
  • and entries from right team total of 115.
  • 245 + 130 = 375 points
  • the 375 must substract the points from right team who got lower points in totality..
  • 375 – 115 = 260 balance left and Balance right will remain 0 .
  • and same goes to the next dates and so On .

Here’s my Current Code :

$queryupdate=mysqli_query($conn, "SELECT * FROM `tbl_generate_report_all` WHERE user_id = '$currentid'");
    $rowupdate=mysqli_num_rows($queryupdate);
    if($rowupdate>0){
        while($forupdate=mysqli_fetch_array($queryupdate)){

            $theid = $forupdate['id'];
            $thepos = $forupdate['position'];
            $thepoints = $forupdate['points'];

            echo  $theid.' ';
            echo  $thepos.' ';
            echo  $thepoints.' ';

     
        }
    }

    
 
    if(ISSET($_POST['search'])){

        
        $date1 = date("Y-m-d", strtotime($_POST['date1']));
        $date2 = date("Y-m-d", strtotime($_POST['date2']));

        $sql_repsummary = mysqli_query($conn, "SELECT sum(points)as totaldr  FROM `tbl_generate_report_all` WHERE direct_refer = '$currentid' and user_id = '$currentid' and date(`date_activated`) BETWEEN '$date1' AND '$date2'") or die(mysqli_error());
        $row_user_details = mysqli_fetch_assoc($sql_repsummary);
        $thetotaltotaldr = $row_user_details['totaldr'] * 10;


        $query=mysqli_query($conn, "SELECT * FROM `tbl_generate_report_all` WHERE user_id = '$currentid' and date(`date_activated`) 
        BETWEEN '$date1' AND '$date2'") or die(mysqli_error());
        $row=mysqli_num_rows($query);
        if($row>0){
            while($fetch=mysqli_fetch_array($query)){
?>
    <tr>
        <td><?php echo $fetch['date_activated']?>
    </td>
        <td><?php

            $acq_pkg =$fetch['pkg'];

            if ($acq_pkg == 0) {    
                $pkge_column = 'Bronze-15';
                $pkg_value = '15';
                }elseif ($acq_pkg == 1) {
                $pkge_column = 'Silver-30';
                $pkg_value = '30';
                }elseif ($acq_pkg == 2) {
                $pkge_column = 'Gold-60';
                $pkg_value = '60';
                }elseif ($acq_pkg == 3) {
                $pkge_column = 'Platinum-100';
                  $pkg_value = '100';
            }
              
            
            $thepos =$fetch['position'];
            if ($thepos == 'L') {
             echo  $fetch['dl_fullname'] .' - <h5 style="font-size:11px;">'.$pkge_column .'</h5>';
            }else {
                echo '-';
            }



             



              ?></td>
        <td><?php 

$acq_pkg =$fetch['pkg'];

if ($acq_pkg == 0) {    
    $pkge_column = 'Bronze-15';
    $pkg_value = '15';
      }elseif ($acq_pkg == 1) {
    $pkge_column = 'Silver-30';
    $pkg_value = '30';
      }elseif ($acq_pkg == 2) {
    $pkge_column = 'Gold-60';
    $pkg_value = '60';
      }elseif ($acq_pkg == 3) {
      $pkge_column = 'Platinum-100';
      $pkg_value = '100';
}
  

$thepos =$fetch['position'];
if ($thepos == 'R') {
 echo  $fetch['dl_fullname'] .' - <h6 style="font-size:11px;">'.$pkge_column .'</h6>';
}else {
    echo '-';
}



 
        ?></td>
        <td><?php
        if ($thepos == 'L') {
            echo $pkg_value;
        }else {
            echo '-';
        }
            ?></td>

        <td><?php 
        if ($thepos == 'R') {
            echo $pkg_value;
        }else {
            echo '-';
        }
        
        ?></td>
        <td><?php echo $fetch['gc']?></td>
        <td>
            <?php 
            $drid = $fetch['direct_refer'];
            if ($drid == $currentid) {

                echo $pkg_value * 10 ;
            }else {
                echo '-';
            }
            ?>
        
        
        </td>
        <td><?php echo $fetch['total_sm_income']?></td>
    </tr>

     
<?php
            }
        }else{
            echo'
            <tr>
                <td colspan = "4"><center>Record Not Found</center></td>
            </tr>';
        }


    }
?>