Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript for loop to get order details
    primarykey
    data
    text
    <p>I want to put a for loop in this nopcart.js file so that I can save the details of the order as a string. At the moment it is outputting all the details of the order into a table to display of the checkout page. I just want the array information </p> <pre><code>fields = new Array; fields[0] = database.substring( 0, Token0 ); // Product ID fields[1] = database.substring( Token0+1, Token1 ); // Quantity fields[2] = database.substring( Token1+1, Token2 ); // Price fields[3] = database.substring( Token2+1, Token3 ); // Product Name/Description fields[4] = database.substring( Token3+1, Token4 ); // Shipping Cost fields[5] = database.substring( Token4+1, database.length ); //Additional Information </code></pre> <p>to loop and save into one string. Then have all the order details on one string which I can then save to a text file in php. (We are not using SQL at the moment. </p> <p>So far my attempts have only gotten the last item in the cart, or the loop is also writing the html table code as well. This is for a uni assignment and we were not meant to edit the basic shopping cart functions but I think it will be much more useful saving the customer and order details to a text file.</p> <pre><code>//---------------------------------------------------------------------|| // FUNCTION: CheckoutCart || // PARAMETERS: Null || // RETURNS: Product Table Written to Document || // PURPOSE: Draws current cart product table on HTML page for || // checkout. || //---------------------------------------------------------------------|| function CheckoutCart( ) { var iNumberOrdered = 0; //Number of products ordered var fTotal = 0; //Total cost of order var fTax = 0; //Tax amount var fShipping = 0; //Shipping amount var strTotal = ""; //Total cost formatted as money var strTax = ""; //Total tax formatted as money var strShipping = ""; //Total shipping formatted as money var strOutput = ""; //String to be written to page var bDisplay = true; //Whether to write string to the page (here for programmers) var strPP = ""; //Payment Processor Description Field iNumberOrdered = GetCookie("NumberOrdered"); if ( iNumberOrdered == null ) iNumberOrdered = 0; if ( TaxByRegion ) { QueryString_Parse(); fTax = parseFloat( QueryString( OutputOrderTax ) ); strTax = moneyFormat(fTax); } if ( bDisplay ) strOutput = "&lt;TABLE CLASS=\"nopcart\"&gt;&lt;TR&gt;" + "&lt;TD CLASS=\"nopheader\"&gt;&lt;B&gt;"+strILabel+"&lt;/B&gt;&lt;/TD&gt;" + "&lt;TD CLASS=\"nopheader\"&gt;&lt;B&gt;"+strDLabel+"&lt;/B&gt;&lt;/TD&gt;" + "&lt;TD CLASS=\"nopheader\"&gt;&lt;B&gt;"+strQLabel+"&lt;/B&gt;&lt;/TD&gt;" + "&lt;TD CLASS=\"nopheader\"&gt;&lt;B&gt;"+strPLabel+"&lt;/B&gt;&lt;/TD&gt;" + (DisplayShippingColumn?"&lt;TD CLASS=\"nopheader\"&gt;&lt;B&gt;"+strSLabel+"&lt;/B&gt;&lt;/TD&gt;":"") + "&lt;/TR&gt;"; for ( i = 1; i &lt;= iNumberOrdered; i++ ) { NewOrder = "Order." + i; database = ""; database = GetCookie(NewOrder); Token0 = database.indexOf("|", 0); Token1 = database.indexOf("|", Token0+1); Token2 = database.indexOf("|", Token1+1); Token3 = database.indexOf("|", Token2+1); Token4 = database.indexOf("|", Token3+1); fields = new Array; fields[0] = database.substring( 0, Token0 ); // Product ID fields[1] = database.substring( Token0+1, Token1 ); // Quantity fields[2] = database.substring( Token1+1, Token2 ); // Price fields[3] = database.substring( Token2+1, Token3 ); // Product Name/Description fields[4] = database.substring( Token3+1, Token4 ); // Shipping Cost fields[5] = database.substring( Token4+1, database.length ); //Additional Information fTotal += (parseInt(fields[1]) * parseFloat(fields[2]) ); fShipping += (parseInt(fields[1]) * parseFloat(fields[4]) ); if ( !TaxByRegion ) fTax = (fTotal * TaxRate); strTotal = moneyFormat(fTotal); if ( !TaxByRegion ) strTax = moneyFormat(fTax); strShipping = moneyFormat(fShipping); if ( bDisplay ) { strOutput += "&lt;TR&gt;&lt;TD CLASS=\"nopentry\"&gt;" + fields[0] + "&lt;/TD&gt;"; if ( fields[5] == "" ) strOutput += "&lt;TD CLASS=\"nopentry\"&gt;" + fields[3] + "&lt;/TD&gt;"; else strOutput += "&lt;TD CLASS=\"nopentry\"&gt;" + fields[3] + " - &lt;I&gt;"+ fields[5] + "&lt;/I&gt;&lt;/TD&gt;"; strOutput += "&lt;TD CLASS=\"nopentry\"&gt;" + fields[1] + "&lt;/TD&gt;"; strOutput += "&lt;TD CLASS=\"nopentry\"&gt;"+ MonetarySymbol + moneyFormat(fields[2]) + "/ea&lt;/TD&gt;"; if ( DisplayShippingColumn ) { if ( parseFloat(fields[4]) &gt; 0 ) strOutput += "&lt;TD CLASS=\"nopentry\"&gt;"+ MonetarySymbol + moneyFormat(fields[4]) + "/ea&lt;/TD&gt;"; else strOutput += "&lt;TD CLASS=\"nopentry\"&gt;N/A&lt;/TD&gt;"; } strOutput += "&lt;/TR&gt;"; } if ( AppendItemNumToOutput ) { strFooter = i; } else { strFooter = ""; } if ( PaymentProcessor != '' ) { //Process description field for payment processors instead of hidden values. //Format Description of product as: // ID, Name, Qty X strPP += fields[0] + ", " + fields[3]; if ( fields[5] != "" ) strPP += " - " + fields[5]; strPP += ", Qty. " + fields[1] + "\n"; } else { strOutput += "&lt;input type=hidden name=\"" + OutputItemId + strFooter + "\" value=\"" + fields[0] + "\"&gt;"; strOutput += "&lt;input type=hidden name=\"" + OutputItemQuantity + strFooter + "\" value=\"" + fields[1] + "\"&gt;"; strOutput += "&lt;input type=hidden name=\"" + OutputItemPrice + strFooter + "\" value=\"" + fields[2] + "\"&gt;"; strOutput += "&lt;input type=hidden name=\"" + OutputItemName + strFooter + "\" value=\"" + fields[3] + "\"&gt;"; strOutput += "&lt;input type=hidden name=\"" + OutputItemShipping + strFooter + "\" value=\"" + fields[4] + "\"&gt;"; strOutput += "&lt;input type=hidden name=\"" + OutputItemAddtlInfo + strFooter + "\" value=\"" + fields[5] + "\"&gt;"; } } if ( bDisplay ) { strOutput += "&lt;TR&gt;&lt;TD CLASS=\"noptotal\" COLSPAN=3&gt;&lt;B&gt;"+strSUB+"&lt;/B&gt;&lt;/TD&gt;"; strOutput += "&lt;TD CLASS=\"noptotal\" COLSPAN=2 ALIGN=RIGHT&gt;&lt;B&gt;" + MonetarySymbol + strTotal + "&lt;/B&gt;&lt;/TD&gt;"; strOutput += "&lt;/TR&gt;"; if ( DisplayShippingRow ) { strOutput += "&lt;TR&gt;&lt;TD CLASS=\"noptotal\" COLSPAN=3&gt;&lt;B&gt;"+strSHIP+"&lt;/B&gt;&lt;/TD&gt;"; strOutput += "&lt;TD CLASS=\"noptotal\" COLSPAN=2 ALIGN=RIGHT&gt;&lt;B&gt;" + MonetarySymbol + strShipping + "&lt;/B&gt;&lt;/TD&gt;"; strOutput += "&lt;/TR&gt;"; } if ( DisplayTaxRow || TaxByRegion ) { strOutput += "&lt;TR&gt;&lt;TD CLASS=\"noptotal\" COLSPAN=3&gt;&lt;B&gt;"+strTAX+"&lt;/B&gt;&lt;/TD&gt;"; strOutput += "&lt;TD CLASS=\"noptotal\" COLSPAN=2 ALIGN=RIGHT&gt;&lt;B&gt;" + MonetarySymbol + strTax + "&lt;/B&gt;&lt;/TD&gt;"; strOutput += "&lt;/TR&gt;"; } strOutput += "&lt;TR&gt;&lt;TD CLASS=\"noptotal\" COLSPAN=3&gt;&lt;B&gt;"+strTOT+"&lt;/B&gt;&lt;/TD&gt;"; strOutput += "&lt;TD CLASS=\"noptotal\" COLSPAN=2 ALIGN=RIGHT&gt;&lt;B&gt;" + MonetarySymbol + moneyFormat((fTotal + fShipping + fTax)) + "&lt;/B&gt;&lt;/TD&gt;"; strOutput += "&lt;/TR&gt;"; strOutput += "&lt;/TABLE&gt;"; if ( PaymentProcessor == 'an') { //Process this for Authorize.net WebConnect strOutput += "&lt;input type=hidden name=\"x_Version\" value=\"3.0\"&gt;"; strOutput += "&lt;input type=hidden name=\"x_Show_Form\" value=\"PAYMENT_FORM\"&gt;"; strOutput += "&lt;input type=hidden name=\"x_Description\" value=\""+ strPP + "\"&gt;"; strOutput += "&lt;input type=hidden name=\"x_Amount\" value=\""+ moneyFormat((fTotal + fShipping + fTax)) + "\"&gt;"; } else if ( PaymentProcessor == 'wp') { //Process this for WorldPay strOutput += "&lt;input type=hidden name=\"desc\" value=\""+ strPP + "\"&gt;"; strOutput += "&lt;input type=hidden name=\"amount\" value=\""+ moneyFormat((fTotal + fShipping + fTax)) + "\"&gt;"; } else if ( PaymentProcessor == 'lp') { //Process this for LinkPoint strOutput += "&lt;input type=hidden name=\"mode\" value=\"fullpay\"&gt;"; strOutput += "&lt;input type=hidden name=\"chargetotal\" value=\""+ moneyFormat((fTotal + fShipping + fTax)) + "\"&gt;"; strOutput += "&lt;input type=hidden name=\"tax\" value=\""+ MonetarySymbol + strTax + "\"&gt;"; strOutput += "&lt;input type=hidden name=\"subtotal\" value=\""+ MonetarySymbol + strTotal + "\"&gt;"; strOutput += "&lt;input type=hidden name=\"shipping\" value=\""+ MonetarySymbol + strShipping + "\"&gt;"; strOutput += "&lt;input type=hidden name=\"desc\" value=\""+ strPP + "\"&gt;"; } else { strOutput += "&lt;input type=hidden name=\""+OutputOrderSubtotal+"\" value=\""+ MonetarySymbol + strTotal + "\"&gt;"; strOutput += "&lt;input type=hidden name=\""+OutputOrderShipping+"\" value=\""+ MonetarySymbol + strShipping + "\"&gt;"; strOutput += "&lt;input type=hidden name=\""+OutputOrderTax+"\" value=\""+ MonetarySymbol + strTax + "\"&gt;"; strOutput += "&lt;input type=hidden name=\""+OutputOrderTotal+"\" value=\""+ MonetarySymbol + moneyFormat((fTotal + fShipping + fTax)) + "\"&gt;"; } } document.write(strOutput); document.close(); } </code></pre> <p>Thanks guys for your help, you have already helped me out a fair bit already. I can't wait until I'm much more familiar with programming and can figure this stuff out instantly.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload