Jump to content

[SOLVED] inserting loop in Email() body


vinpkl

Recommended Posts

hi all

 

i have an created email function that sends the product information from shopping cart to the customer.

 

i have inserted loop outside the body of email that is showing all the results of products in loop corectly.

 

But i m not able to send that loop results in the email as i m not able to insert that loop in the email body. my email script is here

 


<?php
if(isset($_REQUEST['submit']))
{
$qry_p="select * from order_detail_table where customer_name='$log_id'";
$result_p=mysql_query($qry_p);
$row_p=mysql_fetch_array($result_p);
while($row_p=mysql_fetch_array($result_p))
{
$product_name=$row_p['product_name'];
$quantity=$row_p['quantity'];
$shipping=$row_p['shipping'];
$price=$row_p['price'];
$total_cost=$row_p['total_cost'];
}

$to= $customer_log_id;
$subject= "Shopping Order details with gadgetsonline.co.nz";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:vineet@domain.com' . "\r\n";


$body="This is a detail bill.".  " \n<br><br>
-----------------------------------------------------------" ." \n<br>
Product Details : ". " \n<br>
Product Name = " .$product_name . " \n\n<br>
Quantity = " .$quantity . " \n<br>
Shipping cost= ".$shipping. " \n<br>
Price= ".$price. " \n<br>
Total cost= ".$total_cost. " \n<br>
Payment Status =". "Pending";

if(mail($to,$subject,$body,$headers))
$msg2= "order mail sent!"; 
else
$msg2= "order mail not sent!";	
}	
?>

Link to comment
Share on other sites

Hi vinpkl,

 

You should include some error checks in you code to ensure that it's all working properly, for example;

<?php
$qry_p="select * from order_detail_table where customer_name='$log_id'";
$result_p=mysql_query($qry_p);
if (!$result_p) die('Invalid query: ' . mysql_error());
?>

 

Try adding this and make sure it runs properly. Do you mean that the email currently has no product details when sent?

Link to comment
Share on other sites

Hi vinpkl,

 

You should include some error checks in you code to ensure that it's all working properly, for example;

<?php
$qry_p="select * from order_detail_table where customer_name='$log_id'";
$result_p=mysql_query($qry_p);
if (!$result_p) die('Invalid query: ' . mysql_error());
?>

 

Try adding this and make sure it runs properly. Do you mean that the email currently has no product details when sent?

 

hi

 

thanks for reply. i wil include that in my script.

The email at present send information of only one product.

 

<?php
$qry_p="select * from order_detail_table where customer_name='$log_id'";
$result_p=mysql_query($qry_p)or die (mysql_error()); 
$row_p=mysql_fetch_array($result_p);
while($row_p=mysql_fetch_array($result_p))
{
$product_name=$row_p['product_name'];
$quantity=$row_p['quantity'];
$shipping=$row_p['shipping'];
$price=$row_p['price'];
$total_cost=$row_p['total_cost'];
}
?>

 

vineet

Link to comment
Share on other sites

If you want to get all your products in noe email you're going to need to do a few changes to your code, try the code bellow and see if it works for you;

 

<?php
if(isset($_REQUEST['submit']))
{
$qry_p="select * from order_detail_table where customer_name='$log_id'";
$result_p=mysql_query($qry_p)or die (mysql_error());
$row_p=mysql_fetch_array($result_p);
while($row_p=mysql_fetch_array($result_p))
{
$array_p['product_name'][]=$row_p['product_name'];
$array_p['quantity'][]=$row_p['quantity'];
$array_p['shipping'][]=$row_p['shipping'];
$array_p['price'][]=$row_p['price'];
$array_p['total_cost'][]=$row_p['total_cost'];
}

$absolute_total = 0;         
for($i=0;$i<count($array_p['total_cost']);$i++)
{
$absolute_total += $array_p['total_cost'][$i];
}

$to= $customer_log_id;
$subject= "Shopping Order details with gadgetsonline.co.nz";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:vineet@domain.com' . "\r\n";

                  
$body="This is a detail bill.".."\n<br><br>
-----------------------------------------------------------".." \n<br>";
for($j=0;$j<count($array_p['product_name']);$j++)
{
$body .= "Product Details : \n<br>
Product Name = ".$array_p['product_name'][$j]." \n\n<br>
Quantity = ".$array_p['quantity'][$j]." \n<br>
Shipping cost= ".$array_p['shipping'][$j]." \n<br>
Price= ".$array_p['price'][$j]." \n<br>";
}

$body .= "Total cost= ".$absolute_total." \n<br>
Payment Status = Pending";
         
if(mail($to,$subject,$body,$headers))
$msg2= "order mail sent!"; 
else
$msg2= "order mail not sent!";   
}   
?>

 

Give that a try, hopefully it will work ok for you.

Link to comment
Share on other sites

hi gevans

 

i would like to ask you one more question.

I have 3 row results in my order table and i when i echo them in the loop then i get only two row result with same query for three.

 

But when i write

<?php
echo mysql_num_rows($result_p);
?>

then i get result as 3. That is corect number of rows. If this is corect then why i am not able to print 3 rows. i m able to print only 2 rows.

 

This is my code without email. just for test

<?php
$qry_p="select * from order_detail_table where customer_name='$log_id'";
$result_p=mysql_query($qry_p);
$row_p=mysql_fetch_array($result_p);

//echo mysql_num_rows($result_p); //this is giving me corect result.

while($row_p=mysql_fetch_array($result_p))
{
$product_name=$row_p['product_name'];
$quantity=$row_p['quantity'];
$shipping=$row_p['shipping'];
$price=$row_p['price'];
$total_cost=$row_p['total_cost'];
//echo $qry_p ."<br>". $product_name.",".$quantity.",".$shipping.",".$price."<br>";
}

?>

 

what could be the reason and how can i find out.

 

vineet

 

Link to comment
Share on other sites

hi gevans

 

i would like to ask you one more question.

I have 3 row results in my order table and i when i echo them in the loop then i get only two row result with same query for three.

 

But when i write

<?php
echo mysql_num_rows($result_p);
?>

then i get result as 3. That is corect number of rows. If this is corect then why i am not able to print 3 rows. i m able to print only 2 rows.

 

This is my code without email. just for test

<?php
$qry_p="select * from order_detail_table where customer_name='$log_id'";
$result_p=mysql_query($qry_p);
$row_p=mysql_fetch_array($result_p);

//echo mysql_num_rows($result_p); //this is giving me corect result.

while($row_p=mysql_fetch_array($result_p))
{
$product_name=$row_p['product_name'];
$quantity=$row_p['quantity'];
$shipping=$row_p['shipping'];
$price=$row_p['price'];
$total_cost=$row_p['total_cost'];
//echo $qry_p ."<br>". $product_name.",".$quantity.",".$shipping.",".$price."<br>";
}

?>

 

what could be the reason and how can i find out.

 

vineet

 

 

SO when you run the above script you only get two rows from your database prinnitng?

Link to comment
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.