Well been learning php for a few weeks now and this little bit is driving me crazy.
I have 2 pages, one is product.php and the other is cart.php
In product.php I have a drop down menu (size of the product, small, medium and large) and I want that variable to be passed along to cart.php so in the cart it shows what size the user has selected. Also i want this size to be forwarded to the payments screen and invoicing etc
Following is the base code I have so far for both product.php and cart.php
PRODUCT.PHP
[php]<?php
// Script Error Reporting
error_reporting(E_ALL);
ini_set(‘display_errors’,‘1’);
?>
<?php
// Check to see the URL variable is set and that it exsists in the database
if (isset($_GET['id'])) {
// run a select query to get the latest 6 items
include "storescripts/connect_to_mysql.php";
$id = preg_replace('#[^0-9]#i','',$_GET['id']);
// Use this var to check to see if the ID exsists, if yes then get the product
// details, if no then exit then script and show the following
$sql = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0){
// Get all the product details
while($row = mysql_fetch_array($sql)){
$product_name = $row["product_name"];
$sku = $row["sku"];
$price = $row["price"];
$material = $row["material"];
$description = $row["description"];
$category = $row["category"];
$subcategory = $row["subcategory"];
}
} else {
echo "This product does not exsist";
exit ();
}
} else {
echo "Data to render this page is missing";
exit ();
mysql_close ();
}?>
Revs Clothing - <?php echo $product_name; ?>
<?php include_once("template_header.php");?>
<?php include_once("template_left.php");?>
|
View full sized image
|
Name:
<?php echo $product_name; ?>
Item Code:
<?php echo "$id $sku"; ?>
Price:
<?php echo "£".$price; ?>
Size:
Small
Medium
Large
Material:
<?php echo $material; ?>
Description:
<?php echo $description; ?>
|
|
More stuff |
<?php include_once("template_footer.php");?>
[/php]
CART.PHP
[php]<?php
session_start(); //start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set(‘display_errors’,‘1’);
// Connect to mysql database
include “storescripts/connect_to_mysql.php”;
?>
<?php
////////////
// Section - If user attempts to add something to cart on product page
if (isset($_POST['pid'])){
$pid = $_POST['pid'];
$wasFound = false;
$i = 0;
// if the cart session variable is not set or the cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
// Run if cart is empty
$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
} else {
// Run if cart has at least one item
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 the cart already so lets adjust the 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 - If User chooses to empty shopping cart
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
unset($_SESSION["cart_array"]);
}
?>
<?php
///////////////////////////
// Section - If User chooses to adjust item quantity
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 out everything but numbers
if ($quantity >= 100) { $quantity = 99; }
if ($quantity <= 0) { $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 the cart already so lets adjust the 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
}
?>
<?php
///////////////////////////
// Section - 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 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"]);
}
}
?>
<?php
///////////////////////////
// Section - render the cart for the user to view
$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) <1) {
$cartOutput = "
Your shopping cart is empty
";
} else {
// start Paypal checkout button
$pp_checkout_btn = '
';
// Start the for each loop
$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)) {
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$description = $row["description"];
}
$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
setlocale(LC_MONETARY, "en_GB.UTF-8");
$pricetotal = money_format("%10.2n",$pricetotal);
// Dynamic Checkout Btn Assembly
$x = $i + 1;
$pp_checkout_btn .= '
';
// Create the product array variable
$product_id_array .= "$item_id-".$each_item['quantity'].",";
// Dynamic Table Row Assembly
$cartOutput .='
';
$cartOutput .=' | ';
$cartOutput .='' . $product_name . ' | ';
$cartOutput .=''.$description.' | ';
$cartOutput .='************INSERT CODE FOR VARIABLE SIZE HERE********** | ';
$cartOutput .='£'.$price.' | ';
$cartOutput .='
| ';
//$cartOutput .=''.$each_item['quantity'].' | ';
$cartOutput .=''.$pricetotal.' | ';
$cartOutput .='
| ';
$cartOutput .='
';
$i++;
}
setlocale(LC_MONETARY, "en_GB.UTF-8");
$cartTotal = money_format("%10.2n",$cartTotal);
$cartTotal = "
".$cartTotal."
";
// Finish the Paypal Checkout Btn
$pp_checkout_btn .= '
';
}
?>
Revs Clothing - Your shopping basket
<?php include_once("template_header.php");?>
Image |
Product |
Desciption |
Size |
Unit Price |
Quantity |
Total |
Remove |
<?php echo $cartOutput; ?>
|
|
|
|
|
BASKET TOTAL |
<?php echo $cartTotal ?> |
|
<?php include_once("template_footer.php");?>
[/php]
Thankyou to everyone in advance for helping me with this one, think my head is hurting from banging it against the wall. I have tried so many different things I think I need to step back and relax