General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 3rd, 2003, 10:01 AM
Alex Piotto Alex Piotto is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Maputo, Mozambique
Posts: 3 Alex Piotto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Email with items and user data

Hi, this is my first post here!

I am working on a script to follow the "persistent shopping cart" tutorial... I made this:

--------------------------------------------
<?php
if (isset($sendorder)) {
include("db.php");
global $dbServer, $dbUser, $dbPass, $dbName;
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");

while($row = mysql_fetch_array($result)) {

$category[] = $row["category"];
$itemName[] = $row["itemName"];
$itemPrice[] = $row["itemPrice"];
$qty[] = $row["qty"];

$subtotal = $row["qty"] * $row["itemPrice"];
$totalcost += $subtotal;
$iva = ($totalcost/100*17);
$grandtotal = $totalcost + $iva;

$itemPriceformatted[] = number_format($itemPrice, 2, ",", ".");
$subtotalformatted[] = number_format($subtotal, 2, ",", ".");
$totalcostformatted[] = number_format($totalcost, 2, ",", ".");
$ivaformatted[] = number_format($iva, 2, ",", ".");
$grandtotalformatted[] = number_format($grandtotal, 2, ",", ".");
}

$headers .= "Content-Type: text/html; charset=iso-8859-1\n"; // Mime type
$headers .= "From: $email\n";
$headers .= "X-Priority: 1\n"; // Urgent message!

$to = "me@myhost.com";
$subject = "Website ORDER!";

for($i=1; $i<=count($itemPriceFormatted); $i++); {

$body .= "
Quantity = $qty<br>
Category = $category<br>
Model = $itemName<br>
Price = $itemPriceformatted<br>
Subtotal = $subtotalformatted
";
}
$body .= "
--------------------------------<br>
Entreprise = $empresa<br>
Name = $name<br>
Surname = $surname<br>
Address = $address<br>
Telephone = $tel<br>
Cellphone = $cell<br>
E-Mail = $email<br>
Shipment Address = $shipmentaddress<br>
--------------------------------<br>
";
}

if (mail($to, $subject, $body, $headers)) { echo("<p>ok! it's gone</p>"); }
else { echo("<p>Message delivery failed...</p>"); }
?>

----------------------------------------------------

...but this is the sad result in the email body:

Entreprise = myentreprise
Name = myname
Surname = etc
Address = etc
Telephone = etc
Cellphone = etc
E-Mail = etc
Shipment Address = etc
--------------------------------
Quantity = Array
Category = Array
Model = Array
Price = Array
Subtotal = Array
--------------------------------
total = Array
IVA = Array
TOTAL to Client = Array


Why is this happening, and why there is only one item instead of all of them from the cart? User data is ok.


Thanks for any help...
Alex

Reply With Quote
  #2  
Old July 3rd, 2003, 10:34 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
For each row returned from the database, you're resetting each of your arrays (qty[], for example) to the scalar value returned from the database. You should instantiate each of these arrays before entering your db loop and then, for each row in the result set execute array_push for each field and its corresponding array.

Then you can loop through and, for the size of your item stack, print the appropriate value from each array. Some pseudo-code to illustrate:

PHP Code:
 $category=array();
$qty=array();

//DB setup and connection stuff here...

while($row=mysql_fetch_array($result)){
    
array_push($category,$row["category"]);
    
array_push($qty,$row["qty"]);
}

//Do other formatting stuff here...

for($i=0$i<=count($itemPriceFormatted); $i++); { 
    
$body .= "Category: " $category[$i] . "<br>";
    
$body .= "Quantity: " $qty[$i] . "<br>";


Reply With Quote
  #3  
Old July 3rd, 2003, 02:41 PM
Alex Piotto Alex Piotto is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Maputo, Mozambique
Posts: 3 Alex Piotto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
still in trouble...

Hi, dhouston and thans for the answer

I modified the script, but the loop is not working.. I get only the last item in the stack in the email body. What I've done wrong?

-------------------------------------------------

<?php
if (isset($sendorder)) {

$category = array();
$itemName = array();
$itemPrice = array();
$qty = array();

include("db.php");
global $dbServer, $dbUser, $dbPass, $dbName;
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");

while($row = mysql_fetch_array($result)) {

array_push($category, $row["category"]);
array_push($itemName, $row["itemName"]);
array_push($itemPrice, $row["itemPrice"]);
array_push($qty, $row["qty"]);

$subtotal = $row["qty"] * $row["itemPrice"];
$totalcost += $subtotal;
$totalcostformatted = number_format($totalcost, 2, ",", ".");

$iva = ($totalcost/100*17);
$ivaformatted = number_format($iva, 2, ",", ".");

$grandtotal = $totalcost + $iva;
$grandtotalformatted = number_format($grandtotal, 2, ",", ".");

}

$headers .= "Content-Type: text/html; charset=iso-8859-1\n"; // Mime type
$headers .= "From: $email\n";
$headers .= "X-Priority: 1\n"; // Urgent message!

$to = "piotto2000@libero.it";
$subject = "Website ORDER!";

$body .= "
Entreprise = $empresa<br>
Name = $name<br>
Surname = $surname<br>
Address = $address<br>
Telephone = $tel<br>
Cellphone = $cell<br>
E-Mail = $email<br>
Shipment Address = $shipmentaddress<br>
--------------------------------<br>
";

for($i=1; $i<=count($subtotal); $i++); {

$body .= "Quantity: " . $qty[$i] . "<br>";
$body .= "Category: " . $category[$i] . "<br>";
$body .= "Model: " . $itemName[$i] . "<br>";
$body .= "Unit Price: " . number_format($itemPrice[$i], 2, ",", ".") . "<br>";
$body .= "Subtotal: " . number_format($subtotal, 2, ",", ".") . "<br>";
$body .= "--------------------------------<br>";

}

$body .= "
total = $totalcostformatted<br>
IVA = $ivaformatted<br>
TOTAL to Client = $grandtotalformatted
";
}

if (mail($to, $subject, $body, $headers)) { echo("<p>ok! it's gone</p>"); }
else { echo("<p>Message delivery failed...</p>"); }
?>

------------------------------------

All the calculation of the total are ok, and also the user data arrive just fine, but the loop... doesn't loops!

Please, a bit more of help...
Thanks
Alex

Reply With Quote
  #4  
Old July 3rd, 2003, 04:10 PM
Alex Piotto Alex Piotto is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Maputo, Mozambique
Posts: 3 Alex Piotto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
GOT IT!!!!

After burning out half of my brain I found a solution... apparently easier... and it works well!

here is the code...

---------------------------------------------------------<?php
if (isset($sendorder)) {

$body .= "
Entreprise = $empresa<br>
Name = $name<br>
Surname = $surname<br>
Address = $address<br>
Telephone = $tel<br>
Cellphone = $cell<br>
E-Mail = $email<br>
Shipment Address = $shipmentaddress<br>
--------------------------------<br>
";

include("db.php");
global $dbServer, $dbUser, $dbPass, $dbName;
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");

while($row = mysql_fetch_array($result)) {

$category = $row["category"];
$itemName = $row["itemName"];
$itemPrice = $row["itemPrice"];
$qty = $row["qty"];

$itemPriceformatted = number_format($itemPrice, 2, ",", ".");

$subtotal = $row["qty"] * $row["itemPrice"];
$subtotalformatted = number_format($subtotal, 2, ",", ".");

$totalcost += $subtotal;
$totalcostformatted = number_format($totalcost, 2, ",", ".");

$iva = ($totalcost/100*17);
$ivaformatted = number_format($iva, 2, ",", ".");

$grandtotal = $totalcost + $iva;
$grandtotalformatted = number_format($grandtotal, 2, ",", ".");

$body .= "Quantity: " . $qty . "<br>";
$body .= "Category: " . $category . "<br>";
$body .= "Model: " . $itemName . "<br>";
$body .= "Unit Price: " . $itemPriceformatted . "<br>";
$body .= "Subtotal: " . $subtotalformatted . "<br>";
$body .= "--------------------------------<br>";

}

$body .= "
total = $totalcostformatted<br>
IVA = $ivaformatted<br>
TOTAL to Client = $grandtotalformatted
";

$headers .= "Content-Type: text/html; charset=iso-8859-1\n"; // Mime type
$headers .= "From: $email\n";
$headers .= "X-Priority: 1\n"; // Urgent message!

$to = "your emai address";
$subject = "Website ORDER!";

}

if (mail($to, $subject, $body, $headers)) { echo("<p>ok! it's gone</p>"); }
else { echo("<p>Message delivery failed...</p>"); }
?>

----------------------------------------------

ciao )

Alex

Reply With Quote
  #5  
Old July 3rd, 2003, 04:29 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Glad you found a solution. For the record, I think the reason you were only getting the last two results was because you neglected to change the initial value for $i in your for loop. I had changed that to 0, since we were working with array indices.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Email with items and user data


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT