Click to See Complete Forum and Search --> : Sorry but I need a bit of help again


michael
02-25-2003, 06:03 PM
I am trying to make a varible increment itself by a certain amount in a loop.

It's for part of an order form.

This is part of the script....

itemlist = 0;
for (i = 0; i <= fulllist.length; i++) {
if (fulllist.substring(i,i+1) == '[') {
itemstart = i+1;
} else if (fulllist.substring(i,i+1) == ']') {
itemend = i;
thequantity = fulllist.substring(itemstart, itemend);
itemtotal = 0;
itemtotal = (eval(theprice*thequantity));
temptotal = itemtotal * 100;
totprice = totprice + itemtotal;
blendtot = blendtot + thequantity;
itemlist=itemlist+1;

etc............

What I want to do is have the variable called blendtot increase itself by thequantity each time it goes through the loop.
The way I see it, blendtot = blendtot + thequantity should mean that if there where 3 items in the order, and for each item there was just 1 of them, that would mean 3 items in all, which means that blendtot should also = 3, because thequantity = 1 for each loop (3 loops in all (if 3 items are ordered)).
The variable thequantity is the amount of each item ordered, so if there are 3 items ordered, and there are 2 of the first item, 2 of the second item and 5 of the third item then that makes 9 items in total.
According to blendtot = blendtot + thequantity blendtot should = 9.
Hope this makes sence.

Anyway I have written the script so that I can see the value for blendtot and instead of it printing 3 it's printing 111.
It seem to be dispalying the 3 one's rather than adding them together.

Any ideas?

Thank you.

pyro
02-25-2003, 06:07 PM
It is probably just concatinating your strings, rather than adding them, try...

yourvar = Number(yourfirstvar) + Number(yoursecondvar);

michael
02-25-2003, 06:17 PM
Sorry to seem REALLY STUPID but do I actually write the equation like this....

blendtot = number(blendtot) + number(thequantity);

if so then it didn't work.

pyro
02-25-2003, 06:20 PM
Yes, that is how you would code it to add blendtot + thequantity. What did it return?

michael
02-25-2003, 06:33 PM
Well it didn't actually return anything because it stoped the bottom part of the form from being displayed, that part where the total, subtotal, etc... are shown.

michael
02-25-2003, 06:42 PM
Also I noticed a small yellow triangle in the bottom left of my browser, suggesting that the script has an error in it somewhere.

pyro
02-25-2003, 06:42 PM
Would you post the full code, or a link?

michael
02-25-2003, 07:16 PM
I really appreciate your help on this.

The full code for the whole page is below. This is just the order form part of a Java shopping cart called JShop.
The variables for the items are stored on another page in a frame then the order form page (below) prints out the complete order.
The form itself works fine, but the person I'm doing it for wants a discount percentage to be included. This percentage would increase the bigger the number of total items is.

If you can help I'll give you credit on the finished website.
If you actually need a link to a site that uses this JShop, then visit either www.katseyeblends.co.uk or www.ljonesservices.co.uk

Thanks.

Full code here...

<HTML>

<HEAD>
<TITLE>Shopping Buy</TITLE>
</HEAD>

<BODY bgcolor="white" text="black" link="blue" vlink="blue" alink="blue">
<p align="left">
<SCRIPT LANGUAGE="JavaScript">
blendtot = 0
// alterError - fixes a rounding bug in Netscape 2
function alterError(value) {
if (value<=0.99) {
newPounds = '0';
} else {
newPounds = parseInt(value);
}
newPence = parseInt((value+.0008 - newPounds)* 100);
if (eval(newPence) <= 9) newPence='0'+newPence;
newString = newPounds + '.' + newPence;
return (newString);
}
// showItems () - creates a table of items in the basket and
// creates the start of a form which sets information for
// basket items
function showItems() {
index = document.cookie.indexOf("TheBasket");
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
fulllist = document.cookie.substring(countbegin, countend);
totprice = 0;
document.writeln('<FORM action="bfinish.html" target="_top">');
document.writeln('<TABLE BORDER COLS=4>');

document.writeln('<TR><TD><b>Item</b></TD><TD><b>Quantity</b></TD><TD><b>Cost Each</b></TD><td><b>Total Cost</b></TR>');
itemlist = 0;
for (i = 0; i <= fulllist.length; i++) {
if (fulllist.substring(i,i+1) == '[') {
itemstart = i+1;
} else if (fulllist.substring(i,i+1) == ']') {
itemend = i;
thequantity = fulllist.substring(itemstart, itemend);
itemtotal = 0;
itemtotal = (eval(theprice*thequantity));
temptotal = itemtotal * 100;
totprice = totprice + itemtotal;
blendtot = blendtot + thequantity;
itemlist=itemlist+1;
document.writeln('<tr><td>'+theitem+'</td><td align=right>'+thequantity+'</td><td align=right>'+theprice+'</td><td align=right>'+alterError(itemtotal)+'</td></tr>');
document.writeln('<INPUT TYPE="hidden" NAME="item'+itemlist+'" VALUE="'+theitem+'" SIZE="40">');
document.writeln('<INPUT TYPE="hidden" NAME="quantity'+itemlist+'" VALUE="'+thequantity+'" SIZE="40">');
document.writeln('<INPUT TYPE="hidden" NAME="price each'+itemlist+'" VALUE="'+theprice+'" SIZE="40">');
document.writeln('<INPUT TYPE="hidden" NAME="total cost'+itemlist+'" VALUE="'+alterError(itemtotal)+'" SIZE="40">');
} else if (fulllist.substring(i,i+1) == ',') {
theitem = fulllist.substring(itemstart, i);
itemstart = i+1;
} else if (fulllist.substring(i,i+1) == '#') {
theprice = fulllist.substring(itemstart, i);
itemstart = i+1;
}
}
document.writeln('<tr><td colspan=3 align=right><b>SubTotal (£)</b></td><td align=right>'+alterError(totprice)+'</td></tr>');
document.writeln('<tr><td colspan=3 align=left><a href="discounts.html"><font size="2">'+blendtot+'</font></a><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- discount</b></td><td align=right>'+findPercentage()+' %</td></tr>');
document.writeln('<tr><td colspan=3 align=right><b>Total (£)</b></td><td align=right>'+findDiscount().toFixed(2)+'</td></tr>');
document.writeln('<INPUT TYPE="hidden" NAME="Goods Total" VALUE="'+alterError(totprice)+'" SIZE="40">');
document.writeln('</TABLE>');
}

<!-- Begin percentage
function findPercentage() {
var units = getNumberOfUnits();
percentage = getPercentage(units);
return percentage;
}
function getNumberOfUnits() {
var units = blendtot;
return units;
}
function getPercentage(units) {
if (units >= 100) return 45;
if (units >= 70) return 40;
if (units >= 40) return 35;
if (units >= 20) return 30;
if (units >= 10) return 25;
if (units <= 9) return 0;
}
// End percentage -->

<!-- Begin discount
function findDiscount() {
var subtotal = alterError(totprice);
var discount = (alterError(totprice)*(percentage/100)).toFixed(2);
var total = subtotal - discount;
return total;
}
// End discount -->
</SCRIPT>


.........just some HTML here to get customer info..........


<!-- call showItems to show items in basket -->
<SCRIPT LANGUAGE="JavaScript">
showItems();
</SCRIPT>
<!-- finish off the form with other details and a /FORM tag -->


...............some HTML follows............

pyro
02-25-2003, 07:43 PM
Try my first post again, I edited it, and it may work now...

michael
02-25-2003, 08:56 PM
It works !!!!!!

You're a bloody genius...

Thank you very much.