|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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:
|
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
||||
|
||||
|
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.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Email with items and user data |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|