Search code examples
phpmysqlsumcumulative-sumsubtotal

sum of $row based on specific time field rather than just total


This code works just fine to display a list of reservations for a given day and sums the guest and table counts just fine for the entire day. Sums all reservations returned.

What I am trying to do or figure out is a way to get totals based on specific time intervals. For example how many guests and tables at 8:00, 9:00, 10:00, etc....

I can see where the sums are calculated, but need help adding a way to add a variable to look at the reservation_time and sum by hour rather than just daily total.

        $tablesum ++;
        $guestsum += $row->reservation_pax;
<?php echo $guestsum;?>&nbsp;&nbsp;<?php echo _guest_summary;?>

The full code that pulls in the data and then sums it up in total:

<?php
    // Clear reservation variable
    $reservations ='';

    if ($_SESSION['page'] == 1) {
        $reservations = querySQL('all_reservations');
    }else{
        $reservations = querySQL('reservations');
    }

    // reset total counters
    $tablesum = 0;
    $guestsum = 0;

    if ($reservations) {

        //start printing out reservation grid
        foreach($reservations as $row) {
            // reservation ID
            $id = $row->reservation_id;
            $_SESSION['reservation_guest_name'] = $row->reservation_guest_name;
            // check if reservation is tautologous
            $tautologous = querySQL('tautologous');

        echo "<tr id='res-".$id."'>";
        echo "<td";
        // daylight coloring
        if ($row->reservation_time > $daylight_evening){
            echo " class='evening noprint'";
        }else if ($row->reservation_time > $daylight_noon){
            echo " class='afternoon noprint'";
        }else if ($row->reservation_time < $daylight_noon){
            echo " class='morning noprint'";
        }

        echo " style='width:10px !important; padding:0px;'>&nbsp;</td>";
        echo "<td id='tb_time'";
        // reservation after maitre message
        if ($row->reservation_timestamp > $maitre['maitre_timestamp'] && $maitre['maitre_comment_day']!='') {
            echo " class='tautologous' title='"._sentence_13."' ";
        }
        echo ">";
        echo "<strong>".formatTime($row->reservation_time,$general['timeformat'])."</strong></td>";
        echo "<td id='tb_pax'><strong class='big'>".$row->reservation_pax."</strong>&nbsp;&nbsp;<span class='noprint'>";
        $tablesum ++;
        $guestsum += $row->reservation_pax;
        }
    }
    ?>

    <?php echo $guestsum;?>&nbsp;&nbsp;<?php echo _guest_summary;?>

Ok I am getting close: was able to get it to total and display... though I need to narrow by one more parameter. That is giving me the total guest count (reservation_pax) for all reservations. I need to do it by the given date or session. The page shows only reservation for the date selected, but script counts all days with reservations at 8:00.

Just need help to narrow by session page is already creating for specific date being viewed.

$result = mysql_query("SELECT SUM(reservation_pax) FROM reservations WHERE reservation_time = '8:00'"); 
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}

while($row = mysql_fetch_array($result))
{

   echo $row['SUM(reservation_pax)'];
}

Solution

  • $result = mysql_query("SELECT SUM(reservation_pax) FROM reservations WHERE reservation_time = '8:00:00' AND reservation_date = '2014-10-27' AND reservation_hidden ='0'") ; 
    if($result === FALSE) {
        die(mysql_error()); // TODO: better error handling
    }
    
    while($row = mysql_fetch_array($result))
    {
       echo $row['SUM(reservation_pax)'];
    }
    
    ?>