/    Sign up×
Community /Pin to ProfileBookmark

Calculate Array of Discounts

### How can I calculate the Array of Discounts against Base Price in initial then calculate against the Amount After Discount?

This [screenshot ](https://i.stack.imgur.com/atlJp.png) is having a Base Price. This base price can have multiple discounts.

My PHP script here;

`<tbody>
<div class=”form-group”>
<label class=”col-sm-3″ style=’color: green’><strong>Base Price</strong></label>
<div class=’col-sm-8′>
<input type=’number’ class=’form-control’ id=’basePrice’ name=”basePrice” value=’5985′>
</div>
</div>
<?php
$query = $connect->prepare(“SELECT * FROM `
manage_customers_discount` WHERE status = 1 AND company_id = ‘1’ AND discount_is_deleted = 0″);
$query->execute();
$query->store_result();

$rows = $query->num_rows;
$rows = $rows + 1;

$arrayDiscountNumber = 0;
for ($y = 1; $y < $rows; $y++) { ?>

<tr id=”row<?php echo $y; ?>” class=”<?php echo $arrayDiscountNumber; ?>”>
<td style=”margin-left:20px;”>

<div class=”form-group”>

<select class=”form-control” name=”discountName[]” id=”discountName<?php echo $y; ?>” onchange=”getDiscountData(<?php echo $y; ?>)”>
<option value=””>~~SELECT~~</option>
<?php
$discountSql = “SELECT * FROM `manage_customers_discount` WHERE status = 1 AND company_id = ‘1’ AND discount_is_deleted = 0 ORDER BY discount_order ASC”;
$discountData = $connect->query($discountSql);

while ($row = $discountData->fetch_array()) {
echo “<option value='” . $row[‘id’] . “‘ id=’changeDiscount” . $row[‘id’] . “‘>” . $row[‘discount_name’] . “</option>”;
} // /while

?>
</select>
</div>
</td>
<td style=”padding-left:20px;”>
<input type=”text” name=”rateDiscount[]” id=”rateDiscount<?php echo $y; ?>” autocomplete=”off” disabled=”true” class=”form-control” />
<input type=”hidden” name=”rateDiscountValue[]” id=”rateDiscountValue<?php echo $y; ?>” autocomplete=”off” class=”form-control” />
</td>
<td style=”padding-left:20px;”>
<input type=”text” name=”totalDiscount[]” id=”totalDiscount<?php echo $y; ?>” autocomplete=”off” class=”form-control” disabled=”true” />
<input type=”hidden” name=”totalDiscountValue[]” id=”totalDiscountValue<?php echo $y; ?>” autocomplete=”off” class=”form-control” />
</td>
<td style=”padding-left:20px;”>
<input type=”text” name=”amountAfterDiscount[]” id=”amountAfterDiscount<?php echo $y; ?>” autocomplete=”off” class=”form-control” disabled=”true” />
<input type=”hidden” name=”amountAfterDiscountValue[]” id=”amountAfterDiscountValue<?php echo $y; ?>” autocomplete=”off” class=”form-control” />
</td>
<td>

<button class=”btn btn-default removeDiscountRowBtn” type=”button” id=”removeDiscountRowBtn” onclick=”removeDiscountRow(<?php echo $y; ?>)”><i class=”glyphicon glyphicon-trash”></i></button>
</td>
</tr>
<?php
$arrayDiscountNumber++;
} // /for
?>

</tbody>`

As you can see in the screenshot, I have calculated the base price which has given me the first Amount after Discount Base Price * 15%. In the second discount, I need to calculate the First Amount After Discount * 10%. Same with the 3rd discount. The last Amount after Discount will display in the Sub Discount Amount.

Below is my JS,

`var subTotalValue = $(‘#basePrice’).val();

$(“#rateDiscount” + row).val(response.percentage);
$(“#rateDiscountValue” + row).val(response.percentage);

var total = Number(response.percentage) * Number(subTotalValue);
total = total.toFixed(2);
$(“#totalDiscount” + row).val(total);
$(“#totalDiscountValue” + row).val(total);

var total = Number(subTotalValue) – Number(total);
total = total.toFixed(2);
$(“#amountAfterDiscount” + row).val(total);
$(“#amountAfterDiscountValue” + row).val(total);`

PROBLEM: It is calculating all the percentage against the Base Price

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @borgy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...