I can't seem to figure out what I'm missing. After the end user selects one or more classes, they go to a page that runs a foreach statement to determine which discount to use on each class. But it doesn't seem to be adding the resulting total to the array, so I can create a final total of all class costs.

Here's what I've got so far...
line 80 starts the empty array
Line 82 is the foreach statement
Line 143 is where I try to put into the array
Line 160 I tried to total up the array

.I get 0.

<?php


session_start();




// connect to database 
include("dbconn.php"); 
$discount = mysql_escape_string($_POST['discount']);
$donation = mysql_escape_string($_POST['donation']);


    $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 = 0;
    // 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;

                $p[] = $grand_total;
            }
            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

    $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 = $grand_total;


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


    }   


      $final_total2 = array_sum($p);

    /* end insert of new math */
?>  
Member Avatar for jmichae3

I think you need to learn how to query a database table.
unless you know you are going to get exactly 1 row (such as you would get with COUNT(), AVG(), MAX(), MIN() and such), do this:

$result1 = mysql_query("SELECT * FROM sometable WHERE id='".intval($_POST['id'])."'", $link);
while ($row = mysql_fetch_assoc(result1)) {
    //do something with $row[]
}
mysql_free_result($result1);

mysql_free_result() allows us to use $result1 again and doesn't scramble our results like it would if we didn't.
your first query block is a nested query. this would be fine, however, there is no while statement being used, so you are not going to have any decent results. you are going to have to think about your data sets and recode.

2nd issue is your coding of strings. integers should be surrounded with php's intval() to make sure they are really integers. strings should be surrounded with mysql_real_escape_string() to escape certain characters before they get into the database!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.