Jump to content

Recommended Posts

Below is my javascript.  It's for printing out coupons.  As you can see I'm manually specifying if ss_quantity equals 1 do the 8 document.write stuff which prints out the coupon and if ss_quantity equals 2 do the 8 document.write stuff times 2.  In my real script I have this up to 10 but I want to do a loop for this so that if the quantity is 2 it loops through the coupon twice, if 3 it loops 3 times, etc.  I think I need to do nested "for" loops but I'm pretty new to this stuff and would like some help.  Thanks.

 

<script type="text/javascript">

<!--

 

for (i = 0; i < number_products; i++) {

        if (ss_weight == 0.000000 && ss_quantity == 1) {

 

        document.write ("<div id=\"coupon1\"><img src=\"design/coupon.png\"></div><div id=\"coupon2\">");

                document.write(ss_name);

        document.write("</div>");

        document.write ("<div id=\"coupon3\">Order Number:");

        document.write (ss_ordernum);

        document.write ("&nbsp&nbsp&nbsp&nbsp&nbspExpires: ");

                document.write(Date.today().add({months: 6}).toString("M-d-yyyy"));

        document.write ("</div><br><br><br><br><br><br><br>");

 

}

else if (ss_weight == 0.000000 && ss_quantity == 2) {

 

        document.write ("<div id=\"coupon1\"><img src=\"design/coupon.png\"></div><div id=\"coupon2\">");

                document.write(ss_name);

        document.write("</div>");

        document.write ("<div id=\"coupon3\">Order Number:");

        document.write (ss_ordernum);

        document.write ("&nbsp&nbsp&nbsp&nbsp&nbspExpires: ");

                document.write(Date.today().add({months: 6}).toString("M-d-yyyy"));

        document.write ("</div><br><br><br><br><br><br><br>");

        document.write ("<div id=\"coupon1\"><img src=\"design/coupon.png\"></div><div id=\"coupon2\">");

                document.write(ss_name);

        document.write("</div>");

        document.write ("<div id=\"coupon3\">Order Number:");

        document.write (ss_ordernum);

        document.write ("&nbsp&nbsp&nbsp&nbsp&nbspExpires: ");

                document.write(Date.today().add({months: 6}).toString("M-d-yyyy"));

        document.write ("</div><br><br><br><br><br><br><br>");

 

}

else{

        }

}

 

// -->

</script>

 

Link to comment
https://forums.phpfreaks.com/topic/262492-for-loop-help/
Share on other sites

It might be easier to see it in action.  If you view the page source you should be able to see what I'm trying to do.  I have products that are shipped that I don't want to print out a coupon for which is why I have the last else statement.  Here's a link of it in action:

 

https://www.spaenvydeals.com/Deals/sc/testing11.html

 

Link to comment
https://forums.phpfreaks.com/topic/262492-for-loop-help/#findComment-1345230
Share on other sites

I think your kinda missing the point of the for loop and automation in general. The theory behind it is, write it once, then loop over it x amount of times. So basically you shouldn't need that massive if/else statement inside of your for loop.

 

Here's how it should look:

<script>
var i, x;
var products = new Array;

//you'll probably be creating the array with php, but here's a hard coded one
products['ordernum'] = 12345678;
products[0] = new Array;
products[0]['name'] = 'Name1';
products[0]['quantity'] = 1;
products[0]['weight'] = 0.000000;
products[1] = new Array;
products[1]['name'] = 'Name2';
products[1]['quantity'] = 1;
products[1]['weight'] = 0.000000;
products[2] = new Array;
products[2]['name'] = 'Name3';
products[2]['quantity'] = 1;
products[2]['weight'] = 0.000000;

for(i in products){
        if( products[i]['weight'] == 0.000000 ){
                x = 0;
                for(x; x < products[i]['quantity']; x++){
                        document.write ("<div id=\"coupon1\"><img src=\"design/coupon.png\"></div><div id=\"coupon2\">");
                        document.write(products[i]['name']);
                        document.write("</div>");
                        document.write ("<div id=\"coupon3\">Order Number:");
                        document.write (products['ordernum']);
                        document.write ("&nbsp&nbsp&nbsp&nbsp&nbspExpires: ");
                        document.write(Date.today().add({months: 6}).toString("M-d-yyyy"));
                        document.write ("</div><br><br><br><br><br><br><br>");
                }
        }
}
</script>

 

Hopefully that will give you a better idea of how to better organize your loops and products array. Let me know if you have anymore questions on it.

Link to comment
https://forums.phpfreaks.com/topic/262492-for-loop-help/#findComment-1345242
Share on other sites

I found some bugs in my last code sample, this one is tested and working:

<html>
<body>
<script>
var products = new Array;
var expires  = new Date();
var dd = expires.getDate();
var mm = expires.getMonth()+7; //January is 0!
var yyyy = expires.getFullYear();
if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} expires = mm+'/'+dd+'/'+yyyy;

//you'll probably be creating the array with php, but here's a hard coded one
products['ordernum'] = 12345678;
products[0] = new Array;
products[0]['name'] = 'Name1';
products[0]['quantity'] = 2;
products[0]['weight'] = 0.000000;
products[1] = new Array;
products[1]['name'] = 'Name2';
products[1]['quantity'] = 1;
products[1]['weight'] = 0.000000;
products[2] = new Array;
products[2]['name'] = 'Name3';
products[2]['quantity'] = 1;
products[2]['weight'] = 0.000000;

var output = '';
for(var i in products){
        if( products[i]['weight'] == 0.000000 ){
                for(x=0; x < products[i]['quantity']; x++){
                        output = output + "<div id=\"coupon1\"><img src=\"design/coupon.png\"></div><div id=\"coupon2\">";
                        output = output + products[i]['name'];
                        output = output + "</div>";
                        output = output + "<div id=\"coupon3\">Order Number:";
                        output = output + products['ordernum'];
                        output = output + "&nbsp&nbsp&nbsp&nbsp&nbspExpires: ";
                        output = output + expires;
                        output = output + "</div><br><br><br><br><br><br><br>";
                }
        }
}

document.write( output );
</script>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/262492-for-loop-help/#findComment-1345247
Share on other sites

The code below is the javascript I can't change.  This javascript is written with a CGI script which is really complex.  I might be able to modify it but then I could mess up the shopping cart so I really don't want to try to change it.  Any ideas?  I've been trying to figure this out for a long time.  Thanks!

<script type="text/javascript" language="JavaScript">
<!-- 
  var ss_screen = "Thank You";
  var ss_sbid = "SSMSB1336493648.5134";
  var ss_associate = "Jeff Lodwick";
  var ss_ordernum = "1322";
  var ss_paytype = "99";
  var ss_taxstr = "Colorado Tax";
  var ss_taxpercent = "6.75";
  var ss_shippingstr = "Ground";
  var ss_email = "j_lodwick@hotmail.com";
  var ss_billname = "Jeff Lodwick";
  var ss_address1 = "9551 Dolton Way";
  var ss_address2 = "";
  var ss_city = "Highlands Ranch";
  var ss_state = "CO";
  var ss_postalcode = "80126";
  var ss_country = "United States";
  var ss_company = "";
  var ss_phone = "303-919-1366";
  var ss_ordertotal = "0.00";
  var ss_subtotal = "0.04";
  var ss_taxtotal = "0.00";
  var ss_shiptotal = "5.00";
  var ss_name = new Array;
  var ss_sku = new Array;
  var ss_quantity = new Array;
  var ss_finite_options = new Array;
  var ss_price = new Array;
  var ss_weight = new Array;
  var ss_total_price = new Array;
  var ss_freeform_options = new Array;
  var ss_field1 = new Array;
  var ss_field2 = new Array;
  var ss_field3 = new Array;
  var ss_field4 = new Array;
  var ss_field5 = new Array;
  var ss_field6 = new Array;
  var ss_field7 = new Array;
  var ss_field8 = new Array;
  var ss_field9 = new Array;
  var ss_field10 = new Array;
  var ss_field11 = new Array;
  var ss_field12 = new Array;
  var ss_field13 = new Array;
  var ss_field14 = new Array;
  var ss_field15 = new Array;
  var ss_field16 = new Array;
  var ss_field17 = new Array;
  var ss_field18 = new Array;
  var ss_field19 = new Array;
  var ss_field20 = new Array;
  var ss_field21 = new Array;
  var ss_field22 = new Array;
  var ss_field23 = new Array;
  var ss_field24 = new Array;
  var ss_field25 = new Array;
  ss_name[0]="$.01 for massage at Jeff\'s Massage Company";
  ss_sku[0]="";
  ss_quantity[0]=1;
  ss_finite_options[0]="";
  ss_freeform_options[0]="";
  ss_weight[0]=0.000000;
  ss_price[0]="0.01";
  ss_total_price[0]="0.01";
  ss_field1[0]="";
  ss_field2[0]="";
  ss_field3[0]="";
  ss_field4[0]="";
  ss_field5[0]="";
  ss_field6[0]="";
  ss_field7[0]="";
  ss_field8[0]="";
  ss_field9[0]="";
  ss_field10[0]="";
  ss_field11[0]="";
  ss_field12[0]="";
  ss_field13[0]="";
  ss_field14[0]="";
  ss_field15[0]="";
  ss_field16[0]="";
  ss_field17[0]="";
  ss_field18[0]="";
  ss_field19[0]="";
  ss_field20[0]="";
  ss_field21[0]="";
  ss_field22[0]="";
  ss_field23[0]="";
  ss_field24[0]="";
  ss_field25[0]="";
  ss_name[1]="test-shipping";
  ss_sku[1]="";
  ss_quantity[1]=1;
  ss_finite_options[1]="";
  ss_freeform_options[1]="";
  ss_weight[1]=1.000000;
  ss_price[1]="0.01";
  ss_total_price[1]="0.01";
  ss_field1[1]="";
  ss_field2[1]="";
  ss_field3[1]="";
  ss_field4[1]="";
  ss_field5[1]="";
  ss_field6[1]="";
  ss_field7[1]="";
  ss_field8[1]="";
  ss_field9[1]="";
  ss_field10[1]="";
  ss_field11[1]="";
  ss_field12[1]="";
  ss_field13[1]="";
  ss_field14[1]="";
  ss_field15[1]="";
  ss_field16[1]="";
  ss_field17[1]="";
  ss_field18[1]="";
  ss_field19[1]="";
  ss_field20[1]="";
  ss_field21[1]="";
  ss_field22[1]="";
  ss_field23[1]="";
  ss_field24[1]="";
  ss_field25[1]="";
  ss_name[2]="$.01 for massage at Christy\'s Massage Company";
  ss_sku[2]="";
  ss_quantity[2]=2;
  ss_finite_options[2]="";
  ss_freeform_options[2]="";
  ss_weight[2]=0.000000;
  ss_price[2]="0.01";
  ss_total_price[2]="0.01";
  ss_field1[2]="";
  ss_field2[2]="";
  ss_field3[2]="";
  ss_field4[2]="";
  ss_field5[2]="";
  ss_field6[2]="";
  ss_field7[2]="";
  ss_field8[2]="";
  ss_field9[2]="";
  ss_field10[2]="";
  ss_field11[2]="";
  ss_field12[2]="";
  ss_field13[2]="";
  ss_field14[2]="";
  ss_field15[2]="";
  ss_field16[2]="";
  ss_field17[2]="";
  ss_field18[2]="";
  ss_field19[2]="";
  ss_field20[2]="";
  ss_field21[2]="";
  ss_field22[2]="";
  ss_field23[2]="";
  ss_field24[2]="";
  ss_field25[2]="";
  var number_products=3;
  var number_coupons=0;
// -->
</script>

Link to comment
https://forums.phpfreaks.com/topic/262492-for-loop-help/#findComment-1345520
Share on other sites

The code below is the javascript I can't change.  This javascript is written with a CGI script which is really complex.  I might be able to modify it but then I could mess up the shopping cart so I really don't want to try to change it.  Any ideas?  I've been trying to figure this out for a long time.  Thanks!

<script type="text/javascript" language="JavaScript">
<!-- 
  var ss_screen = "Thank You";
  var ss_sbid = "SSMSB1336493648.5134";
  var ss_associate = "Jeff Lodwick";
  var ss_ordernum = "1322";
  var ss_paytype = "99";
  var ss_taxstr = "Colorado Tax";
  var ss_taxpercent = "6.75";
  var ss_shippingstr = "Ground";
  var ss_email = "j_lodwick@hotmail.com";
  var ss_billname = "Jeff Lodwick";
  var ss_address1 = "9551 Dolton Way";
  var ss_address2 = "";
  var ss_city = "Highlands Ranch";
  var ss_state = "CO";
  var ss_postalcode = "80126";
  var ss_country = "United States";
  var ss_company = "";
  var ss_phone = "303-919-1366";
  var ss_ordertotal = "0.00";
  var ss_subtotal = "0.04";
  var ss_taxtotal = "0.00";
  var ss_shiptotal = "5.00";
  var ss_name = new Array;
  var ss_sku = new Array;
  var ss_quantity = new Array;
  var ss_finite_options = new Array;
  var ss_price = new Array;
  var ss_weight = new Array;
  var ss_total_price = new Array;
  var ss_freeform_options = new Array;
  var ss_field1 = new Array;
  var ss_field2 = new Array;
  var ss_field3 = new Array;
  var ss_field4 = new Array;
  var ss_field5 = new Array;
  var ss_field6 = new Array;
  var ss_field7 = new Array;
  var ss_field8 = new Array;
  var ss_field9 = new Array;
  var ss_field10 = new Array;
  var ss_field11 = new Array;
  var ss_field12 = new Array;
  var ss_field13 = new Array;
  var ss_field14 = new Array;
  var ss_field15 = new Array;
  var ss_field16 = new Array;
  var ss_field17 = new Array;
  var ss_field18 = new Array;
  var ss_field19 = new Array;
  var ss_field20 = new Array;
  var ss_field21 = new Array;
  var ss_field22 = new Array;
  var ss_field23 = new Array;
  var ss_field24 = new Array;
  var ss_field25 = new Array;
  ss_name[0]="$.01 for massage at Jeff\'s Massage Company";
  ss_sku[0]="";
  ss_quantity[0]=1;
  ss_finite_options[0]="";
  ss_freeform_options[0]="";
  ss_weight[0]=0.000000;
  ss_price[0]="0.01";
  ss_total_price[0]="0.01";
  ss_field1[0]="";
  ss_field2[0]="";
  ss_field3[0]="";
  ss_field4[0]="";
  ss_field5[0]="";
  ss_field6[0]="";
  ss_field7[0]="";
  ss_field8[0]="";
  ss_field9[0]="";
  ss_field10[0]="";
  ss_field11[0]="";
  ss_field12[0]="";
  ss_field13[0]="";
  ss_field14[0]="";
  ss_field15[0]="";
  ss_field16[0]="";
  ss_field17[0]="";
  ss_field18[0]="";
  ss_field19[0]="";
  ss_field20[0]="";
  ss_field21[0]="";
  ss_field22[0]="";
  ss_field23[0]="";
  ss_field24[0]="";
  ss_field25[0]="";
  ss_name[1]="test-shipping";
  ss_sku[1]="";
  ss_quantity[1]=1;
  ss_finite_options[1]="";
  ss_freeform_options[1]="";
  ss_weight[1]=1.000000;
  ss_price[1]="0.01";
  ss_total_price[1]="0.01";
  ss_field1[1]="";
  ss_field2[1]="";
  ss_field3[1]="";
  ss_field4[1]="";
  ss_field5[1]="";
  ss_field6[1]="";
  ss_field7[1]="";
  ss_field8[1]="";
  ss_field9[1]="";
  ss_field10[1]="";
  ss_field11[1]="";
  ss_field12[1]="";
  ss_field13[1]="";
  ss_field14[1]="";
  ss_field15[1]="";
  ss_field16[1]="";
  ss_field17[1]="";
  ss_field18[1]="";
  ss_field19[1]="";
  ss_field20[1]="";
  ss_field21[1]="";
  ss_field22[1]="";
  ss_field23[1]="";
  ss_field24[1]="";
  ss_field25[1]="";
  ss_name[2]="$.01 for massage at Christy\'s Massage Company";
  ss_sku[2]="";
  ss_quantity[2]=2;
  ss_finite_options[2]="";
  ss_freeform_options[2]="";
  ss_weight[2]=0.000000;
  ss_price[2]="0.01";
  ss_total_price[2]="0.01";
  ss_field1[2]="";
  ss_field2[2]="";
  ss_field3[2]="";
  ss_field4[2]="";
  ss_field5[2]="";
  ss_field6[2]="";
  ss_field7[2]="";
  ss_field8[2]="";
  ss_field9[2]="";
  ss_field10[2]="";
  ss_field11[2]="";
  ss_field12[2]="";
  ss_field13[2]="";
  ss_field14[2]="";
  ss_field15[2]="";
  ss_field16[2]="";
  ss_field17[2]="";
  ss_field18[2]="";
  ss_field19[2]="";
  ss_field20[2]="";
  ss_field21[2]="";
  ss_field22[2]="";
  ss_field23[2]="";
  ss_field24[2]="";
  ss_field25[2]="";
  var number_products=3;
  var number_coupons=0;
// -->
</script>

It will still work similarly. Since each field is built into an array with corresponding keys.

 

CHeck it:

<html>
<body>
<script>
var ss_ordernum = "1322";
var ss_name = new Array;
var ss_quantity = new Array;
var ss_weight = new Array;
ss_name[0] = 'Name1';
ss_quantity[0] = 2;
ss_weight[0] = 0.000000;
ss_name[1] = 'Name2';
ss_quantity[1] = 1;
ss_weight[1] = 0.000000;
ss_name[2] = 'Name3';
ss_quantity[2] = 1;
ss_weight[2] = 0.000000;
var number_products = 3;

//CODE YOU NEED STARTS HERE
var expires  = new Date();
var dd = expires.getDate();
var mm = expires.getMonth()+7; //January is 0!
var yyyy = expires.getFullYear();
if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} expires = mm+'/'+dd+'/'+yyyy;

var output = '';
for(i in ss_name){
        if( ss_weight[i] == 0.000000 ){
                for(x=0; x < ss_quantity[i]; x++){
                        output = output + "<div id=\"coupon1\"><img src=\"design/coupon.png\"></div><div id=\"coupon2\">";
                        output = output + ss_name[i];
                        output = output + "</div>";
                        output = output + "<div id=\"coupon3\">Order Number:";
                        output = output + ss_ordernum;
                        output = output + "&nbsp&nbsp&nbsp&nbsp&nbspExpires: ";
                        output = output + expires;
                        output = output + "</div><br><br><br><br><br><br><br>";
                }
        }
}

document.write( output );
</script>
</body>
</html>

It's almost exactly the same,(I DIDN'T TEST THIS ONE, BUT IT SHOULD BE OK), the only difference is I was using a multi-dimensional array before, which would have been a little bit more organized in my opinion, but either way works.

 

Let me know how that works out for you.

Link to comment
https://forums.phpfreaks.com/topic/262492-for-loop-help/#findComment-1345655
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.