Home » Php » php – Javascript is not doing sum of all records

php – Javascript is not doing sum of all records

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to do the sum of all cart items but it is not doing it.
It is showing only the sum of first or last item added in the cart.

What is the issue i cannot understand. I have tried by moving code in the java but no use.

Suppose i have 2 items.

Item1 total is = 100
Item2 total is = 200

Total should be 300 not only the 1 items total.

Below is my code.

Thanks in advance

<script type="text/javascript" src="js/jquery-1.12.1.min.js"></script>

<table class="table mg-bottom-45 refresh" id="myTable">
    <thead>
    <tr>
    <th class="product-cart-price"><span>PRICE</span></th>
    <th class="product-cart-price"><span>TOTAL</span></th>
    </tr>
    </thead>

<tbody>
<?php
include ('inc/db.php');

$citem  = "select * from orders_temp
where user_id = 1 order by id
";
$ciquery    = $dba2->query($citem);
while ($cifetch = $ciquery->fetch_assoc()){
    $orderID    =   $cifetch['id'];
    $userID =   $cifetch['user_id'];
?>
    <td class="product-cart-thumbnail">
    <input name="quantity" type="text" data-id="<?php echo $orderID; ?>" 
    data-price="" value="<?php echo $cifetch['qty']; ?>"
    class="un quant" /> 
    </td>


    <td class="product-cart-thumbnail">
    <input name="quantity" type="text" data-id="<?php echo $orderID; ?>" 
    data-price="<?php echo $cifetch['price']; ?>"
    value="<?php echo $cifetch['price']; ?>" class="un pricex" />
    </td>


<?php } ?>
    <td class="totalAmount"></td>
</tbody>
</table>


<script type="text/javascript">
$(document).ready(function() {
  updatexx<?php echo $orderID; ?>();
  function updatexx<?php echo $orderID; ?>() {
    var sumx = '';
    $('#myTable > tbody  > tr').each(function() {
      var quantityx = $(this).find('.quant').val(); 
      var pricex = $(this).find('.pricex').attr('data-price').replace(',', '.');
      var amountx = (quantityx * pricex).toFixed(3);

      sumx += amountx;
    });
    $('.totalAmount').text(sumx);
  }
});
</script>
How to&Answers:

First, please make sure that your loop is running multiple times (same number of products in cart).

$('#myTable > tbody  > tr').each(function() {}) 

so that the above loop iterate exact number times as per required.

You should make some changes in code as follows:

var sumx = 0.00;

and in loop code should be

 sumx += parsefloat(amountx);

Answer:

So here is the solution with some changes in my code.
Now it is working perfectly as required. It is doing the sum of all items and everything what i want.

<table class="table mg-bottom-45 refresh" id="myTable">
<tbody>
<?php
$i = 0;
//error_reporting(0);
$ses_mem    =   1;
include ('inc/db.php');

$citem      = "select id, user_id, qty, org_price  from orders_temp 
where user_id = '".$ses_mem."' and status = 1
order by id
";
$ciquery    = $dba2->query($citem);
while ($cifetch = $ciquery->fetch_assoc()){
    $orderID    =   $cifetch['id'];
?>

<tr>

<script type="text/javascript" src="js/jquery-1.12.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  updatexx<?php echo $orderID; ?>();
  function updatexx<?php echo $orderID; ?>() {
var sumx = 0.000;
var quantity;
$('#myTable > tbody  > tr').each(function() {

  quantity = $(this).find('.quant').val();  
  var price = $(this).find('.pricex').attr('data-price').replace(',', '.');
  var amountx = (quantity * price);
      var munterx = amountx.toFixed(3);

  sumx += amountx;
  $(this).find('.amountx<?php echo $orderID; ?>').text('' + munterx);
});
    var sumxx = sumx.toFixed(3);
$('.total').text(sumxx);
  }
});
</script>

        <td class="single-qty" style="border: 0;">
        <input id="<?php echo $orderID; ?>" name="quantity" type="text" 
        data-id="<?php echo $orderID; ?>" 
        data-price="" value="<?php echo $cifetch['qty']; ?>"
        class="un quant" />
        </td>

        <td class="product-cart-price pricex" data-price="<?php echo $cifetch['org_price']; ?>">
        </td>

        <td class="amountx<?php echo $orderID; ?>">
        </td>
</tr>
<?php } ?>
</tbody>
</table>

<table class="table">
<tbody>
<tr>
<td>Sub Total (RS)</td>
<td class="text-right"
    style="text-shadow: 2px 2px 3px #999999; font-weight: bold;
    color: black; direction: rtl;">
        <span class="total">
        </span>
    </td>
</tr>
</tbody>
</table>