Jump to content

Need help with output


andrej13

Recommended Posts

Hey folks,

 

I have made a cart for buying drinks. I also made an sql database. But when I click on the email button, I receive an email but only with the id of the products. I would like to have an output with the id of the product, but also the name of the product ( the name of the column in sql is also 'name')

 

this is my code and you can check it out on  http://fhcs.be/cart-demo3/

 

thanks in advance

 

cart.php

<?php
// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
// Start the session

session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
case 'add':
	if ($cart) {
		$cart .= ','.$_GET['id'];
	} else {
		$cart = $_GET['id'];
	}
	break;
case 'delete':
	if ($cart) {
		$items = explode(',',$cart);
		$newcart = '';
		foreach ($items as $item) {
			if ($_GET['id'] != $item) {
				if ($newcart != '') {
					$newcart .= ','.$item;
				} else {
					$newcart = $item;
				}
			}
		}
		$cart = $newcart;
	}
	break;
case 'update':
if ($cart) {
	$newcart = '';
	foreach ($_POST as $key=>$value) {
		if (stristr($key,'qty')) {
			$id = str_replace('qty','',$key);
			$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
			$newcart = '';
			foreach ($items as $item) {
				if ($id != $item) {
					if ($newcart != '') {
						$newcart .= ','.$item;
					} else {
						$newcart = $item;
					}
				}
			}
			for ($i=1;$i<=$value;$i++) {
				if ($newcart != '') {
					$newcart .= ','.$id;
				} else {
					$newcart = $id;
				}
			}
		}
	}
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP Shopping Cart Demo &#0183; Cart</title>
<link rel="stylesheet" href="css/styles.css" />
</head>

<body>

<div id="shoppingcart">

<h1>Your Shopping Cart</h1>


<?php
echo writeShoppingCart();

?>

</div>

<div id="contents">

<h1>Please check quantities...</h1>

<?php
echo showCart();
?>

<p><a href="index.php">Back to bookshop...</a></p>

<form action="mail.php" method="post">
<input type="submit" name="sendemail" value="Email" />
<input type="hidden" name="cart" value="<?= $cart; ?>" />
</form>




</div>

</body>
</html>

 

mail.php

<?php


session_start();

if($_POST['sendemail'] == 'Email')
{


  $headers = 'From: Sender <andrej13@gmail.com>';
  mail('andrej13@gmail.com', 'Subject', $_SESSION["cart"] , $headers);
  
  
  echo 'Your mail has been sent';
}
else
{
  echo 'message not sent';
}


?>

 

and the functions.php

<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
	return '<p>You have no items in your shopping cart</p>';
} else {
	// Parse the cart session variable
	$items = explode(',',$cart);
	$s = (count($items) > 1) ? 's':'';
	return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
}
}

function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
	$items = explode(',',$cart);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
	}
	$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
	$output[] = '<table>';
	foreach ($contents as $id=>$qty) {
		$sql = 'SELECT * FROM products WHERE id = '.$id;
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$output[] = '<tr>';
		$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
		$output[] = '<td>'.$name.'</td>';
		$output[] = '<td>€'.$price.'</td>';
		$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
		$output[] = '<td>€'.($price * $qty).'</td>';
		$total += $price * $qty;
		$output[] = '</tr>';
	}
	$output[] = '</table>';
	$output[] = '<p>Grand total: <strong>€'.$total.'</strong></p>';
	$output[] = '<div><button type="submit">Update cart</button></div>';
	$output[] = '</form>';
} else {
	$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}

?>

 

 

 

 

Link to comment
Share on other sites

In functions.php you have code which looks up the names of the products.  You can use similar code in mail.php.

 

Ideally you should have a function which takes as input the array of product ids, and returns as output an array containing product names, ids and quantities.  Then you can use that same function in mail.php and also in showCart()

Link to comment
Share on other sites

Sorry that I don't have a solution for you right now... But it might help you to look into arrays instead of saving the list as a string and having to add to it and explode it over and over. Also, if you store the shopping cart in the $_SESSION, there's no need to submit it through a form. Just call the $_SESSION variables in your mail script. Then you could have your script lookup each products name from the database... here's some things you should consider.

 

you can use $cart as an Array(list).

 

Use this to add the $_GET[id] to the cart list and keep track of quantity.

$cart[$_GET['id']] = $_GET['qty'];

 

To process the array you could use a foreach loop.

foreach($cart AS $id => $qty) {
  //stuff
}

Link to comment
Share on other sites

just toss in a couple extra lines of code to pull the name out the database and send it in the mail like the ID:

$qry = 'SELECT name from cart_1 WHERE <id_field> = ' .$_SESSION['cart'];
$result = mysql_query($qry);
$row = mysql_fetch_assoc($result);
$_SESSION['name'] = $row['name'];

Then just add this to your mail:

$info = $_SESSION['cart'] . ' : ' . $_SESSION['name'];
...
mail('andrej13@gmail.com', 'Subject', $info, $headers);

Link to comment
Share on other sites

just toss in a couple extra lines of code to pull the name out the database and send it in the mail like the ID:

$qry = 'SELECT name from cart_1 WHERE <id_field> = ' .$_SESSION['cart'];
$result = mysql_query($qry);
$row = mysql_fetch_assoc($result);
$_SESSION['name'] = $row['name'];

Then just add this to your mail:

$info = $_SESSION['cart'] . ' : ' . $_SESSION['name'];
...
mail('andrej13@gmail.com', 'Subject', $info, $headers);

 

 

thanks for the great tip, but where do I put this?

$qry = 'SELECT name from cart_1 WHERE <id_field> = ' .$_SESSION['cart'];
$result = mysql_query($qry);
$row = mysql_fetch_assoc($result);
$_SESSION['name'] = $row['name'];

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.