javascript for循环获取订单详细信息

时间:2013-05-31 04:00:46

标签: javascript html loops for-loop

我想在这个nopcart.js文件中放置一个for循环,这样我就可以将订单的详细信息保存为字符串。目前,它将订单的所有细节输出到表格中以显示结帐页面。我只想要数组信息

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

循环并保存为一个字符串。然后在一个字符串上包含所有订单详细信息,然后我可以将其保存到php中的文本文件中。 (我们目前不使用SQL。

到目前为止,我的尝试只获得了购物车中的最后一项,或者循环也在编写html表格代码。这是一个单一的任务,我们不打算编辑基本的购物车功能,但我认为将客户和订单详细信息保存到文本文件会更有用。

//---------------------------------------------------------------------||
// 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 = "<TABLE CLASS=\"nopcart\"><TR>" +
                  "<TD CLASS=\"nopheader\"><B>"+strILabel+"</B></TD>" +
                  "<TD CLASS=\"nopheader\"><B>"+strDLabel+"</B></TD>" +
                  "<TD CLASS=\"nopheader\"><B>"+strQLabel+"</B></TD>" +
                  "<TD CLASS=\"nopheader\"><B>"+strPLabel+"</B></TD>" +
                  (DisplayShippingColumn?"<TD CLASS=\"nopheader\"><B>"+strSLabel+"</B></TD>":"") +
                  "</TR>";

   for ( i = 1; i <= 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 += "<TR><TD CLASS=\"nopentry\">"  + fields[0] + "</TD>";

         if ( fields[5] == "" )
            strOutput += "<TD CLASS=\"nopentry\">"  + fields[3] + "</TD>";
         else
            strOutput += "<TD CLASS=\"nopentry\">"  + fields[3] + " - <I>"+ fields[5] + "</I></TD>";

         strOutput += "<TD CLASS=\"nopentry\">" + fields[1] + "</TD>";
         strOutput += "<TD CLASS=\"nopentry\">"+ MonetarySymbol + moneyFormat(fields[2]) + "/ea</TD>";

         if ( DisplayShippingColumn ) {
            if ( parseFloat(fields[4]) > 0 )
               strOutput += "<TD CLASS=\"nopentry\">"+ MonetarySymbol + moneyFormat(fields[4]) + "/ea</TD>";
            else
               strOutput += "<TD CLASS=\"nopentry\">N/A</TD>";
         }

         strOutput += "</TR>";
      }

      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 += "<input type=hidden name=\"" + OutputItemId        + strFooter + "\" value=\"" + fields[0] + "\">";
         strOutput += "<input type=hidden name=\"" + OutputItemQuantity  + strFooter + "\" value=\"" + fields[1] + "\">";
         strOutput += "<input type=hidden name=\"" + OutputItemPrice     + strFooter + "\" value=\"" + fields[2] + "\">";
         strOutput += "<input type=hidden name=\"" + OutputItemName      + strFooter + "\" value=\"" + fields[3] + "\">";
         strOutput += "<input type=hidden name=\"" + OutputItemShipping  + strFooter + "\" value=\"" + fields[4] + "\">";
         strOutput += "<input type=hidden name=\"" + OutputItemAddtlInfo + strFooter + "\" value=\"" + fields[5] + "\">";

      } 

   }

   if ( bDisplay ) {
      strOutput += "<TR><TD CLASS=\"noptotal\" COLSPAN=3><B>"+strSUB+"</B></TD>";
      strOutput += "<TD CLASS=\"noptotal\" COLSPAN=2 ALIGN=RIGHT><B>" + MonetarySymbol + strTotal + "</B></TD>";
      strOutput += "</TR>";

      if ( DisplayShippingRow ) {
         strOutput += "<TR><TD CLASS=\"noptotal\" COLSPAN=3><B>"+strSHIP+"</B></TD>";
         strOutput += "<TD CLASS=\"noptotal\" COLSPAN=2 ALIGN=RIGHT><B>" + MonetarySymbol + strShipping + "</B></TD>";
         strOutput += "</TR>";
      }

      if ( DisplayTaxRow || TaxByRegion ) {
         strOutput += "<TR><TD CLASS=\"noptotal\" COLSPAN=3><B>"+strTAX+"</B></TD>";
         strOutput += "<TD CLASS=\"noptotal\" COLSPAN=2 ALIGN=RIGHT><B>" + MonetarySymbol + strTax + "</B></TD>";
         strOutput += "</TR>";
      }

      strOutput += "<TR><TD CLASS=\"noptotal\" COLSPAN=3><B>"+strTOT+"</B></TD>";
      strOutput += "<TD CLASS=\"noptotal\" COLSPAN=2 ALIGN=RIGHT><B>" + MonetarySymbol + moneyFormat((fTotal + fShipping + fTax)) + "</B></TD>";
      strOutput += "</TR>";

      strOutput += "</TABLE>";


      if ( PaymentProcessor == 'an') {
         //Process this for Authorize.net WebConnect
         strOutput += "<input type=hidden name=\"x_Version\" value=\"3.0\">";
         strOutput += "<input type=hidden name=\"x_Show_Form\" value=\"PAYMENT_FORM\">";
         strOutput += "<input type=hidden name=\"x_Description\" value=\""+ strPP + "\">";
         strOutput += "<input type=hidden name=\"x_Amount\" value=\""+ moneyFormat((fTotal + fShipping + fTax)) + "\">";
      } else if ( PaymentProcessor == 'wp') {
         //Process this for WorldPay
         strOutput += "<input type=hidden name=\"desc\" value=\""+ strPP + "\">";
         strOutput += "<input type=hidden name=\"amount\" value=\""+ moneyFormat((fTotal + fShipping + fTax)) + "\">";
      } else if ( PaymentProcessor == 'lp') {
         //Process this for LinkPoint         
         strOutput += "<input type=hidden name=\"mode\" value=\"fullpay\">";
         strOutput += "<input type=hidden name=\"chargetotal\" value=\""+ moneyFormat((fTotal + fShipping + fTax)) + "\">";
         strOutput += "<input type=hidden name=\"tax\" value=\""+ MonetarySymbol + strTax + "\">";
         strOutput += "<input type=hidden name=\"subtotal\" value=\""+ MonetarySymbol + strTotal + "\">";
         strOutput += "<input type=hidden name=\"shipping\" value=\""+ MonetarySymbol + strShipping + "\">";
         strOutput += "<input type=hidden name=\"desc\" value=\""+ strPP + "\">";
      } else {
         strOutput += "<input type=hidden name=\""+OutputOrderSubtotal+"\" value=\""+ MonetarySymbol + strTotal + "\">";
         strOutput += "<input type=hidden name=\""+OutputOrderShipping+"\" value=\""+ MonetarySymbol + strShipping + "\">";
         strOutput += "<input type=hidden name=\""+OutputOrderTax+"\"      value=\""+ MonetarySymbol + strTax + "\">";
         strOutput += "<input type=hidden name=\""+OutputOrderTotal+"\"    value=\""+ MonetarySymbol + moneyFormat((fTotal + fShipping + fTax)) + "\">";
      }
   }

   document.write(strOutput);


   document.close();
}

谢谢你们的帮助,你们已经帮我了解了一下。我不能等到我对编程更加熟悉并能立即解决这些问题。

1 个答案:

答案 0 :(得分:2)

更新

根据我们的讨论,这是您正在寻找的代码(或类似的代码)?

// Return a string containing all of the items in the cart,
// one line per item with a newline character after each line,
// and the fields in each line separated by | characters.
function getCartString() {
    var result = '',
        count = GetCookie('NumberOrdered') || 0;
    for( var i = 1;  i <= count;  i++ ) {
        result += GetCookie( 'Order.' + i ) + '\n';
    }
    return result;
}

这段代码中有什么东西你不明白吗?我很乐意解释它。它产生的格式是你想要的吗?如果不是,请详细描述您想要的格式,生成它应该是微不足道的 - 现在来自nopcart.js罚款的可怕代码已经消失,所以它不会让我们所有人感到困惑。

原始答案

你写的代码太多了!当你有这么多的重复时,很难遵循,也很容易出错。

编辑:这个笑话在我身上!这不是你写的代码,它不是你的教授或任何人喜欢写的代码,它是“动态HTML”时代留下的一些可怕的脚本。不过,对于那些必须处理这样的脚本的人来说,以下内容可能会有一两个有用的提示。 : - )

现在最重要的是如何简化代码。特别突出的两个领域是:

  1. 尽可能使用内置JavaScript功能而非手动编码。
  2. 每当您发现自己重复一段代码时,请将其变为函数。
  3. 以下是一个例子:

    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
    

    该代码可以替换为:

    var database = GetCookie(NewOrder);
    var fields = database.split('|');
    

    是的,真的!这完全是一回事。 (嗯,不完全正确。正如您所看到的,它不会创建所有Token0Token1等变量,但这些变量在其他任何地方都不会被使用。)

    此外,代替描述fields[0]fields[1]等含义的所有评论,您应该完全删除这些评论,而是为所有这些字段创建命名变量:

    var database = GetCookie(NewOrder);
    var fields = database.split('|'),
        productID = fields[0],
        quantity = parseInt( fields[1], 10 ),
        price = +fields[2],
        description = fields[3],
        shipping = +fields[4],
        moreInfo = fields[5];
    

    然后在其余代码中,您可以用这些名称替换所有fields[n]引用。例如,此代码:

     fTotal += ( parseInt(fields[1]) * parseFloat(fields[2]) );
    

    可以替换为:

     fTotal += quantity * price;
    

    这种方式更具可读性,不是吗?

    相关说明:

    1. 如果您使用parseInt()始终会在第二个参数中提供明确的基础。否则,如果用户碰巧输入“010”,它将被转换为8,这可能不是预期的。
    2. 为简单起见,您可以使用+someString代替parseFloat(someString)
    3. 现在我将留下一些作为锻炼的东西。查看您拥有此类代码的所有地方,重复几次,但有很小的变化:

      strOutput += "<TR><TD CLASS=\"noptotal\" COLSPAN=3><B>"+strSUB+"</B></TD>";
      strOutput += "<TD CLASS=\"noptotal\" COLSPAN=2 ALIGN=RIGHT><B>" + MonetarySymbol + strTotal + "</B></TD>";
      strOutput += "</TR>";
      

      你能想到一种方法,你可以编写代码一次并多次调用它,传入为每个实例更改的项目吗?例如,在重复此代码时,除了您放入strSUBSymbol + strTotal的位置外,每次都是相同的。代码重复的下一个位置是,在这些位置有strSHIPMonetarySymbol + strShipping但是相同。等等。

      在JavaScript中是否有办法编写可重用的代码片段,您可以在其中传递这样的参数,因此大部分代码只出现一次并为每个实例提供参数?

      最后,一个轻微的风格笔记。您的部分HTML有<lowercasetags>,有些还有<UPPERCASETAGS>。浏览器无论如何都会以相同的方式对待它们,但最好保持一致,现代JavaScript / HTML代码使用<lowercasetags>