O
O
OKNOZA2015-11-22 19:41:22
PHP
OKNOZA, 2015-11-22 19:41:22

What needs to be done to combine with?

I took the code from the website of the online store (basket), the meaning is this, it simply enters all the data into the localStorage browser, it works, but I need to recalculate the data, the final price (total amount) and the total weight (weight product)
MySQL

  1. id - number (1)
  2. name - product name (sugar)
  3. price - item price (5r)
  4. weight - product weight ( 0.1 grams)


Jquery
$(document).ready(function() {

// Display the contents of the store
function ref_cart() {
var output = "";
$(".cart li").remove();
for (var i = 0; i < localStorage.length; i++) {
output += "ID: "+localStorage.key(i)+" | Quantity: "+localStorage.getItem(localStorage.key(i))+ " X ";
}
$(".cart ").append(output);
}

// compatibility check
function web_storage() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}

ref_cart();

// Add to cart
$(".incart").on('click', function() {
var reg = /[0-9]/,
id = $(this).attr("data-pr"),
kolvo = $("#"+id).val();

if (reg.test(kolvo)) {
if(web_storage()) {
$("#"+id).val('');
localStorage.setItem (id, kolvo);
ref_cart();
} else{
alert("Your browser cannot handle local storage!");
}
} else {
$("#"+id).val('');
alert(" Use only numbers!");
}
});

// Remove from cart
$(document.body).on('click','.remove',function() {
localStorage.removeItem($(this).attr("data-pr"));
$(this).parent('li').remove();
});

});


And this I display everything from the database, the name and ID of the product
<? while ($row = mysqli_fetch_array($result)): ?>
  <tr>
        <td><?=$row['name'];?></td>        
        <td><div class="userpro-input"> <input type="text" id="pr<?=$row['id'];?>" name="username_or_email" style="color:black;"  class="log_pass_input"></div></td>
        <td><div class="userpro-input"> <button data-pr="pr<?=$row['id'];?>" class="incart">В корзину</button> </div>   </td>
    </tr>        
    <? endwhile; ?>


Please help me to recalculate the final view.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MuTaToRage, 2015-11-22
@MuTaToRage

What is the problem with taking price from the database and creating a variable for summing?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question