Click to See Complete Forum and Search --> : Urgent Help plzzzzzzzzz


Sonia
07-28-2003, 08:22 AM
The following code is found in a shopping cart. It's function is to display the products chosen by the user in a table.
Can anyone explain me that code.Plzzz


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('<TABLE BORDER>');

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 (var 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;
itemlist=itemlist+1;
document.writeln("<form name='f1' action = 'mailto:Gounshali@yahoo.com' enctype = 'text/plain' method = 'post' ><tr><td><input name = 'Item' value = ' " + theitem + " ' ></td><td align=right>" + thequantity + "</td><td align=right> " + theprice + "</td><td align=right>" + alterError(itemtotal) + "</td></tr>");
} 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><b>Total</b></td><td align=right>'+ alterError(totprice)+'</td><td></td></tr>');
document.writeln('</TABLE>');
}

Nevermore
07-28-2003, 09:32 AM
No offense to the code's author but this JavaScript isn't very good. Anyhow, here's a commented version for you:
I highlighted the bad bits. (Obscure Dilbert reference)




function showItems() { //Declare function
index = document.cookie.indexOf("TheBasket"); //Looks for the word 'TheBasket' in a cookie
countbegin = (document.cookie.indexOf("=", index) + 1); //Finds beginning of area of cookie with relevant info
countend = document.cookie.indexOf(";", index); //Finds end of area
if (countend == -1) { // These two check to make sure a semicolon was actually found
countend = document.cookie.length;// and if not assumes it was missed off and sets the end as the end of the cookie
}
fulllist = document.cookie.substring(countbegin, countend); //Gets relevant area of cookie as a string
totprice = 0; //Sets a variable
document.writeln('<TABLE BORDER>'); //Adds an (incorrect) table declaration to a new HTML page.

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>');// Creates column headers for said table
itemlist = 0; //Sets a variable
for (var i = 0; i <= fulllist.length; i++) { //Creates 'for loop' (iterates a number of times equal to the number of letters in the cookie string
if (fulllist.substring(i,i+1) == '[') { //if the letter we are currently on is an opening square bracket
itemstart = i+1; // Variable itemstart = i+1
} else if (fulllist.substring(i,i+1) == ']') { //Or if the letter is a closing square bracket
itemend = i; //Then the end of that item has been found
thequantity = fulllist.substring(itemstart, itemend); //Extracts the item quantity from between the two brackets
itemtotal = 0; //Sets a variable
itemtotal = (eval(theprice*thequantity)); //Total price = price per unit * quantity
temptotal = itemtotal * 100; //Creates a subtotal?
totprice = totprice + itemtotal; //Increments total price
itemlist=itemlist+1; //Incremenents number of items
document.writeln("<form name='f1' action = 'mailto:Gounshali@yahoo.com' enctype = 'text/plain' method = 'post' ><tr><td><input name = 'Item' value = ' " + theitem + " ' ></td><td align=right>" + thequantity + "</td><td align=right> " + theprice + "</td><td align=right>" + alterError(itemtotal) + "</td></tr>");//Writes some more text
} else if (fulllist.substring(i,i+1) == ',') { //If the substring that wasnt a bracket is a comma
theitem = fulllist.substring(itemstart, i); //Then we have found the item itself
itemstart = i+1; //Sets a variable with the 'start number' of the item name
} else if (fulllist.substring(i,i+1) == '#') { //If it is a #
theprice = fulllist.substring(itemstart, i); //Then we have found the price
itemstart = i+1; //For some reason it seems to be taking the price down as the item name.
}
}

document.writeln('<tr><td colspan=3><b>Total</b></td><td align=right>'+ alterError(totprice)+'</td><td></td></tr>');//Writes more HTML
document.writeln('</TABLE>');//Writes more HTML
}

Nevermore
07-28-2003, 09:33 AM
Is this for a class? Cos if it is, you might want to find a better script. (Or even better, make your own and actually learn!!)

Sonia
07-28-2003, 10:40 AM
Yes,I'd like to learn how to make one on my own!