...

View Full Version : problem with 'for' loop....



chump2877
06-25-2005, 08:45 AM
Hi people, I wrote a PHP script for a shopping cart awhile ago, and now I'm having trouble with it.....i believe I've isolated the problem to the following 'for' loop:


$ref_no = $index_no;
$total = 0;



for ($i = 1; $i <= $ref_no; $i++)
{
$query = "SELECT product_ID FROM ".$_SESSION['username']." WHERE ref_no = ".$i."";
$result = mysql_query($query);
if (mysql_num_rows($result) >= '1')
{
while ($row = mysql_fetch_assoc($result))
{
$product_ID[$i] = $row['product_ID'];
}
}
if (mysql_errno())
{
die("<br>" . mysql_errno() . ": " . mysql_error() . "<br>");
}

$queryA = "SELECT quantity FROM ".$_SESSION['username']." WHERE ref_no = ".$i."";
$resultA = mysql_query($queryA);
if (mysql_num_rows($resultA) >= '1')
{
while ($rowA = mysql_fetch_assoc($resultA))
{
$quantity[$i] = $rowA['quantity'];
settype($quantity[$i], "integer");
}
}
if (mysql_errno())
{
die("<br>" . mysql_errno() . ": " . mysql_error() . "<br>");
}

$queryB = "SELECT price FROM parts WHERE product_code LIKE '%$product_ID[$i]%'";
$resultB = mysql_query($queryB);
if (mysql_num_rows($resultB) >= '1')
{
while ($rowB = mysql_fetch_assoc($resultB))
{
$price[$i] = $rowB['price'];
settype($price[$i], "float");
$price2[$i] = number_format($price[$i], 2);
}
}
if (mysql_errno())
{
die("<br>" . mysql_errno() . ": " . mysql_error() . "<br>");
}


$subtotal[$i] = $price[$i] * $quantity[$i];
$subtotal2[$i] = number_format($subtotal[$i], 2);


$total = $total + $subtotal[$i];

$body2 .= "Customer, with username ".$_SESSION['username'].", has ordered ".$quantity[$i]." item(s) with product code ".$product_ID[$i].". Each item costs $".$price2[$i].", and the subtotal for item(s) is $".$subtotal2[$i].".\n\n";

$list_products .= $i . ") " . substr($product_ID[$i], 5) . "\n\n";
}


$total2 = number_format($total, 2);

OK, now here is what I'm doing with this code.....$index_no is the number (set as type 'integer') of items in the customer's shopping cart....Let's say, for instance, that $index_no = 3......I've set $ref_no = $index_no, so naturally $ref_no = 3 as well......

The 'for' loop is designed to cycle through the different items in a person's shopping cart....In each cycle through the loop, the script identifies quantity, price, and product numbers of an item, calculates the subtotal for this item, adds the subtotal to a running total amount, and integrates the item information into 2 different strings ($body2 and $list_products) that will later be used in the bodies of 2 separate e-mails....So, anyway, if $ref_no = 3, then the loop does all of this 3 times.....

After the 'for' loop runs its course, I now have 2 strings and a grand total figure that I can use for my emails.....So then I set up my e-mails:


// Send the first e-mail.


$mail_to = $_POST['mail_to'];
$mail_to_3 = $_POST['mail_to_3'];
$subject = $_POST['subject'];



$body .= "New component order request !!\n\n";

$body .= "Billing Address:\n\n";

$body .= "First Name: " . $first_name . "\n";
$body .= "Last Name: " . $last_name . "\n";
$body .= "Address: " . $address . "\n";
$body .= "Suite #: " . $suite . "\n";
$body .= "City: " . $city . "\n";
$body .= "State: " . $state . "\n";
$body .= "ZIP: " . $zip . "\n\n";

$body .= "Pick Up:\n\n";

if ($pick_up_yes == 1)
{
$body .= "YES\n\n";
}
elseif ($pick_up_no == 1)
{
$body .= "NO\n\n";
}

$body .= "Shipping Address:\n\n";

$body .= "Address: " . $address2 . "\n";
$body .= "Suite #: " . $suite2 . "\n";
$body .= "City: " . $city2 . "\n";
$body .= "State: " . $state2 . "\n";
$body .= "ZIP: " . $zip2 . "\n\n";

$body .= "Primary E-mail Address:\n\n";

$body .= "Email: " . $email . "\n\n";

$body .= "Phone Number:\n\n";

$body .= "Phone: " . $phone . "\n\n\n\n";



$body2 .= "Grand Total is $" . $total2 . ".\n\n\n\n";

$body2 .= "Additional comments or concerns:\n\n";

$body2 .= stripslashes($comments) . "\n\n";

$body2 .= "Subscribe to Newsletter:\n\n";

if (isset($newsletter))
{
$body2 .= "YES\n\n\n\n";
}
else
{
$body2 .= "NO\n\n\n\n";
}



$body3 = $body . $body2;


$subject .= " - " . date("m.d.y.h.i.s.A");
$subject .= "." . rand(1, 9999);


mail($mail_to, $subject, $body3);






// Send the second e-mail.


$body4 .= "Hi xxxxxxx,\n\n";

$body4 .= "This is an automatically generated e-mail sent to inform you that I have just received an order request for the following components (indicated here by their product codes):\n\n";

$body4 .= $list_products;

$body4 .= "Could you please e-mail me at \"Admin@xxxxxxxx.com\" and let me know if these items are currently in stock and available for purchase. Please DO NOT REPLY directly to this message.\n\n";

$body4 .= "Thank you for your assistance.\n\n";

$body4 .= "xxxxxx\n";

$body4 .= "xxxxx xxxxxx";



mail($mail_to_3, $subject, $body4);

So this part of the script sends 2 different e-mails, incorporating the strings $body2 and $list_products (that were generated during the 'for' loop) into the e-mail messages....Fine, you're probably saying to yourself, seems to make sense....

Well this is the output of the first e-mail:


New component order request !!

Billing Address:

First Name: dfhdf
Last Name: hdfh
Address: dfhd
Suite #: fhdf
City: hdf
State: hd
ZIP: dfh

Pick Up:

NO

Shipping Address:

Address: dfh
Suite #: dfh
City: dfh
State: df
ZIP: dfh

Primary E-mail Address:

Email: dfh

Phone Number:

Phone: dfh



Grand Total is $0.00.



Additional comments or concerns:

dfh dfh ddfh dfh dfh

Subscribe to Newsletter:

NO

Notice that the portion of $body2 from the 'for' loop is clearly missing in the message....Also, notice that $total2 is returning the value of '0'.....This is also incorrect, given that there were 3 items in the cart to begin with ($ref_no = 3)....

This is the output of the second e-mail:


Hi xxxxxx,

This is an automatically generated e-mail sent to inform you that I have just received an order request for the following components (indicated here by their product codes):

Could you please e-mail me at "Admin@xxxxxxxx.com" and let me know if these items are currently in stock and available for purchase. Please DO NOT REPLY directly to this message.

Thank you for your assistance.

xxxxxxx
xxxxxx xxxxxx

Similar to before, notice that $list_products from the 'for' loop is clearly missing in the message....

The way I see it, one of 3 things have to be happening here: 1) There is something wrong with the for loop, and it is never being executed, or 2) the "for-loop-generated" strings (represented by $body2 and $list_products) are incorrectly placed in the body of my e-mails, or 3) both of these things could be the problem......But, of course, I'm obviously missing something, so it may be none of these things.... :eek:

Can anybody help me with this?? My eyes are seeing double from looking at the screen for too long...... :(

Thanks in advance.


Edit: Also, even though you don;t see it in the code here, I have opened the database in the preceding code...so the database is OPEN, and that shouldn't be part of the problem....AND, the $_SESSION variables have been set and the session is active....

chump2877
06-25-2005, 08:13 PM
OK, I inserted this code after all of the preceding code:


// error checking

echo "\$body2 = " . $body2 . "<br><br>";
echo "\$body3 = " . $body3 . "<br><br>";
echo "\$list_products = " . $list_products . "<br><br>";
echo "\$body4 = " . $body4 . "<br><br>";

die();

All these variables output just fine....but for some reason the first part of $body2 and all of $list_products (both are contained in the 'for' loop) are still not showing up in my e-mails....why?

For example, what's wrong with the placement of $list_products in this code:


// Send the second e-mail.


$body4 .= "Hi xxxxxxx,\n\n";

$body4 .= "This is an automatically generated e-mail sent to inform you that I have just received an order request for the following components (indicated here by their product codes):\n\n";

$body4 .= $list_products;

$body4 .= "Could you please e-mail me at \"Admin@xxxxxxxxxx.com\" and let me know if these items are currently in stock and available for purchase. Please DO NOT REPLY directly to this message.\n\n";

$body4 .= "Thank you for your assistance.\n\n";

$body4 .= "xxxxxxx\n";

$body4 .= "xxxxxx xxxxxx";


mail($mail_to, $subject, $body4);

Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum