Agreaves Posted August 28, 2012 Share Posted August 28, 2012 My shopping cart works fine but I dont know how to put the contents of it in a variable to send via email. Below is the body of webpage. In the middle of the table I have included my shopping cart file that captures the contents of the cart. I just need to know how to format it to send it in an html email. I have tried putting columns in a variable for example $message = '<tr><td></td></tr>'; etc etc, for each line but when I test my cart nothing is displayed in the cart. <body> <table width="1024" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td valign="baseline"></td> <td width="151" align="right" valign="bottom"><table width="150" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="35" align="center"><a href="../index.php" id="btn"><span>Continue Shopping</span></a></td> </tr> </table></td> </tr> <tr> <td height="40" colspan="2" valign="middle" class="bdr" id="heading"><?php echo 'Shopping Cart'; ?></td> </tr> </table> <table width="1024" border="0" align="center" cellpadding="15" cellspacing="" class="main_bdr"> <tr> <td align="center"> <?php include'cart.inc';?> </td> </tr> </table> <table width="1024" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="40" align="center" class="bdr">© <?php echo date("Y");?> MizBones</td> </tr> </table> </body> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 28, 2012 Share Posted August 28, 2012 we are not here to code your stuff for you however i suggest googling how to send html via PHP mail function or how to use Pear mail or php mailer to send text/html messages in email's. Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 28, 2012 Author Share Posted August 28, 2012 Im not asking to code it, im just asking for some advice or suggestions. Whats the purpose of this forum by the way? Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 28, 2012 Share Posted August 28, 2012 I'd say to help you learn how to solve problems That said, this is a great example of when to use templates. I like Smarty. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 28, 2012 Share Posted August 28, 2012 Using Smarty To send templates in emails Using Phpmailer to send emails Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 28, 2012 Author Share Posted August 28, 2012 I installed the smarty but I would like to know if I would have to rewrite my program that creates and displays my shopping cart to incorporate a smarty template with it? Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 28, 2012 Share Posted August 28, 2012 Yes. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 28, 2012 Share Posted August 28, 2012 you don't need to change your cart into a smarty template just create an email template to put the cart around so when it is emailed it shows the cart. look at the first link i posted at the answer using the fetch smarty method. it has examples on how to setup an example email template. Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 29, 2012 Author Share Posted August 29, 2012 My cart is all in php code. How would I use the variable from the cart into the smarty template. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 29, 2012 Share Posted August 29, 2012 paste the code for it and we can tell you how to intergrate it into an email template for smarty. Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 29, 2012 Author Share Posted August 29, 2012 Here is the code for the body of my shopping cart. My database connection is in another file <body> <table width="1024" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td valign="baseline"></td> <td width="151" align="right" valign="bottom"><table width="150" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="35" align="center"><a href="../index.php" id="btn"><span>Continue Shopping</span></a></td> </tr> </table></td> </tr> <tr> <td height="40" colspan="2" valign="middle" class="bdr" id="heading"><?php echo 'Shopping Cart'; ?></td> </tr> </table> <table width="1024" border="0" align="center" cellpadding="15" cellspacing="" class="main_bdr"> <tr> <td align="center"> <?php // Product Id from URL $product_id = $_GET['id']; // Action from URL $action = $_GET['action']; //if there is an product_id and that product_id doesn't exist display an error message if($product_id && !productExists($product_id)) { die("Product Doesn't Exist"); } switch($action) { //decide what to do case "add": $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id break; case "remove": $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise it will show zero, then -1, -2 etc when the user keeps removing items. break; case "empty": unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. break; } ?> <?php if($_SESSION['cart']) { //if the cart isn't empty //show the cart echo "<form method=\"post\" name=\"body\" action=\"$_SERVER[php_SELF]\">"; echo "<table align=\"center\" id=\"tbl_bdr\" width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"10\"> <tr align=\"left\"> <td align=\"left\"><b>Name:</b></td> <td><input id=\"txt_box\" type=\"text\" name=\"name\"/></td> <td align=\"left\"><b>Email:</b></td> <td><input id=\"txt_box\" type=\"text\" name=\"email\"/></td> </tr> <tr align=\"left\"> <td align=\"left\"><b>Address:</b></td> <td><input id=\"txt_box\" type=\"text\" name=\"address1\"/></td> <td align=\"left\"><b>Address(ext):</b></td> <td><input id=\"txt_box\" type=\"text\" name=\"address2\"/></td> </tr> <tr align=\"left\"> <td align=\"left\"><b>Telephone:</b></td> <td><input id=\"txt_box\" type=\"text\" name=\"telephone\"/></td> </tr> <tr><td height=\"10\"></td></tr> </table>"; echo "<table id =\"cart_bdr\" align =\"center\" border=\"0\" padding=\"15\" width=\"950\">"; //format the cart using a HTML table echo "<tr id=\"hdg_bkgrnd\" height=\"40\"><td width=\"237\" align=\"center\"><b>Thumbnail</b></td><td width=\"237\" align=\"center\"><b>Id</b></td><td width=\"237\" align=\"center\"><b>Name</b></td><td width=\"237\" align=\"center\"><b>Quantity</b></td><td width=\"237\" align=\"center\"><b>Amount</b></td></tr>"; //iterate through the cart, the $product_id is the key and $quantity is the value foreach ($_SESSION['cart'] as $product_id => $quantity) { //get the name, price and pix from the database - this will depend on your database implementation. //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection $sql = sprintf("SELECT Id, Name, Price, Pix FROM products WHERE Id = %d;", $product_id); $result = mysqli_query($con,$sql) or die("Couldnt run query"); $row = mysqli_num_rows($result); //Only display the row if there is a product (though there should always be as we have already checked) if($row > 0) { list($Id, $Name, $Price, $Pix) = mysqli_fetch_row($result); $line_cost = $Price * $quantity; //work out the line cost $total = $total + $line_cost; //add to the total cost echo "<tr height=\"55\">"; echo "<td align=\"center\"><img id=\"thumb_bdr\" src=\"$Pix\" width=\"50\" height=\"50\"/></td>"; echo "<td align=\"center\" id=\"cols_bkgrnd\"><div name=\"id\">$Id</div></td>"; //show this information in table cells echo "<td align=\"center\" id=\"cols_bkgrnd\">$Name</td>"; //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product echo "<td align=\"center\" id=\"cols_bkgrnd\">$quantity <a href=\"$_SERVER[php_SELF]?action=remove&id=$product_id\">X</a></td>"; echo "<td align=\"right\" id=\"cols_bkgrnd\">$$line_cost</td>"; echo "</tr>"; } } //show the total echo "<tr id=\"hdg_bkgrnd\" height=\"40\">"; echo "<td></td>"; echo "<td></td>"; echo "<td></td>"; echo "<td align='right'><div id=\"total\"><b>Total</b></div></td>"; echo "<td align='right'>$$total</td>"; echo "</tr>"; echo "</table>"; //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation echo "<table width=\"950\" align=\"center\">"; echo "<tr>"; echo "<td width=\"237\"></td>"; echo "<td width=\"237\"></td>"; echo "<td width=\"237\"></td>"; echo "<td width=\"237\" align=\"right\"><input id=\"submit_btn\" value=\"Send\" type=\"submit\"/></td>"; echo "<td width=\"237\"><input id=\"cart_btn\" type=\"button\" value=\"Empty\" onclick=\"window.location.href='$_SERVER[php_SELF]?action=empty'\" /></td>"; echo "</tr>"; echo "</table>"; echo "</form>"; } else { //otherwise tell the user they have no items in their cart echo "You have no items in your shopping cart."; } //function to check if a product exists function productExists($product_id) { global $product_id; global $con; global $host; global $user; global $pass; global $dbase; //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection $sql = sprintf("SELECT * FROM products WHERE id = %d;", $product_id); $result = mysqli_query($con,$sql) or die ('Could not run query'); $row = mysqli_num_rows($result); return $row > 0; } ?> </td> </tr> </table> <table width="1024" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="40" align="center" class="bdr">© <?php echo date("Y");?> MizBones</td> </tr> </table> </body> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 29, 2012 Share Posted August 29, 2012 did you look at the example i gave you smarty email template: Dear {$contact_info.name}, Welcome and thank you for signing up as a member of our user group. Click on the link below to login with your user name of '{$contact_info.username}' so you can post in our forums. {$login_url} List master {textformat wrap=40} This is some long-winded disclaimer text that would automatically get wrapped at 40 characters. This helps make the text easier to read in mail programs that do not wrap sentences for you. {/textformat} <?php // get $contact_info from db or other resource here $smarty->assign('contact_info',$contact_info); $smarty->assign('login_url',"http://{$_SERVER['SERVER_NAME']}/login"); mail($contact_info['email'], 'Thank You', $smarty->fetch('email_body.tpl')); ?> this shows you how to send an email with variables in it. Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 29, 2012 Author Share Posted August 29, 2012 so basically what would I have to do? include the smarty library in my shopping cart script and create the variables for the template there? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 29, 2012 Share Posted August 29, 2012 yup just modify that email template name it email_body.tpl and make sure you use the assign method for smarty to assign all your variables for the template. Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 29, 2012 Author Share Posted August 29, 2012 Im creating the smarty template but im stuck at how to assign the variables, all the variables are assigned through php and I cant find any documentation on how to pass php assigned variables in smarty. All the examples are assigned through smarty. So how do I pass the php variables inside smarty to use on my template? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 29, 2012 Share Posted August 29, 2012 assigning values in smarty Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 29, 2012 Author Share Posted August 29, 2012 Let's say for example we have the following php query. "$row = mysqli_fetch_assoc ($result) list($Id,$name);". How do I get the variables from that into smarty? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 29, 2012 Share Posted August 29, 2012 Assigning mysql_fetch_assoc to Smarty variables Hopefully you should be able to figure this out and tweak it how you need it. Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 29, 2012 Author Share Posted August 29, 2012 In my code below im getting a notice undefined index and notice undefined offset message from php. I checked the line I cant figure out whats wrong with it, I have never gotten this notice before. I just updated my testing server to the latest version and all the error options are turned on. What is it im doing wrong here? <?php // Product Id from URL $product_id = array_key_exists('id',$_GET) ? $_GET['id']: null; // Action from URL $action = array_key_exists('action',$_GET) ? $_GET['action']: null; //if there is an product_id and that product_id doesn't exist display an error message if($product_id && !productExists($product_id)) { die("Product Doesn't Exist"); } switch($action) { //decide what to do case "add": $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id break; case "remove": $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise it will show zero, then -1, -2 etc when the user keeps removing items. break; case "empty": unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. break; } ?> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 29, 2012 Share Posted August 29, 2012 a few things i noticed one about your switch statement there is no DEFAULT for it. might need to fix that as well. change == to === but your main problem is that you are using array_key_exists insted of isset Quote Link to comment Share on other sites More sharing options...
silkfire Posted August 30, 2012 Share Posted August 30, 2012 Off top: I laughed really loud when I read your mispelled topic title, sounds like you're saying "Need help raping the body of my shopping cart" Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 30, 2012 Author Share Posted August 30, 2012 In my code above I tried to define the $_GET variables as NULL to get rid of notices of undefined variables but it messes with the action of my switch for the cart, now the empty button doesnt clear the cart. This is what I have right now for both $_GET['id'] and $_GET['action']. What else could I do to define the variables and still have the switch function as needed? if(isset($_GET['id'])) { $product_id = $_GET['id']; } else { $product_id = NULL; } Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2012 Share Posted August 30, 2012 Try changing $action = array_key_exists('action',$_GET) ? $_GET['action']: null; to $action = array_key_exists('action',$_GET) ? $_GET['action']: "empty"; And your switch case "empty": unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. break; to case "empty": default: unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. break; Edit: you can also just use isset() instead of array_key_exists. Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 30, 2012 Author Share Posted August 30, 2012 I tried that but everytime I added a product to the cart and go to another page then come back to cart it displays "You have no item in your shopping cart". It empty the cart regardless of whether items are in it or not. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2012 Share Posted August 30, 2012 Go through step by step backwards. What does $action print? What is the URL of this page? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.