Jump to content

? how to total up "for each" results


turpentyne

Recommended Posts

I got this script, and it didn't dawn on me to total up the results. What I have below runs through each class, checks the database for what discount is applied. Then the math is done and I get a total. at the end. But I'm not quite sure how to do that - down at the bottom of the script below.

 

<?php
/* set the cache limiter to 'private' */

session_cache_limiter('private');
$cache_limiter = session_cache_limiter();

/* set the cache expire to 7 minutes */
session_cache_expire(7);
$cache_expire = session_cache_expire();

/* start the session */
// connect to database 
include("db.php"); 

$discount = mysql_escape_string($_POST['discount']);
$donation = mysql_escape_string($_POST['donation']);

/*here, is the total cost from all classes selected on previous page */
$total = mysql_escape_string($_POST['total']); 
    

/* start insert of new math */
$discount_code = mysql_escape_string($_POST['discount']); //The discount code, if entered.
    //$selected_classes = array(217 => 1, 215 => 2); //How many of each class was selected. class_id => quantity
    $selected_classes = $_SESSION['s_filtered'];

$grand_total = "";
    // Built an array with information about each class, and what discount it can get.
    $classes = array();
    foreach ($selected_classes as $class => $quantity)
    { $testquery = "SELECT * FROM `active_discounts` LEFT JOIN tbl_discount ON active_discounts.disc_id = tbl_discount.discount_id WHERE `code` = '{$discount_code}' AND `class_id` = '{$class}'";
    
        $result = mysql_query($testquery);
    
        if (mysql_num_rows($result) > 0)
            {
            
            $discount_row = mysql_fetch_assoc($result);
            $discount_type = $discount_row['discount_type'];
            $discount_value = $discount_row['discount_amount'];
            $discount_name = $discount_row['discount_name'];
            $active_inactive = $discount_row['active_inactive'];

            $result = mysql_query("SELECT * FROM `tbl_workshops` WHERE `workshop_id` = '{$class}'");
            $class_row = mysql_fetch_assoc($result);
            $class_name = $class_row['workshop_title'];
            $class_cost = $class_row['workshop_price'];
            $classes[$class]['workshop_title'] = $class_name;
            $classes[$class]['workshop_price'] = $class_cost;
    $classes[$class]['active_inactive'] = $active_inactive;
            
            $classes[$class]['discount_name'] = $discount_name;
            $classes[$class]['discount_type'] = $discount_type;
            
            $classes[$class]['discount_value'] = $discount_value;
            $classes[$class]['quantity'] = $quantity;
            }
            // The section below was supposed to be for when there was no discount. (nothing in the active_discounts table)
        else {
            $discount_value = "0";
             $result = mysql_query("SELECT * FROM tbl_workshops WHERE workshop_id = '{$class}'");
            if (mysql_num_rows($result) > 0){
                $row = mysql_fetch_assoc($result);
                $grand_total = $row['workshop_price'] * $quantity;
                
            }
            else {
                // The class doesnt exist
            }
            }
            
        }
    // set some constants to make the code more readible.   
define("OFF_TOTAL", 1);
    define("OFF_INDIVIDUAL", 2);
    define("PERC_TOTAL", 3);
    define("PERC_INDIVIDUAL", 4);
    define("OFF_FRIEND", 5);
    define("PERC_FRIEND", 6);
    
    // Now we loop through each class and take off any valid discounts
    
    foreach ($classes as &$class)
        {
        if ($class['discount_type'] == OFF_INDIVIDUAL)
            {
            $total = $class_cost * $class['quantity'];
            $discount = $class['quantity'] * $class['discount_value'];
            $total = $total - $discount;
            $grand_total += $total;
            
            }
    
        if ($class['discount_type'] == PERC_INDIVIDUAL)
            {
            $total = $class_cost * $class['quantity'];
            // did say  $total = $class['cost'] * $class['quantity'];
            $discount = ($class['discount_value'] / 100) * $class_cost;
            $total = $total - $discount;
            $grand_total += $total;
            }
    
    
        if ($class['discount_type'] == OFF_FRIEND)
            {
            $firstclass = $class_cost-$class['discount_value'];
    $total = $class_cost * $class['quantity'] - $class['discount_value'];
    $grand_total += $total;
            }
    
if ($class['discount_type'] == PERC_FRIEND)
    {
    $discount = ($class['discount_value'] / 100) * $class_cost; 
    $total = $class_cost * $class['quantity'] - $discount;
    $grand_total += $total;
    }
    
        }

    if($discount_type == PERC_TOTAL)
        {
$total = mysql_escape_string($_POST['total']);
        $discount = ($class['discount_value'] / 100) * $total;
        $grand_total = $total - $discount;
        
        }

    if ($class['discount_type'] == OFF_TOTAL)
        {
        $discount_value = $class['discount_value'];
$grand_total = ($class_cost * $class['quantity'])-$discount_value;
        }


/* end insert of new math */
    $final_total = $grand_total;


$_SESSION['s_total_price'] = $final_total;
if ($_SESSION['s_total_price'] == 0) {
$_SESSION['$pay_or_not'] = "1";
/*header("Location: register4free.php"); */
echo $grand_total;

}	

/* now I need to total them up but how*/

?>	

Link to comment
Share on other sites

grand total should start as an int or float, not as a string, and you need to make sure you are adding a number to a number, and that total is not returning a string - example:

$grandTotal = 0;
foreach($something as $sum){
$total = $total +$sum;
$grandTotal += (float)$total;
}

Link to comment
Share on other sites

Hmmm.. Still no luck.  Here's what I've got right now:

 


$p = array(); 
    
    foreach ($classes as &$class)
        {
        if ($class['discount_type'] == OFF_INDIVIDUAL)
            {
            $total = $class_cost * $class['quantity'];
            $discount = $class['quantity'] * $class['discount_value'];
            $total = $total - $discount;
           /* $grand_total += $total;*/
		$grand_total += (float)$total;

            
            }
    
        if ($class['discount_type'] == PERC_INDIVIDUAL)
            {
            $total = $class_cost * $class['quantity'];
            // did say  $total = $class['cost'] * $class['quantity'];
            $discount = ($class['discount_value'] / 100) * $class_cost;
            $total = $total - $discount;
            /* $grand_total += $total;*/
		$grand_total += (float)$total;

            }
    
    
        if ($class['discount_type'] == OFF_FRIEND)
            {
            $firstclass = $class_cost-$class['discount_value'];
    $total = $class_cost * $class['quantity'] - $class['discount_value'];
    /* $grand_total += $total;*/
		$grand_total += (float)$total;

            }
    
if ($class['discount_type'] == PERC_FRIEND)
    {
    $discount = ($class['discount_value'] / 100) * $class_cost; 
    $total = $class_cost * $class['quantity'] - $discount;
    /* $grand_total += $total;*/
		$grand_total += (float)$total;

    }


    if($discount_type == PERC_TOTAL)
        {
$total = mysql_escape_string($_POST['total']);
        $discount = ($class['discount_value'] / 100) * $total;
        $grand_total = $total - $discount;

    
        }

    if ($class['discount_type'] == OFF_TOTAL)
        {
        $discount_value = $class['discount_value'];
$grand_total = ($class_cost * $class['quantity'])-$discount_value;


        }

$p[] = $grand_total;
    
        }


  $final_total = array_sum($p);


?>	

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.