Page 1 of 2

Emailing cart contents to customer

Posted: Wed Mar 09, 2005 8:19 pm
by squatchimo
I've been working on this for some time. This is the last stage of checkout. Email arrives fine but doesn't display the results from the loop.

Code: Select all

} else {

$get_cart = "select tr.id, inv.name, inv.price, tr.qty from tracking 
as tr left join inventory as inv on inv.id = tr.item_id where session_id = '$userses'";
$get_cart_res = mysql_query($get_cart) or die(mysql_error());
		
    $subject = "blah blah blah...";

    $message = "Dear $firstname $lastname, 
    Thank you for ordering at blah.com! 

	Order Summary:\n";
	$message .= "************************";
	while ($cart = mysql_fetch_array($get_cart_res)) {
   	$name = stripslashes($cart['name']);
   	$price = $cart['price'];
   	$qty = $cart['qty'];
    $message .= "$qty. x .$name. - £.$price. each\n";
    }
	$message .= "************************
	Subtotal: $subtotal
	Tax: $tax
	Shipping: $shipping
	Total: $total
	
	Blah blah blah...";

mail($email, $subject, $message, "From: blah@blah.com\r\n");
header("Location: https://-URL-");
}

feyd | hey look ma,

Code: Select all

works now. [/color]
????  (feyd: what's this for?)

Posted: Wed Mar 09, 2005 8:31 pm
by feyd
have you established that the query does indeed return data? echo or var_export() $get_cart to be sure the query is correct.

Posted: Thu Mar 10, 2005 7:48 am
by squatchimo
Yes, it is the same query I run on another page to display the cart.

Posted: Thu Mar 10, 2005 8:39 am
by feyd
the only way the loop stuff won't show is there there was an error in the query, or no records returned.

Posted: Thu Mar 10, 2005 8:43 am
by Chris Corbyn
Why dont you just print_r($cart) and var_export($get_cart) like feyd said to be double sure? Could save a lot of time and keep some hairs on your head before you pull them out :lol:

Posted: Thu Mar 10, 2005 8:50 am
by squatchimo
I was mainly hoping that one of you may spot an error in the code shown. I guess the code must be fine, I'll try what you two suggested next. Thanks for the help guys! :)

Posted: Thu Mar 10, 2005 10:43 am
by squatchimo
I used the following code:

Code: Select all

$get_cart = "select tr.id, inv.name, inv.price, tr.qty from tracker 
as tr left join inventory as inv on inv.id = tr.item_id where session_id = '$userses'";
$get_cart_res = mysql_query($get_cart) or die(mysql_error());
print_r($get_cart_res);
var_export($get_cart_res);
And it returns:
"Resource id #3NULL"

I assume that means the query is returning data? So why won't it display?

Posted: Thu Mar 10, 2005 10:47 am
by squatchimo
Then I tried:

Code: Select all

$get_cart = "select tr.id, inv.name, inv.price, tr.qty from tracker 
as tr left join inventory as inv on inv.id = tr.item_id where session_id = '$userses'";
$get_cart_res = mysql_query($get_cart) or die(mysql_error());
	while ($cart = mysql_fetch_array($get_cart_res)) {
print_r($cart);
var_export($cart);
    }
And it returns a blank page. No errors...just nothing.

Posted: Thu Mar 10, 2005 10:53 am
by squatchimo
And finally, I tried:

Code: Select all

$get_cart = "select tr.id, inv.name, inv.price, tr.qty from tracker
as tr left join inventory as inv on inv.id = tr.item_id where session_id = '$userses'";
$get_cart_res = mysql_query($get_cart) or die(mysql_error());
	while ($cart = mysql_fetch_array($get_cart_res)) {
   	$name = stripslashes($cart['name']);
   	$price = $cart['price'];
   	$qty = $cart['qty'];
    $message .= "$qty. x .$name. - £.$price. each\n";
    }
print_r($cart);
var_export($cart);
This returns:
"false"

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Mar 10, 2005 10:54 am
by Chris Corbyn

Code: Select all

$cart = mysql_fetch_array($get_cart_res); //Doesn't contain any data since print_r is blank
Looks like $cart is empty. Tracking back your query probably returns nothing. Look through the mysql related parts of your code to see if you can spot where it'sa broken. I don't think it's the loop itself, it's what's going into the loop (nothing) :wink:

Posted: Thu Mar 10, 2005 11:08 am
by squatchimo
OK, to test that thought, I modified the code to echo one of the results:

Code: Select all

$num = 160;
$get_cart = "select tr.id, inv.name, inv.price, tr.qty from tracker 
as tr left join inventory as inv on inv.id = tr.item_id where tr.id = '$num'";
$get_cart_res = mysql_query($get_cart) or die(mysql_error());
	while ($cart = mysql_fetch_array($get_cart_res)) {
   	$name = stripslashes($cart['name']);
   	$price = $cart['price'];
   	$qty = $cart['qty'];
    $message .= "$qty. x .$name. - £.$price. each\n";
    }
	
echo $name;
And the proper name is displayed. So the query does work and does return results. $cart is not empty.

//Feyd: I am using the

Code: Select all

brackets as specified...whatare you commenting on?

Posted: Thu Mar 10, 2005 12:21 pm
by squatchimo
The code is correct, the query DOES pull data, but it won't display when I try to include it in the $message variable. It displays fine if I just echo the results...but not if it's mailed.

I've been searching everywhere for an answer to this problem but have not found one yet. Anybody have any guesses?

Posted: Fri Mar 11, 2005 8:05 am
by feyd
There was no reason to bump the thread.

Break the message addition down.. add only some bits, see if that works. Get basic on it. You may find that the pound mark is the problem.

Posted: Fri Mar 11, 2005 8:11 am
by Chris Corbyn
feyd wrote:You may find that the pound mark is the problem.
How did none of us see that? LOL

Re: Emailing cart contents to customer

Posted: Fri Mar 11, 2005 9:31 am
by squatchimo
I thought of that and modified the code to return only one variable like so:

Code: Select all

} else {

$get_cart = "select tr.id, inv.name, inv.price, tr.qty from tracking 
as tr left join inventory as inv on inv.id = tr.item_id where session_id = '$userses'";
$get_cart_res = mysql_query($get_cart) or die(mysql_error());
		
    $subject = "blah blah blah...";

    $message = "Dear $firstname $lastname, 
    Thank you for ordering at blah.com! 

	Order Summary:\n";
	$message .= "************************";
	while ($cart = mysql_fetch_array($get_cart_res)) {
   	$name = stripslashes($cart['name']);
   	$price = $cart['price'];
   	$qty = $cart['qty'];
    $message .= "$qty";
    }
	$message .= "************************
	Subtotal: $subtotal
	Tax: $tax
	Shipping: $shipping
	Total: $total
	
	Blah blah blah...";

mail($email, $subject, $message, "From: blah@blah.com\r\n");
header("Location: https://-URL-");
}
When the email comes through, the first part of the message is fine but the contents from the loop (now just quantity) does not appear, but the subtotal, tax, etc. DO appear. So what am I missing?