Checkout script.
Jeg skal bruge et checkout scripttil min lille shop. det som er det eneste den skal er at sende en mail med indholdet af min indkøbs kurv... og så noget adresse info hvor det skal sendes til! :Dfilerne i min shop ser således ud.
:KODE:
------------db.php--------------
<?
session_start();
$dbServer = "localhost";
$dbUser = "eterno_dk";
$dbPass = "beeeep";
$dbName = "eterno_dk";
function ConnectToDb($server, $user, $pass, $database)
{
$s = mysql_connect($server, $user, $pass) or die("Query failed: $query<br><br>" . mysql_error());
$d = mysql_select_db($database, $s) or die("Query failed: $query<br><br>" . mysql_error());
/*
if (!$s || !$d)
return false ;
else
return true ;
*/
}
function GetCartId()
{
//generate an encrypted string and set it as cookie
if (isset($_cookie["cartId"]))
{
return $_cookie["cartId"];
}
else
{
setcookie("cartId", session_Id(), time() + ((3600 * 24) * 30));
return session_id();
}
}
?>
-------------products.php--------------
<?
include ("db.php");
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
//$result = mysql_query("select * from items order by itemName asc");
//show the error with mysql_error() function
$result = mysql_query("select * from items order by itemName asc") or die("Query failed: $query<br><br>". mysql_error());
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr bgcolor="#999999">
<td width="85"><font color="#FFFFFF" size="2" face="Tahoma"><strong>Titel</strong></font></td>
<td width="59"><font color="#FFFFFF" size="2" face="Tahoma"><strong>Pris</strong></font></td>
<td width="303"><font color="#FFFFFF" size="2" face="Tahoma"><strong>Beskrivelse</strong></font></td>
<td width="53"><font size="2" face="Tahoma"> </font></td>
</tr>
</table>
<table width="100%" border="0" align="center">
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td width="17%" valign="top">
<font color="black" size="2" face="Tahoma"><?php echo $row["itemName"]; ?>
</font>
</td>
<td width="12%" align="left" valign="top">
<font color="black" size="2" face="Tahoma"><? echo $row["itemPrice"]; ?> Dkk</font>
</td>
<td width="61%" valign="top">
<font color="black" size="2" face="Tahoma"><?php echo $row["itemDesc"]; ?>
</font>
</td>
<td width="9%" valign="top">
<font color="black" size="2" face="Tahoma"><a href ="cart.php?action=add_item&id=<?php echo $row["itemId"];?>&qty=1">Tilføj
</a></font>
</td>
<td width="1%"></td>
</tr>
<tr>
<td colspan="4">
<hr size="1" NOSHADE>
</td>
</tr>
<? } ?>
<tr>
<td colspan="4">
<font color="black" size="1" face="Tahoma"><a href="cart.php">Indkøbs kurv</a></font>
</td>
</tr>
</table>
</BODY>
---------------cart.php---------------
<?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.itemName 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 = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
}
</script>
</head>
<body bgcolor="#ffffff">
<font face="Tahoma">Indkøbs kurv </font>
<form name="frmCart" method="get">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr bgcolor="#999999">
<td width="15%" height="25">
<font color="white" size="1" face="Tahoma"> <b>Antal</b></font>
</td>
<td width="55%" height="25">
<font color="white" size="1" face="Tahoma">
<b>Produkt</b></font>
</td>
<td width="20%" height="25">
<font color="white" size="1" face="Tahoma">
<b>Pris pr. stk </b></font>
</td>
<td width="10%" height="25">
<font color="white" size="1" face="Tahoma">
<b>Fjern ? </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 color="black" size="1" face="Tahoma">
<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 color="black" size="1" face="Tahoma">
<?php echo $row["id"]; ?><?php echo $row["itemName"]; ?>
</font>
</td>
<td width="20%" height="25">
<font color="black" size="1" face="Tahoma"> <?php echo number_format($row["itemPrice"], 2, ".", ","); ?> Dkk
</font>
</td>
<td width="10%" height="25">
<font color="black" size="1" face="Tahoma">
<a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Fjern</a>
</font>
</td>
</tr>
<?php
}
// Display the total
?>
<tr>
<td width="100%" colspan="4">
<hr size="1" NOSHADE>
</td>
</tr>
<tr>
<td width="70%" colspan="2">
<font face="Tahoma"><font face="verdana" size="1" color="black">
<a href="index.php">Fortsæt indkøb</a>
- <a href="checkout.php">Checkout</td>
<td width="30%" colspan="2">
<font color="black" size="2" face="Tahoma">
<b>Total: <?php echo number_format($totalCost, 2, ".", ","); ?> Dkk </b>
</font>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
}
?>
--------------showcart.php--------------
<?
function ShowCart()
{
?>
<script language="JavaScript">
function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;
document.location.href = 'cart.php?update_item&id='+itemId+'&qty='+newQty;
}
</script>
<?
$result = mysql_query("select* from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '".GetCartId()."' order by items.itemName asc");
while($row = mysql_fetch_array($result))
{
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>
<tr>
<td width = "15%" height="25">
<font face="arial" size="2" color="black">
<select name="<? echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
<?
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="arial" size="2" color="black">
<? echo $row["itemName"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="arial" size="2" color="black">
RM<? echo number_format($row["itemPrice"], 2,".", "."); ?>
</font>
</td>
<td width="10%" height="25">
<font face="arial" size="2" color="black">
<a href="cart.php?action=remove_item&id=<? echo $row["itemId"]; ?>">Remove</a>
</font>
</td>
<?
} // close for while loop
//$totalCost += ($row["qty"] * $$row["itemPrice"]);
//$totalCost = $totalCost + ($row["qty"] * $row["itemPrice"]); // same by using +=
?>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="70%" colspan="2">
<font face="arial" size="2" color="black">
<a href="products.php"><< Keep Shopping</a>
</font>
</td>
<td width="30%" colspan="2">
<font face="arial" size="3" color="black">
<b>Total: RM<? echo number_format($totalCost, 2, ".","."); ?> </b>
</font>
</td>
</tr>
<? } //close function?>
------------------------------------------
:KODE SLUT:
Jeg vil så have en side der skal hedde checkout.php hvor i man kan fylde sin adreesse info, and so. Og så skal den sende en mail til mig med hva folk har bestilt og en samlet pris... :)
200 Point til den som kan bikse det sammen for jeg kan ikke :!