I've created a cart for users to put items one wants to buy.
Each item has its own id value, all prices and general info coming from MySql db.
I'm outputting javascript to count quantity and final price with discounts for each model individually. Now I want to rebuild counting and it's getting hard to maintain my php code.
My questions are: 1 - what is the best practice to create JS carts with php. 2 - is there a way to simplify my code or it's better to rewrite it from 0.
foreach ($_SESSION["gids"] as $key => $value){//for each model in session add HTML data
echo "\n\t\t<hr><div class=\"row\">";
echo "\n\t\t\t<div class=\"2u\">";//Main photo div
echo "\n\t\t\t\t<span class=\"image image-full cartImg\" id=\"caseShow\">";
echo "\n\t\t\t\t\t<img src=\"cases/cartPics/".$value.".jpg\" id=\"caseImg\" alt=\"Чехол для iPhone 5 с орнаментом этно чехол вышиванка на iPhone\" />";
echo "\n\t\t\t\t</span>";
echo "\n\t\t\t</div>";
echo "\n\t\t\t<div class=\"2u\">";// name and model
echo linkSet('menu', 'name');
echo "\n\t\t\t\t<strong>";
echo catSet('cases', $value, 'name');
echo "\n\t\t\t\t<br />";
echo catSet('cases', $value, 'model');
echo "\n\t\t\t</strong></div>";
echo "\n\t\t\t<div class=\"2u\" id=\"casePriceDiv\">";//price for one item
echo linkSet('menu', 'priceName');
echo "\n\t\t\t\t<br /><span class=\"casePriceCart\">";
echo price('cases', $value);
echo "\n\t\t\t</span></div>";
echo "\n\t\t\t<div class=\"2u\">";//quantity
echo linkSet('menu', 'buyQty');
echo "\t\t\t\t<br /><br />";
echo "\n\t\t\t\t<div>";
echo "\n\t\t\t\t\t<label for=\"qty\"><abbr title=\"Quantity\"></abbr></label>";
echo "\n\t\t\t\t\t<button class=\"cartOpt cartOpt_".$value."\" onclick=\"modify_qty_".$value."(-1)\"><strong><</strong></button>";
echo "\n\t\t\t\t\t<input class =\"qty\" id=\"qty_".$value."\" value=\"1\" disabled=\"disabled\"/>";
echo "\n\t\t\t\t\t<button class=\"cartOpt cartOpt_".$value."\" onclick=\"modify_qty_".$value."(1)\"><strong>></strong></button>";
echo "\n\t\t\t\t</div>";
echo "\n\t\t\t</div>";
echo "\n\t\t\t<div id=\"casePriceDiv\" class=\"2u\">";//price of quantity chosen
echo linkSet('menu', 'buySum');
echo "\n\t\t\t\t - ";
echo linkSet('menu', 'currency');
echo "\n\t\t\t\t<br />";
echo "\n\t\t\t\t<strong>";
echo priceDisc('cases', $value);
echo "\n\t\t\t\t</strong>";
echo "\n\t\t\t\t<div id=\"sale2discCart\" class=\"sale2discCart_".$value."\"></div>";
echo "\n\t\t\t</div>";
print "<script>
function modify_qty_".$value."(val) {
var qty = document.getElementById('qty_".$value."').value;
var new_qty = parseInt(qty,10) + val;
if (new_qty < 1) {
new_qty = 1;
}else if(new_qty > 5){
new_qty = 5;
}
document.getElementById('qty_".$value."').value = new_qty;
document.getElementById('qtyForm_" .$value."' ).value = new_qty;
return new_qty;
};
$(document).ready(function(){
var itemPrice = 0;
$('#itemPrice_".$value."').each(function(){
itemPrice += parseInt($(this).text(),10);
$('.cartOpt_".$value."').click(function(){
var new_qty = ($('#qty_".$value."').val());
var totalItemPrice = new_qty * itemPrice;
$('#itemPrice_".$value."').html(totalItemPrice);
productsInCart();
});
});
function productsInCart(){
var inCart = 0;
var finalPrice;
$('.qty').each(function(){
inCart += parseInt($(this).val());
});
if(inCart<2){
var total = 0;
$('.itemPrice').each(function(){
total += parseInt(this.innerHTML);
});
$('#overallPrice').text(total);
document.getElementById('priceForm_" .$value."' ).value = total;
$('.itemPrice').css('text-decoration','none').addClass;
$('#sale2discCart').empty();
$('.totalDisc').hide();
finalPrice = total;
}else{
var totalNet = 0;
var total = 0;
var totalDisc =0;
var sale2disc = \"-30%\";
var sale2price = $('.itemPrice');
var new2Price = 0;
$('#itemPrice_".$value."').each(function(){
new2Price += parseInt(this.innerHTML)* 0.7;
new2Price = Math.round(new2Price);
document.getElementById('priceForm_" .$value."' ).value = new2Price;
$(this).css('text-decoration','line-through').addClass;
$(this).css('letter-spacing','1px').addClass;
$('.sale2discCart_".$value."').html(sale2disc);
$('.sale2discCart_".$value."').append('<br /><br /><span style=\"text-decoration:line-through\">');
$('.sale2discCart_".$value."').append(new2Price);
$('.sale2discCart_".$value."').append('</span><br />');
});
$('.itemPrice').each(function(){
totalNet = parseInt(this.innerHTML);
totalDisc += totalNet * 0.3;
total += totalNet * 0.7;
total = Math.round(total);
totalDisc = Math.round(totalDisc);
finalPrice = total;
});
$('#overallPrice').text(total);
$('.totalDisc').show();
$('#totalDisc').text(totalDisc).append('.00');
}
var delCost = $('.delCost').val();
finalPrice = parseInt(finalPrice, 10) + parseInt(delCost, 10);
document.getElementById('finalPrice' ).value = finalPrice;
}
productsInCart();
})
</script>";
echo "\n\t\t\t<div class=\"2u\">";
echo linkSet('menu', 'caseDel');
echo "\t\t\t\t\t<br /><br />";
echo "\n\t\t\t\t<label for=\"del\"><abbr title=\"Delete\"></abbr></label>";
echo "\n\t\t\t\t<form name =\"unsetCase\" method =\"POST\" action = \"includes/unsetCartModel.php\">";
echo "\n\t\t\t\t<button class=\"cartOpt\" name=\"unsetCase\" type=\"submit\" value=".$key."><strong>X</strong></button>";
echo "\n\t\t\t\t</form>";
echo "\n\t\t\t</div>";
echo "\n\t\t</div>\n\n";
};