Jump to content

cart.php


CASHOUT

Recommended Posts

the product_image is supposed to redirect you to the product.php page when you click on it.

instead im getting a error 404

 

any ideas??

 

<?php 
session_start(); // Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php"; 
?>
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 1 (if user attempts to add something to the cart from the product page)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['pid'])){
$pid = $_POST['pid'];
$wasFound = false;
$i = 0;
//if the cart session variable is not set or cart arraty is empty
if (!isset($_SESSION["cart_array"])||count($_SESSION["cart_array"]) < 1){
//RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
} else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach($_SESSION["cart_array"] as $each_item){
	$i++;
	while (list($key, $value) = each($each_item)){
		if ($key == "item_id" && $value == $pid){
			// That item is in cart already so lets adjust its quantity using array_splice()
			array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
			$wasFound = true;
		}// close if condition
 	 }// close while loop
  }// close foreach loop
  if ($wasFound == false){
	  array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
  }
  }
  header("location: cart.php");
  exit();
}
?>
<?php 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 2 (if user chooses to empty their shopping cart)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart"){
unset($_SESSION["cart_array"]);
}
?>
<?php 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 3 (if user chooses to adjust item quantuty)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != ""){
// execute some code
$item_to_adjust = $_POST['item_to_adjust'];
$quantity = $_POST['quantity'];
$i = 0;
foreach($_SESSION["cart_array"] as $each_item){
	$i++;
	while (list($key, $value) = each($each_item)){
		if ($key == "item_id" && $value == $item_to_adjust){
			// That item is in cart already so lets adjust its quantity using array_splice()
			array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
		}// close if condition
 	 }// close while loop
}// close foreach loop
header("location: cart.php");
  exit();
}
?>
<?php 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 4 (if user wants to remove an item from the cart)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] !=""){
// Access the array and run code to remove that array index
$key_to_remove = $_POST['index_to_remove'];
if (count($_SESSION["cart_array"]) <= 1){
	unset($_SESSION["cart_array"]);
} else {
	unset($_SESSION["cart_array"]["$key_to_remove"]);
	sort($_SESSION["cart_array"]);
}
header("location: cart.php");
  exit();
}
?>
<?php 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 5 (render the cart for the user to view on the page)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cartOutput = "";
$cartTotal = "";
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
$cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item){
	$item_id = $each_item['item_id'];
	$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
	while ($row = mysql_fetch_array($sql)){
		$product_name = $row["product_name"];
		$price = $row["price"];
		$details = $row["details"];
	}
	$pricetotal = $price * $each_item['quantity']; //this line calculates final price for duplicate items in cart
	$cartTotal = $pricetotal + $cartTotal;
	setlocale(LC_MONETARY , "en_US");
	$pricetotal = money_format("%10.2n", $pricetotal);
	// Dynamic table row assembly
	$cartOutput .= "<tr>";
	$cartOutput .= '<td><a href=\"product.php?id=' . $item_id . '">' . $product_name . '</a><br/><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name . '" width="40" height="52" border="1" /></td>';
	$cartOutput .= '<td>' . $details . '</td>';
	$cartOutput .= '<td>$' . $price . '</td>';
	$cartOutput .= '<td><form action="cart.php" method="post">
	<input name="quantity" type="text" value="' . $each_item['quantity']. '" size="1" maxlength="1" />
	<input name="adjustBtn' . $item_id . '" type="submit" value="change" />
	<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
	</form></td>';
	//$cartOutput .= '<td>' . $each_item['quantity']. '</td>';
	$cartOutput .= '<td>' . $pricetotal . '</td>';
	$cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
	$cartOutput .= '</tr>';
	$i++;
}
setlocale(LC_MONETARY , "en_US");
	$cartTotal = money_format("%10.2n", $cartTotal);
$cartTotal = "<div style='font-size: 18px; margin-top: 12px;' align='right'>Cart Total : $".$cartTotal." USD </div>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Your Cart</title>
<link href="style/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
    <div id="pageContent">
    <div style="margin:24px; text-align:left;">
    
    <br />
    <table width="100%" border="1" cellspacing="0" cellpadding="6">
      <tr>
        <td width="18%" bgcolor="#99CCFF"><strong>Product</strong></td>
        <td width="39%" bgcolor="#99CCFF"><strong>Product Description</strong></td>
        <td width="12%" bgcolor="#99CCFF"><strong>Unit Price</strong></td>
        <td width="11%" bgcolor="#99CCFF"><strong>Quantity</strong></td>
        <td width="10%" bgcolor="#99CCFF"><strong>Total</strong></td>
        <td width="10%" bgcolor="#99CCFF"><strong>Remove</strong></td>
      </tr>
     <?php echo $cartOutput; ?>
      <!--<tr>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
      </tr> -->
    </table>
    <?php echo $cartTotal; ?>
    <br />
    <a href="cart.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a>
    </div>
    <br />
    </div>
    <?php include_once("template_footer.php");?>
    </div>
    </body>
    </html>

Link to comment
https://forums.phpfreaks.com/topic/244463-cartphp/
Share on other sites

finished product before, scripting variables for website payment standard, and adding PayPal checkout to the 3rd- party shopping cart.

 

 

<?php 
session_start(); // Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php"; 
?>
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 1 (if user attempts to add something to the cart from the product page)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['pid'])){
$pid = $_POST['pid'];
$wasFound = false;
$i = 0;
//if the cart session variable is not set or cart arraty is empty
if (!isset($_SESSION["cart_array"])||count($_SESSION["cart_array"]) < 1){
//RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
} else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach($_SESSION["cart_array"] as $each_item){
	$i++;
	while (list($key, $value) = each($each_item)){
		if ($key == "item_id" && $value == $pid){
			// That item is in cart already so lets adjust its quantity using array_splice()
			array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
			$wasFound = true;
		}// close if condition
 	 }// close while loop
  }// close foreach loop
  if ($wasFound == false){
	  array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
  }
  }
  header("location: cart.php");
  exit();
}
?>
<?php 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 2 (if user chooses to empty their shopping cart)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart"){
unset($_SESSION["cart_array"]);
}
?>
<?php 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 3 (if user chooses to adjust item quantuty)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != ""){
// execute some code
$item_to_adjust = $_POST['item_to_adjust'];
$quantity = $_POST['quantity'];
$quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
if ($quantity >=2){ $quantity = 1;}
if ($quantity < 1){ $quantity = 1;}
if ($quantity == ""){ $quantity = 1;}
$i = 0;
foreach($_SESSION["cart_array"] as $each_item){
	$i++;
	while (list($key, $value) = each($each_item)){
		if ($key == "item_id" && $value == $item_to_adjust){
			// That item is in cart already so lets adjust its quantity using array_splice()
			array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
		}// close if condition
 	 }// close while loop
}// close foreach loop
header("location: cart.php");
  exit();
}
?>
<?php 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 4 (if user wants to remove an item from the cart)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] !=""){
// Access the array and run code to remove that array index
$key_to_remove = $_POST['index_to_remove'];
if (count($_SESSION["cart_array"]) <= 1){
	unset($_SESSION["cart_array"]);
} else {
	unset($_SESSION["cart_array"]["$key_to_remove"]);
	sort($_SESSION["cart_array"]);
}
header("location: cart.php");
  exit();
}
?>
<?php 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 5 (render the cart for the user to view on the page)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cartOutput = "";
$cartTotal = "";
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
$cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item){
	$item_id = $each_item['item_id'];
	$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
	while ($row = mysql_fetch_array($sql)){
		$product_name = $row["product_name"];
		$price = $row["price"];
		$details = $row["details"];
	}
	$pricetotal = $price * $each_item['quantity']; //this line calculates final price for duplicate items in cart
	$cartTotal = $pricetotal + $cartTotal;

	setlocale(LC_MONETARY , "en_US");
	$pricetotal = money_format("%10.2n", $pricetotal);
	// Dynamic table row assembly
	$cartOutput .= "<tr>";
	$cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br/><img src="inventory_images/' . $item_id . '.jpg"alt="' . $product_name . '" width="40" height="52" border="1" /></td>';
	$cartOutput .= '<td>' . $details . '</td>';
	$cartOutput .= '<td>$' . $price . '</td>';
	$cartOutput .= '<td><form action="cart.php" method="post">
	<input name="quantity" type="text" value="' . $each_item['quantity']. '" size="1" maxlength="1" />
	<input name="adjustBtn' . $item_id . '" type="submit" value="change" />
	<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
	</form></td>';
	//$cartOutput .= "<td>' . $each_item['quantity']. '</td>";
	$cartOutput .= '<td>' . $pricetotal . '</td>';
	$cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
	$cartOutput .= '</tr>';
	$i++;
}
setlocale(LC_MONETARY , "en_US");
	$cartTotal = money_format("%10.2n", $cartTotal);
$cartTotal = "<div style='font-size: 18px; margin-top: 12px;' align='right'>Cart Total : $".$cartTotal." USD </div>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Your Cart</title>
<link href="style/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
    <div id="pageContent">
    <div style="margin:24px; text-align:left;">
    
    <br />
    <table width="100%" border="1" cellspacing="0" cellpadding="6">
      <tr>
        <td width="18%" bgcolor="#99CCFF"><strong>Product</strong></td>
        <td width="39%" bgcolor="#99CCFF"><strong>Product Description</strong></td>
        <td width="12%" bgcolor="#99CCFF"><strong>Unit Price</strong></td>
        <td width="11%" bgcolor="#99CCFF"><strong>Quantity</strong></td>
        <td width="10%" bgcolor="#99CCFF"><strong>Total</strong></td>
        <td width="10%" bgcolor="#99CCFF"><strong>Remove</strong></td>
      </tr>
     <?php echo $cartOutput; ?>
      <!--<tr>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
      </tr> -->
    </table>
    <?php echo $cartTotal; ?>
    <br />
    <a href="cart.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a>
    </div>
    <br />
    </div>
    <?php include_once("template_footer.php");?>
    </div>
    </body>
    </html>

 

Link to comment
https://forums.phpfreaks.com/topic/244463-cartphp/#findComment-1255689
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.