Emailing cart contents to customer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

squatchimo
Forum Commoner
Posts: 28
Joined: Thu Feb 03, 2005 3:36 pm

Emailing cart contents to customer

Post 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?)
Last edited by squatchimo on Thu Mar 10, 2005 7:52 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
squatchimo
Forum Commoner
Posts: 28
Joined: Thu Feb 03, 2005 3:36 pm

Post by squatchimo »

Yes, it is the same query I run on another page to display the cart.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the only way the loop stuff won't show is there there was an error in the query, or no records returned.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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:
squatchimo
Forum Commoner
Posts: 28
Joined: Thu Feb 03, 2005 3:36 pm

Post 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! :)
squatchimo
Forum Commoner
Posts: 28
Joined: Thu Feb 03, 2005 3:36 pm

Post 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?
squatchimo
Forum Commoner
Posts: 28
Joined: Thu Feb 03, 2005 3:36 pm

Post 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.
squatchimo
Forum Commoner
Posts: 28
Joined: Thu Feb 03, 2005 3:36 pm

Post 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]
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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:
squatchimo
Forum Commoner
Posts: 28
Joined: Thu Feb 03, 2005 3:36 pm

Post 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?
squatchimo
Forum Commoner
Posts: 28
Joined: Thu Feb 03, 2005 3:36 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

feyd wrote:You may find that the pound mark is the problem.
How did none of us see that? LOL
squatchimo
Forum Commoner
Posts: 28
Joined: Thu Feb 03, 2005 3:36 pm

Re: Emailing cart contents to customer

Post 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?
Post Reply