turpentyne Posted June 7, 2012 Share Posted June 7, 2012 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*/ ?> Quote Link to comment https://forums.phpfreaks.com/topic/263793-how-to-total-up-for-each-results/ Share on other sites More sharing options...
turpentyne Posted June 7, 2012 Author Share Posted June 7, 2012 update: I just tried to add in the following buts but it didn't work. $p = array() and to each math section: $p[] = $grand_total; then at the end: $p[] = $grand_total; $final_total = array_sum($p) This just gave me '0' Quote Link to comment https://forums.phpfreaks.com/topic/263793-how-to-total-up-for-each-results/#findComment-1351815 Share on other sites More sharing options...
gristoi Posted June 7, 2012 Share Posted June 7, 2012 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; } Quote Link to comment https://forums.phpfreaks.com/topic/263793-how-to-total-up-for-each-results/#findComment-1351861 Share on other sites More sharing options...
turpentyne Posted June 8, 2012 Author Share Posted June 8, 2012 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/263793-how-to-total-up-for-each-results/#findComment-1352034 Share on other sites More sharing options...
alvin567 Posted June 8, 2012 Share Posted June 8, 2012 how about adding a counter on the outer loop? Quote Link to comment https://forums.phpfreaks.com/topic/263793-how-to-total-up-for-each-results/#findComment-1352045 Share on other sites More sharing options...
turpentyne Posted June 8, 2012 Author Share Posted June 8, 2012 Trying to figure out how to do that, but I'm wondering if that's not going to help me? Does it count the number of times the loop occurs? each time will be a different amount, so how would it do the math? I need each individual total. Quote Link to comment https://forums.phpfreaks.com/topic/263793-how-to-total-up-for-each-results/#findComment-1352086 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.