|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello,
Im still a PHP newb, but i got a shopping cart script up and running on my web server it is the one from here URL it works great but i want it to do one more thing but i do not know how to make it do that. What i want is to make a checkout page that would e-mail me all the contents of the shopping cart, and tell me what the user bought and how much each item is, ect. I do not know how to make it do that, Again the code is just like the code on the Macromedia site, down to the table names. This is my Order Table layout: order_id int(11) No auto_increment order_date date No 0000-00-00 order_qty tinyint(4) No 0 order_price decimal(4,2) No 0.00 user_id int(11) No 0 itemId int(11) No 0 This is my User table: user_id int(11) No auto_increment user_name varchar(50) No user_email varchar(100) No user_address varchar(120) No user_city varchar(60) No user_nhood varchar(60) No user_zip varchar(5) No user_country varchar(60) No user_pass varchar(30) No user_phone varchar(14) No And that is as far as i can get. here is my code:: Code:
<?php
include("db.php");
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
function AddItem($itemId, $qty)
{
// Will check whether or not this item
// already exists in the cart table.
// If it does, the UpdateItem function
// will be called instead
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
// Check if this item already exists in the users cart table
$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId" );
$row = mysql_fetch_row($result);
$numRows = $row[0];
if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query
@mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)");
}
else
{
// This item already exists in the users cart,
// we will update it instead
UpdateItem($itemId, $qty);
}
}
function UpdateItem($itemId, $qty)
{
// Updates the quantity of an item in the users cart.
// If the qutnaity is zero, then RemoveItem will be
// called instead
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
if($qty == 0)
{
// Remove the item from the users cart
RemoveItem($itemId);
}
else
{
mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");
}
}
function RemoveItem($itemId)
{
// Uses an SQL delete statement to remove an item from
// the users cart
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
}
function ShowCart()
{
// Gets each item from the cart table and display them in
// a tabulated format, as well as a final total for the cart
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
$totalCost = 0;
$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemPrice asc");
?>
<html>
<head>
<title> Your Shopping Cart </title>
<script language="JavaScript">
function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;
document.location.href = 'order.php?action=update_item&id='+itemId+'&qty='+newQty;
}
</script>
</head>
<body bgcolor="#ffffff">
<form name="frmCart" method="get">
<table align="left" width="80%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="15%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Qty</b>
</font>
</td>
<td width="55%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Product</b>
</font>
</td>
<td width="20%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Price Each</b>
</font>
</td>
<td width="10%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Remove?</b>
</font>
</td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>
<tr>
<td width="15%" height="25">
<font face="verdana" size="1" color="black">
<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
<?php
for($i = 1; $i <= 20; $i++)
{
echo "<option ";
if($row["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</font>
</td>
<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="order.php
?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a>
</font>
</td>
</tr>
<?php
}
// Display the total
?>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="33%" colspan="1">
<font face="verdana" size="1" color="black">
<a href="products.php"><< Keep Shopping</a>
</font>
</td>
<td width="25%" colspan="1">
<font face="verdana" size="2" color="black">
<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>
</font>
</td>
<td width="33%" colspan="2">
<font face="verdana" size="1" color="black">
<a href="checkout.php">Checkout >></a>
</font>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
}
?>
|
|
#2
|
||||
|
||||
|
There's a handy PHP function that isn't hard to implement...
http://www.php.net/manual/en/function.mail.php If you're running it on your home machine, you may need to set up a mail server... Here's a helpful thread that may assist you: http://forums.devarticles.com/showt...ght=mail+server |
|
#3
|
|||
|
|||
|
yes, the mail function but what would i put in the function to make is send the the contents of the cart back? Would it be a form varable of some sort like in my code i posted down
Code:
form name="frmCart" method="get"> Is there a way to chage the port on which sendmail operates? Because im almost + that my ISP Charter Comm. blocks those ports. Im running The Fedora Core on my box. |
|
#4
|
||||
|
||||
|
Sorry, I underestimated you... =)
I would suggest coding it (in PHP) inside your AddCart() function... after you insert the information into the database, format a string with all the information you want to see then mail() it to yourself (or wherever) As far as sendmail goes... Edit sendmail.cf and look for this DaemonPortOptions=Port=smtp,Addr=127.0.0.1, Name=MTA Replace Port=smtp with Port=125 |
|
#5
|
|||
|
|||
|
ok i must be missing something i have no AddCart() function i do have AddItem though. Otherwise i think i got it from there. For the string i would put in
Code:
$row["itemId"]; $row["itemName"]; $row["itemPrice"]; |
|
#6
|
||||
|
||||
|
Sorry, my mistake... I meant AddCart()
the information in your insert statement is: GetCartId(), $itemId, $qty You could simply email yourself that information, or submit another query to the database (based on the CartId) and email those results... |
|
#7
|
|||
|
|||
|
Yea thanks ill try that in a few minutes.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Checkout Page Help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|