PHP - $_session Shopping Cart Problem
Hi,
I am using a $_SESSION for my shopping cart but cannot figure out how to total my quantity. The format I am using is: $_SESSION['cart']['itemid']['qty'] I realise that i could write a for loop to start at item id 0 and end at 1000 each time totalling the qty field but there must be a better, more efficient way, Does anybody have any advice? Mark Similar Tutorialshi my first language is german my english is not good so kindly bear it i have uploaded my files on free server /hosting site when i run it it shows me my products now the problem i m facing is when i click on add to cart it generates me this this error or warning at the all above page it generates this error Warning: Cannot modify header information - headers already sent by (output started at /www/clanteam.com/a/z/e/azeempro/htdocs/cart/include/config.php:1) in /www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/index.php on line 33 below it it add products in cart array Shopping Cart Array ( [2] => 1 ) 1 Items $90.00 checkout below that it gives this warning Warning: include() [function.include]: Unable to access /www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/shop/add_to_cart.php in /www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/layouts/shop.php on line 24 Warning: include(/www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/shop/add_to_cart.php) [function.include]: failed to open stream: No such file or directory in /www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/layouts/shop.php on line 24 Warning: include() [function.include]: Failed opening '/www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/shop/add_to_cart.php' for inclusion (include_path='.:/usr/lib/php') in /www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/layouts/shop.php on line 24 kindly help me i think there is path problem kindly solve this out please i will be thankful regards this is my index page Code: [Select] <?php include('db_fns.php'); include("cart_functions.php"); if(!isset($_SESSION['cart'])) { //session_start(); //session_start($_SESSION['cart']); $_SESSION['cart']=array(); $_SESSION['total_items']=0; $_SESSION['total_price']=0.00; } $view=empty($_GET['view'])?'index':$_GET['view']; $controller='shop'; switch ($view){ case "index": $products = find_product(); break; case "add_to_cart": $id=$_GET['id']; $add_item=add_to_cart($id); $_SESSION['total_items']=total_items($_SESSION['cart']); $_SESSION['total_price']=total_price($_SESSION['cart']); header('Location: index.php'); break; case "update_cart": update_cart(); $_SESSION['total_items']=total_items($_SESSION['cart']); $_SESSION['total_price']=total_price($_SESSION['cart']); header('Location:index.php?view=checkout'); break; case "checkout": $shipping=0.00; if($_SESSION['total_price']<=100.00 || $_SESSION['total_price']<=200.00){ $shipping=$shipping + 30.00; }elseif($_SESSION['total_price']<=300.00 || $_SESSION['total_price']<=500.00){ $shipping=$shipping + 45.00; }elseif($_SESSION['total_price']<=600.00 || $_SESSION['total_price']<=1000.00){ $shipping=$shipping+ 70.00; }elseif($_SESSION['total_price']>1000.00){ $shipping=$shipping+ 100.00; }else{ $shipping=10.00; } break; } include ('/www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/layouts/'.$controller.'.php'); ?>shop.php is Code: [Select] <?php ?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title> Shopping Cart</title> <link href="stylesheets/cool_style.css" media="screen" rel="Stylesheet" type="text/css" /> </head> <body> <h1> products</h1> <div class="cart"> <p><b> Shopping Cart</b></p> <p><?php print_r($_SESSION['cart']);?></p> <p><?php echo $_SESSION['total_items'];?> Items </p> <p>$<?php echo number_format($_SESSION['total_price'],2);?></p> <p><a href="index.php?view=checkout">checkout</a></p> </div> <hr/> <?php include('/www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/'.$controller.'/'.$view.'.php');?> </body> </html> I followed tutorial from youtube Code: [Select] http://www.youtube.com/watch?v=5b3TcoeY7Bsand made shopping cart. Everything is working fine, but there is one part I want to add. I want user to be able to set the quantity of products without going to the cart (user have to be able to enter quantity himself). At the moment I have no idea how to do this, i am still researching this topic, but wanted to try to ask for help from the pros . This is the complete code (a bit large): index.php: Code: [Select] <?php session_start(); require("includes/connection.php"); if(isset($_GET['page'])){ $pages=array("products", "cart"); if(in_array($_GET['page'], $pages)) { $_page=$_GET['page']; }else{ $_page="products"; } }else{ $_page="products"; } ?> <!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" /> <link rel="stylesheet" href="css/reset.css" /> <link rel="stylesheet" href="css/style.css" /> <title>Shopping Cart</title> </head> <body> <div id="container"> <div id="main"> <?php require($_page.".php"); ?> </div><!--end of main--> <div id="sidebar"> <h1>Cart</h1> <?php if(isset($_SESSION['cart'])){ $sql="SELECT * FROM products WHERE id_product IN ("; foreach($_SESSION['cart'] as $id => $value) { $sql.=$id.","; } $sql=substr($sql, 0, -1).") ORDER BY name ASC"; $query=mysql_query($sql); while($row=mysql_fetch_array($query)){ ?> <p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p> <?php } ?> <hr /> <a href="index.php?page=cart">Go to cart</a> <?php }else{ echo "<p>Your Cart is empty. Please add some products.</p>"; } ?> </div><!--end of sidebar--> </div><!--end container--> </body> </html> cart.php: Code: [Select] <?php if(isset($_POST['submit'])){ foreach($_POST['quantity'] as $key => $val) { if($val==0) { unset($_SESSION['cart'][$key]); }else{ $_SESSION['cart'][$key]['quantity']=$val; } } } ?> <h1>View cart</h1> <a href="index.php?page=products">Go back to products page</a> <form method="post" action="index.php?page=cart"> <table> <tr> <th>Name</th> <th>Quantity</th> <th>Price</th> <th>Items Price</th> </tr> <?php $sql="SELECT * FROM products WHERE id_product IN ("; foreach($_SESSION['cart'] as $id => $value) { $sql.=$id.","; } $sql=substr($sql, 0, -1).") ORDER BY name ASC"; $query=mysql_query($sql); $totalprice=0; while($row=mysql_fetch_array($query)){ $subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price']; $totalprice+=$subtotal; ?> <tr> <td><?php echo $row['name'] ?></td> <td><input type="text" name="quantity[<?php echo $row['id_product'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>" /></td> <td><?php echo $row['price'] ?>$</td> <td><?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?>$</td> </tr> <?php } ?> <tr> <td>Total Price: <?php echo $totalprice ?></td> </tr> </table> <br /> <button type="submit" name="submit">Update Cart</button> </form> <br /> <p>To remove an item set it's quantity to 0. </p> products.php: Code: [Select] <?php if(isset($_GET['action']) && $_GET['action']=="add"){ $id=intval($_GET['id']); if(isset($_SESSION['cart'][$id])){ $_SESSION['cart'][$id]['quantity']++; }else{ $sql_s="SELECT * FROM products WHERE id_product={$id}"; $query_s=mysql_query($sql_s); if(mysql_num_rows($query_s)!=0){ $row_s=mysql_fetch_array($query_s); $_SESSION['cart'][$row_s['id_product']]=array( "quantity" => 1, "price" => $row_s['price'] ); }else{ $message="This product id it's invalid!"; } } } ?> <h1>Product List</h1> <?php if(isset($message)){ echo "<h2>$message</h2>"; } ?> <table> <tr> <th>Name</th> <th>Description</th> <th>Price</th> <th>Action</th> </tr> <?php $sql="SELECT * FROM products ORDER BY name ASC"; $query=mysql_query($sql); while ($row=mysql_fetch_array($query)) { ?> <tr> <td><?php echo $row['name'] ?></td> <td><?php echo $row['description'] ?></td> <td><?php echo $row['price'] ?>$</td> <td><a href="index.php?page=products&action=add&id=<?php echo $row['id_product'] ?>">Add to cart</a></td> </tr> <?php } ?> </table> Hello, I'm working on an online bookstore and I'm having some problems with the shopping cart section. On the book page I have the following code, which is supposed to gather information like book title, author and price and send it to the shopping cart: Code: [Select] <form action="cos.php?actiune=adauga" method="post"> <input type="hidden" name="id_carte" value="<?=$id_carte?>"> <input type="hidden" name="titlu" value="<?=$rowCarte['titlu']?>"> <input type="hidden" name="autor" value="<?=$rowCarte['nume_autor']?>"> <input type="hidden" name="pret" value="<?=$rowCarte['pret']?>"> <input type="submit" value="Cumpara acum!"> </form> Cart.php looks like this: Code: [Select] <?php session_start(); include("conectare.php"); include("pagetop.php"); include("meniu.php"); $actiune=$_GET['actiune']; if(isset($_GET['actiune']) && $_GET['actiune']=="adauga") { $_SESSION['id_carte'][]=$_POST['id_carte']; $_SESSION['nr_buc'][]=1; $_SESSION['pret'][]=$_POST['pret']; $_SESSION['titlu'][]=$_POST['titlu']; $_SESSION['nume_autor'][]=$_POST['nume_autor']; } if(isset($_GET['actiune']) && $_GET['actiune']=="modifica") { for($i=0; $i<count($_SESSION['id_carte']); $i++) { $_SESSION['nr_buc'][$i]=$_POST['noulNrBuc'][$i]; } } ?> <td align="top"> <h1>Cosul de cumparaturi</h1> <form action="cos.php?actiune=modifica" method="post"> <table border="1" cellspacing="0" cellpadding="4"> <tr bgcolor="#F9F1E7"> <td><b>Nr. Buc</b></td> <td><b>Carte</b></td> <td><b>Pret</b></td> <td><b>Total</b></td> </tr> <?php for($i=0; $i<count($_SESSION['id_carte']); $i++) { if($_SESSION['nr_buc'][$i] !=0) { print '<tr><td><input type="text" name="noulNrBuc['.$i.']" size="1" value="'.$_SESSION['nr_buc'][$i].'"></td> <td>'.$_SESSION['titlu'][$i].' de '.$_SESSION['nume_autor'][$i].' </td> <td align="right">'.$_SESSION['pret'][$i].' lei </td> <td align="right">'.($_SESSION['pret'][$i] * $_SESSION['nr_buc'][$i]).' lei </td> </tr>'; $totalGeneral=$totalGeneral + ($_SESSION['pret'][$i] * $_SESSION['nr_buc'][$i]); } } print '<tr><td align="right" colspan="3"> Total in cos </td><td align="right">'.$totalGeneral.' lei </td></tr>'; ?> </table> <input type="submit" value="Modifica"><br><br> Introduceti 0 pentru cartile ce doriti sa le scoateti din cos! <h1>Continuare</h1> <table> <tr> <td width="200" align="center"><img src="cos.jpg"><a href="index1.php">Continua cumparaturile</a></td> <td width="200" align="center"><img src="casa.jpg"><a href="casa.php">Mergi la casa</a></td> </tr> </table> </td> <?php include("page_bottom.php"); ?> My problem is every time I add a book to the cart, nothing is updated except for the quantity, so I have no author, title or price info, like so: I'm just starting php coding and I can't figure out what's going on. Anyone willing to lend a helping hand? Hey again, Still on the same project but now doing the cart page. I've been bringing my skills up to date a lot doing this project, however, having said that I now can't figure out why my cart is not adding the items to the cart, i've checked the post data and the quantity is set to 1. Would really appreciate someone having a look at it and hopefully can see what i'm missing. you can see what it should look like (well so far lol) at here in my sandbox site. thanks again. (p.s. The parts are all over the place on the page, so thought better put whole page, sorry if shouldn't have done that) <?php // Initialize the session session_start(); // Include config file require_once "dbcontroller.php"; $db_handle = new DBController(); if(!empty($_GET["action"])) { switch($_GET["action"]) { case "add": if(!empty($_POST["quantity"])) { $productByCode = $db_handle->runQuery("SELECT * FROM products WHERE product_code='" . $_GET["product_code"] . "'"); $itemArray = array($productByCode[0]["product_code"]=>array('product_name'=>$productByCode[0]["product_name"], 'product_code'=>$productByCode[0]["product_code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"], 'image'=>$productByCode[0]["img1"])); if(!empty($_SESSION["cart_item"])) { if(in_array($productByCode[0]["product_code"],array_keys($_SESSION["cart_item"]))) { foreach($_SESSION["cart_item"] as $k => $v) { if($productByCode[0]["product_code"] == $k) { if(empty($_SESSION["cart_item"][$k]["quantity"])) { $_SESSION["cart_item"][$k]["quantity"] = 0; } $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"]; } } } else { $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray); } } else { $_SESSION["cart_item"] = $itemArray; } } break; case "remove": if(!empty($_SESSION["cart_item"])) { foreach($_SESSION["cart_item"] as $k => $v) { if($_GET["product_code"] == $k) unset($_SESSION["cart_item"][$k]); if(empty($_SESSION["cart_item"])) unset($_SESSION["cart_item"]); } } break; case "empty": unset($_SESSION["cart_item"]); break; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>LeaversHoodies.ie</title> <!-- Bootstrap --> <link href="css/bootstrap-4.4.1.css" rel="stylesheet"> <link href="css/accordian.css" rel="stylesheet"> </head> <body> <?php include 'header_student.php'; ?> <br /> <?php $school_page = "ravenswell"; $sql = "SELECT * FROM schools WHERE school_page = '$school_page'"; if($result = mysqli_query($conn, $sql)) while($row = mysqli_fetch_array($result)) { ?> <h3 class="text-center">Student Ordering Page For</h3> <h2 class="text-center" style="text-transform:uppercase;"><?php echo $row['school_name']; ?></h2> <hr style="width: 50%; text-align:center; border: 2px solid #00aeef; border-radius: 5px; margin: 0 auto;"> <br /> <div class="container"> <div class="row"> <div class="col"> <?php $path = "images/schools/"; $file = $row["logo"]; if(!empty($row['logo'])) { echo '<img src="'.$path.$file.'" style="width:95%; height:auto; margin-top:-130px;"><br /><br />'; } else { echo '<img src="images/schools/140x140.gif" style="width:95%; height:auto; margin-top:-130px;"><br /><br />'; } ?></div> <div class="col-6"> <h5>These are the garments your school has choosen :</h5><br /> <?php $school_id = $row["school_id"]; } var_dump($_SESSION); var_dump($_POST); $product_array = $db_handle->runQuery("SELECT * FROM choices INNER JOIN products USING (product_code) INNER JOIN colours USING (colour_id) WHERE school_id = '$school_id'"); if (!empty($product_array)) { foreach($product_array as $key=>$value){ ?> <div class="container"> <div class="row"> <div class="col-5" style="text-align:left;"> <img src="images/products/<?php echo $product_array[$key]["img1"]; ?>" alt="<?php echo $product_array[$key]["product_code"]; ?>" style="position:relative; top:0; width:200px; display:block;"> </div> <div class="col-7"> <h5><?php echo $product_array[$key]["product_name"]; ?></h5><p> in <?php echo $product_array[$key]["colour_name"]; ?></p> <p style="font-size:12px;"><?php echo $product_array[$key]["description"]; ?></p> <?php $comment = $product_array[$key]["comment"]; if (empty($comment)) { echo ""; } else { ?> <p style="font-size:12px;"><b>A note from your teacher:</b> <br /> <?php echo $product_array[$key]["comment"]; ?></p> <?php }; ?> <form action="student_order.php?schoolname=<?php echo $school_page; ?>?action=add&product_code=<?php echo $product_array[$key]["product_code"]; ?>" method="post"> <?php $product = $product_array[$key]["product_code"]; ?> Please select your size : <select id="size" name="size"> <?php $sql1 = "SELECT DISTINCT * FROM sizes WHERE product_code = '$product'"; if($result1 = mysqli_query($conn, $sql1)) while($row3 = mysqli_fetch_array($result1)){ echo "<option value='" . $row3['size'] . "'>" . $row3['size'] . "</option>"; } else { echo "nothing to see here"; } ?> </select> <br /><br /> <div class="number">How many do you want: <input type="number" style="font-size:12px;" id="quantity" name="quantity" value="1" min="1" max="5"><br /> Price : <?php echo "€".$product_array[$key]["price"]; ?> </div> <input type="hidden" id="product_code" value="<?php echo $product; ?>"><br /> <input type="submit" style="font-size:12px;" value="Add to Order" class="btnAddAction"> </form> </div> </div> <br /><hr style="width: 90%; text-align:center; border: 1px solid #00aeef; border-radius: 5px; margin: 0 auto;"><br /> </div> <?php } } else { echo "No Schools by that name registered."; } ?> </div> <div class="col-3"> <div style="border: 1px solid #d3d3d3; padding: 10px; border-radius: 5px; margin-top:30px;"> Your Order: </div> </div> </div> <div class="txt-heading">Shopping Cart</div> <a id="btnEmpty" href="student_order.php?schoolname=<?php echo $school_page; ?>?action=empty">Empty Cart</a> <?php if(isset($_SESSION["cart_item"])){ $total_quantity = 0; $total_price = 0; ?> <table class="tbl-cart" cellpadding="10" cellspacing="1"> <tbody> <tr> <th style="text-align:left;">Name</th> <th style="text-align:left;">Code</th> <th style="text-align:right;" width="5%">Quantity</th> <th style="text-align:right;" width="10%">Unit Price</th> <th style="text-align:right;" width="10%">Price</th> <th style="text-align:center;" width="5%">Remove</th> </tr> <?php foreach ($_SESSION["cart_item"] as $item){ $item_price = $item["quantity"]*$item["price"]; ?> <tr> <td><img src="images/products/<?php echo $item["img1"]; ?>" class="cart-item-image" /><?php echo $item["product_name"]; ?></td> <td><?php echo $item["product_code"]; ?></td> <td style="text-align:right;"><?php echo $item["quantity"]; ?></td> <td style="text-align:right;"><?php echo "$ ".$item["price"]; ?></td> <td style="text-align:right;"><?php echo "$ ". number_format($item_price,2); ?></td> <td style="text-align:center;"><a href="student_order.php?schoolname=<?php echo $school_page; ?>?action=remove&product_code=<?php echo $item["product_code"]; ?>" class="btnRemoveAction"><img src="icon-delete.png" alt="Remove Item" /></a></td> </tr> <?php $total_quantity += $item["quantity"]; $total_price += ($item["price"]*$item["quantity"]); } ?> <tr> <td colspan="2" align="right">Total:</td> <td align="right"><?php echo $total_quantity; ?></td> <td align="right" colspan="2"><strong><?php echo "$ ".number_format($total_price, 2); ?></strong></td> <td></td> </tr> </tbody> </table> <?php } else { ?> <div class="no-records">Your Cart is Empty</div> <?php } ?> </div> </div> <br /><br /> <?php include 'footer_student.php'; ?> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="js/jquery-3.4.1.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="js/popper.min.js"></script> <script src="js/bootstrap-4.4.1.js"></script> </body> </html>
hello all, hoping someone can help, i am still pretty new to php and i am stuck on creating a php cart. the code i have below is working, but the problem i am having is i need a store in the session array i am struggling to get my head around it so hoping someone can help here. i am hoping to have $product_id (which is currently in) $option1,2,3 and $text (for a textarea box) heres my function file function get_product() { echo "Hello"; } function add_product() { $product_id = $_POST['product_id']; if ($product_id == 'NULL') { echo "No Product Selected!"; } if (isset($_SESSION['cart'][$product_id])) { $_SESSION['cart'][$product_id]++; echo "<script>window.location='product.php'</script>"; }else{ $_SESSION['cart'][$product_id] = 1; echo "<script>window.location='product.php'</script>"; } } function increase_product() { $product_id = $_GET['product_id']; $_SESSION['cart'][$product_id]++; echo "<script>window.location='cart.php'</script>"; } function decrease_product() { $product_id = $_GET['product_id']; $_SESSION['cart'][$product_id]--; echo "<script>window.location='cart.php'</script>"; } function empty_cart() { unset($_SESSION['cart']); echo "<script>window.location='cart.php'</script>"; } }() my product page (currently with out the extra form items (option1,2,3 etc) include '_class/cart.php';$core->session($mode = 'start');if(isset($_POST['buy'])){$cart->add_product();}?><!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>Product</title></head><body><?php $core->navbar();?><form method="post" action=""><input name="product_id" type="hidden" value="Coffee" /><h1>Cup Of Coffee</h1><p>Buy me you know you want too!</p><input name="buy" type="submit" value="Add To Basket" id="buy" /></form><form method="post" action=""><input name="product_id" type="hidden" value="Biscuits" /><h1>Barrel Of Biscuits</h1><p>When Coffee Just Gives You The Munchies!</p><input name="buy" type="submit" value="Add To Basket" id="buy" /></form></body></html>() my include cart file if (isset($_SESSION['cart'])) {//Session Cart foreach($_SESSION['cart'] as $product_id => $quantity){//Session Array if($quantity < 1){//Quantity Less Then 1 unset($_SESSION['cart'][$product_id]); echo "<script>window.location='cart.php'</script>"; }//End Quantity Less Then 1 if(!$_SESSION['cart']){//Cart Array Empty unset($_SESSION['cart']); echo "<script>window.location='cart.php'</script>"; }//End Cart Array Empty if ($product_id != NULL){//Show Products echo '<h1>' . $product_id . '</h1>'; echo "Quantity <br>"; ?><a href="cart.php?product_id=<?php echo $product_id; ?>&option=increase">+</a> <?php echo $quantity; ?> <a href="cart.php?product_id=<?php echo $product_id; ?>&option=decrease">-</a><?php }//End Show Products }//End Session Array require 'library/paypal.php'; }else{ echo "Your Basket Is Empty"; }//End Session Cart() and my actual cart file include '_class/cart.php';$core->session($mode = 'start');?><!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>Cart</title></head><body><?php$core->navbar(); if (isset($_GET['product_id']['option'])) { $product_id = $_GET['product_id']; $option = $_GET['option']; if ($option == 'decrease'){ $cart->decrease_product(); } if ($option == 'increase'){ $cart->increase_product(); } }elseif (isset($_GET['option'])){ $option = $_GET['option']; if ($option == 'empty'){ $cart->empty_cart(); } }else{ require 'library/cart.php'; }?><p><a href="cart.php?option=empty">Empty Cart</a></p></body></html>() please explain answers or exmples as clear as possible thanks everyone I'm not a programmer but can get buy a little with PHP. My need is a "Shopping Cart" and this is what I would like to see.
I have a MotoCMS web site and would like to but a shopping cart in one page all I need is a "Buy Now" button that can hold the "Item Name" "Price" and "Quantity" and Maybe a selectable option? And a "Cart" for my PayPal that I can put anywhere on the page. If I have to use a database that is OK.
I can put the product pictures and info on the page and then a Buy Now link button would be great.
Does anyone know of a shopping cart that I can use to do this?
Lon
Edited by Lon, 28 September 2014 - 09:07 PM. I have followed a shopping cart tutorial from this website. http://v3.thewatchmakerproject.com/journal/276/ 1. When you refresh the page it adds another item to the cart. 2. I would like to add an image of the products from a folder and the link of the images in the database. Can some one please help. Urgently! Thanks. Need some help on developing the steps below I am working on a website money collection system that consist of stores affiliating to the website and sales their products in the website as independent stores. But I am not sure how to collect the percentage I will deduct per transaction for the stores. I have elaborated two plans but I don't know is those a good ideas in terms of bringing more store in to the website and have them comfortable by the way the website hold business with their stores owners. 1- I was thinking of collecting all the money of transactions into a single paypal account and then try to pay them weekly or in a monthly basis. (I am not sure about this first option because I don't know whether the store owners will be thinking to much into it at the time of making a decision to register and to sell their products in the website. I guess they might think whether the website administrator will delivery the money every week, or if the website administrators are going to steal them etc. So this option I don't know if with some changes can be implemented. 2- The second option was to let each store administer each transaction with their credit/debit machine they have at the store and make the transaction right there and then on a monthly or weekly basis the website administrator charge them for the amount of transaction done during that time period in the website. 3- Third option is create a paypal account for each store and at the moment of the transaction to transfer the money to each store account and have the website paypal account deduct its percentage from the transaction and have the store owner paypal account deduct its part. For healthy business purpose and website grow in terms of acquiring more stores into the website which of those three options would be the best option to choose or if there is another idea even better for business grow and comfortability among stores owner that you guys might have in mind share it. Is selecting an option relevant to the grow of the website or it is? co.ador Hi All I have my shopping cart working correctly, just a small tweak which i would like to make to it. Here is the code i currently use... Code: [Select] echo '<p>' . $fldname . ' <a href="cart.php?action=add&id=' . $row['idinfo'] . '">Add to report</a></p>'; But i want it so that when i click 'Add to report' is does not take me to cart.php, but it still adds the item into the shopping cart. kind of just refeshs the current page? Im sure it is only a small tweak which needs to be done.. Any help would be great -Dream hi all i have a simple shopping cart up and runniing i just cannot think of how to remove all items within the cart i can do a remove one item easily but how woudl i remove all items inmy cart ? Would i delete the cookie id or ? Hey all not been on here for a few years, been out of work. i am back and throwing ,myself straight into the deep end. Is it hard to integrate a shopping cart into a website. Any good example snippets of code to look at? I have a little search but someone always seems to know a better one. Why are almost all tutorials these days you have to pay for (times are getting hard ). Can someone give me name of free shopping cart with auctions(offers) like Open Classifieds ? i make a shopping cart,but it dont show the product i have choose. it suppose to show the product i choose at the cart table but its still show the "Your Cart is empty" result..im still new.i follow the instruction from the youtube.sorry for my bad english.
here is the index2.php code
<? session_start(); require("connection.php"); if(isset($_GET['page'])){ $pages=array("products", "cart"); if(in_array($_GET['page'], $pages)) { $_page=$_GET['page']; }else{ $_page="products"; } }else{ $_page="products"; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> <body> </body> </html> <link rel="stylesheet" href="style2.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script> <script type="text/javascript"> $(function() { }); </script> </head> <body> <div id="container"> <div id="main"> <?php require($_page.".php"); ?> </div> <div id="sidebar"> <h1>Cart</h1> <?php if(isset($_SESSION['cart'])){ $sql="SELECT * FROM products WHERE id_product IN ("; foreach($_SESSION['cart'] as $id => $value) { $sql.=$id.","; } $sql=substr($sql, 0, -1).") ORDER BY name ASC"; $query=mysql_query($sql); while($row=mysql_fetch_array($query)){ ?> <p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p> <?php } }else{ echo "<p>Your Cart is empty</p>"; } ?> </div> </div> </body>and this is the products.php code <?php if(isset($_GET['action']) && $_GET['action']=="add"){ $id=intval($_GET['id']); if(isset($_SESSION['cart'][$id])){ $_SESSION['cart'][$id]['quantity']++; }else{ $sql_s="SELECT * FROM products WHERE id_product={$id}"; $query_s=mysql_query($sql_s); if(mysql_num_rows($query_s)!=0){ $row_s=mysql_fetch_array($query_s); $_SESSION['cart'][$row_s['id_product']]=array( "quantity" => 1, "price" => $row_s['price'] ); }else{ $message="This product id it's invalid!"; } } } ?> <h1>Product List</h1> <?php if(isset($message)){ echo "<h2>$message</h2>"; } ?> <table> <tr> <th>Name</th> <th>Description</th> <th>Price</th> <th>Action</th> </tr> <?php $sql="SELECT * FROM products ORDER BY name ASC"; $query=mysql_query($sql); while ($row=mysql_fetch_array($query)) { ?> <tr> <td><?php echo $row['name'] ?></td> <td><?php echo $row['description'] ?></td> <td><?php echo $row['price'] ?></td> <td><a href="index2.php?page=products&action=add$id=<?php echo $row['id_product'] ?>">Add To Cart</a></td> </tr> <?php } ?> </table> To add items to my shopping cart the user must select a size and then click 'add to basket'. When they do this is adds the item and corresponding size to a string in this format 1s2 where 1 is the id and 2 is the size. Multiple items would look like this 1s2,2s1,1s2 That's all fine. The problem I am now having is getting the code to recognise 1s2 exists twice in the string and putting the $qty to 2 for that item. Here is my current code. Any help on this would be HUGELY appreciated as it's doing my head in! Thanks! Code: [Select] <div id="view_basket"> <ul> <?php $basket = $_SESSION['basket']; if ($basket) { $data = preg_split("#[s,]#", $basket); $data = array_chunk($data, 2); foreach($data as $key => $val) $contents[] = (isset($contents[$data])) ? $contents[$data] + 1 : 1; echo '<form action="basket.php?action=update" method="post">'; echo '<table>'; foreach ($data as $id=>$qty) { $sql = "SELECT * FROM store WHERE id LIKE '$id' AND live LIKE '0'"; $result = mysql_query($sql); while ($rows = mysql_fetch_array($result)) { extract($rows); ?> <li> <div id="view_basket_image"> <img src="<?php echo $rows['indeximage']; ?>" /> </div> <div id="view_basket_title"> <span class="view_basket_brand"><?php echo $rows['brand']; ?> ·</span> <span class="view_basket_description"><?php echo $rows['title']; ?></span> </div> <div id="view_basket_qty"> <input type="text" name="qty<?php echo $id; ?>" value="<?php echo $qty; ?>" size="3" maxlength="3" class="view_basket_qty" /> </div> <div id="view_basket_price"> <span class="view_basket_x">x</span>£<?php echo $rows['price']; ?> <?php /* WORK OUT ITEM WEIGHTS */ $itemweight = $rows['weight'] * $qty; $totalweight += $rows['weight'] * $qty; ?> </div> <div id="view_basket_itemtotal"> <?php $itemtotalprice = $rows['price'] * $qty; $itemtotal = number_format($itemtotalprice, 2, '.', ','); echo '£'; echo $itemtotal; $total += $rows['price'] * $qty; ?> </div> <div class="clear"></div> </li> <?php } } ?> </ul> </div> <?php } ?> Hi everyone, I am building a shopping cart for a group of friends' website. It is a pay-per-view workout website. Basically, they want to let people find workout videos and pay on a per-day basis (1 day, 3 days, 7 days and depending on the video pricing will be different). I have set them up everything except I am having some difficulty with the shopping cart. I took the basics of the shopping cart script from a tutorial I found online and then modified it. The only problem is that most shopping carts send an action and a product id through the URL and into the session for the shopping cart. Well, I need to also send a price id variable so that the cart will know which price to use. This is because they want to offer pricing on a per-day basis. How would I modify the cart.php page to accomplish this? I have tried, but cannot get the entire thing to work. All help is appreciated. Below is my code: Code: [Select] <?php // if no session is set, initiate one if (!isset($_SESSION)) { session_start(); } // see if the customer is logged in //if (!isset($_SESSION['customer_id'])) //{ //header("location:login.php"); //} // connect to the database require_once('connect.php'); // select the database mysql_select_db($database, $connect); // get the video information if (isset($_GET['video_id'])) { $video_id = $_GET['video_id']; } else { $video_id = 1; } // get the action if (isset($_GET['action'])) { $action = $_GET['action']; } else { $action = "empty"; } // get the video pricing information if (isset($_GET['price_id'])) { $price_id = $_GET['price_id']; } else { $price_id = ""; } // do the following code based on the action switch($action) { // if the action is add then add the product id to the cart session array case "add": if (isset($_SESSION['cart'][$video_id])) { $_SESSION['cart'][$video_id]++; } else { $_SESSION['cart'][$video_id] = 1; } break; // if the action is remove then remove the product id from the cart session array case "remove": if (isset($_SESSION['cart'][$video_id])) { $_SESSION['cart'][$video_id]--; if ($_SESSION['cart'][$video_id]==0) { unset($_SESSION['cart']); } } break; // if the action is empty then remove the cart session array completely case "empty": unset($_SESSION['cart']); break; } ?> <!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> <meta name="Description" content="Information architecture, Web Design, Web Standards." /> <meta name="Keywords" content="your, keywords" /> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link rel="stylesheet" href="layout.css" type="text/css" /> <title>Straight Muscle Videos...Pay-Per-View Alpha Male Muscle Worship Videos</title> </head> <body> <!--wrap starts here--> <div id="wrap"> <?php include('header.php'); ?> <?php include('menu.php'); ?> <!--content-wrap starts here--> <div id="content-wrap" class="two-col"> <?php include('sidebar.php'); ?> <!--main content starts here--> <div id="main"> <h1>Shopping Cart</h1> <br /> <?php if (isset($_SESSION['cart'])) { $total = 0; ?> <table width="95%" border="0" cellspacing="0" cellpadding="5"> <?php foreach($_SESSION['cart'] as $video_id => $x) { $query = mysql_query("SELECT VideoId, VideoTitle, PriceId, PriceVideoId, PriceAmount, PriceTimeId, TimeId, TimeTitle, TimeLength FROM Video, Price, Time WHERE VideoId = $video_id AND PriceId = $price_id AND PriceVideoId = $video_id AND PriceTimeId = TimeId", $connect); $row = mysql_fetch_array($query); $video_name = $row['VideoTitle']; $order_length = $row['TimeTitle']; $video_price = $row['PriceAmount']; $line_cost = $video_price * $x; $total_cost = $total + $line_cost; ?> <tr> <td align="left" valign="middle"><?php echo $video_name; ?></td> <td align="center" valign="middle"><?php echo $order_length; ?></td> <td align="center" valign="middle"><?php echo "$".$line_cost; ?></td> <td align="center" valign="middle"><a href="cart.php?action=remove&video_id=<?php echo $video_id; ?>&price_id=<?php echo $price_id; ?>">Remove</a></td> </tr> <?php } ?> <tr> <td colspan="4" bgcolor="#666666"><span class="shopping_cart">Purchase Total: $<?php echo $total_cost; ?></span></td> </tr> </table> <div align="center"> <form id="shopping_cart" method="post" action="confirm.php"> <input type="submit" name="place_order" id="place_order" value="Place Order" /> </form> </div> <div align="center"> <p><a href="search.php">Continue Shopping</a></p> <p><a href="cart.php?action=empty">Empty Cart</a></p> </div> <?php } else { echo "<br /><br /><div align='center'>There are no items in your cart.</div>"; } ?> <!--main content ends here--> </div> <!--content-wrap ends here--> </div> <?php include('footer.php'); ?> <!--wrap ends here--> </div> </body> </html> <?php if (!isset($_SESSION)) session_start(); $qstr = $_SERVER['QUERY_STRING']; parse_str ( $qstr ); ?> <body> <div id="wrap"> <?php require 'header.php';?> <section> <tr><td colspan='2'><h4>Shopping Cart</td><tr> <tr> <td colspan="2"> <table border="0" padding="0" cellspacing="0"> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method ="post" name="cartForm" id="cartForm" onsubmit="return checkSubmit();"> <tr class="trColor"> <td width="200" class="nvtxt"><h4>Item Description</td> <td width="100" class="nvtxt"><h4>Item Price</td> <td width="100" class="nvtxt"><h4>Quantity</td> <td width="100" class="nvtxt"><h4>Item Total</td> </tr> <?php foreach($_SESSION['myCart'] AS $temp) { $itmTot = $temp["qty"] * $temp["price"]; ?> <tr> <td><h4><?= $temp["item"] ?></td> <td><h4>$<?= $temp["price"] ?></td> <td><h4><input type="text" size="3" value="<?= $temp['qty'] ?>" ></td> <td><h4>$<?= $itmTot ?></td> </tr> <?php } ?> <tr class="trColor"> <td colspan="2"> </td> <td class="nvtxt"><h4>Quantity</td> <td class="nvtxt"><h4>Total</td> </tr> <tr> <td colspan="2" /> <td><h4><?= $_SESSION['myToti'] ?></td> <td><h4>$<?= $_SESSION['myTotp'] ?></td> </tr> <tr class="trColor"><td colspan="4"> </td></tr> <tr> <td colspan="4" align="center" height="100"> <a href="index.php">Continue Shopping</a> <BR> <a href=checkout.php>Checkout</a> <BR> <input type="submit" name="submit" id="submit" value="Clear Cart" /> <input type="submit" name="save" id="save" value="Edit Card"/> <?php if(isset($_POST['submit'])){ unset($_SESSION['myCart']); header("Location:index.php"); } ?> <?php if(isset($_POST['save'])){ } ?>Above is Viewing the cart which has two buttons. One which clears the cart and the other which will be used to input a new value such as 0 to change the cart and clear that item from the cart- I don't know how to go about doing this so any help is greatly appreciated. Here's the rest of my code. <!--cart.php--> <?php if (!isset($_SESSION)) session_start(); $qstr = $_SERVER['QUERY_STRING']; parse_str ( $qstr ); ?> <body> <div id="wrap"> <?php require 'header.php';?> <section><h4> <tr><td colspan='2'>Shopping Cart</td><tr> <tr> <td colspan="2"> <table border="0" padding="0" cellspacing="0"> <form action="cartUpdate.php" method="post" name="myForm"> <tr class="trColor"> <td width="200" class="nvtxt"><h4>Item Description</td> <td width="100" class="nvtxt"><h4>Item Price</td> <td width="100" class="nvtxt"><h4>Quantity</td> </tr> <tr height="100"> <td><h4><?= $item ?></td> <td><h4>$<?= $price ?></td> <td> <input type="text" name="qty" id="qty" size="3" placeholder="0"> </td> <input type="hidden" name="item" value="<?= $item ?>"> <input type="hidden" name="price" value="<?= $price ?>"> </tr> <tr class="trColor"><td colspan="3"> </td></tr> <tr> <td colspan="3" align="center" height="100"> <!-- continue shopping --> <a href="index.php">Continue Shopping</a> <!-- update cart --> <a href="javascript:void(0)" onclick="document.myForm.submit();">Add to Cart</a> </td> </tr> </form> </table>Script to update the cart when clicking the button "Add to Cart" <?php if (!isset($_SESSION)) session_start(); $item = $_POST['item']; $price = $_POST['price']; $qty = $_POST['qty']; $_SESSION['myCart'][] = array("item"=>$item,"qty"=>$qty,"price"=>$price); $_SESSION['myToti'] = $_SESSION['myToti'] + $qty; $_SESSION['myTotp'] = $_SESSION['myTotp'] + ($price * $qty); header("Location:cartView.php"); ?> <?php $con=mysqli_connect('localhost', 'root', ''); /* check connection */ if (mysqli_connect_errno($con)) { trigger_error('Database connection failed: ' . mysqli_connect_error(), E_USER_ERROR); } $query = "SELECT `inventory` FROM `bencobricks` . `product`"; $result = mysqli_query($con, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($con), E_USER_ERROR); while($rows=mysqli_fetch_array($result)){ if ($qty - $rows['inventory'] < '0') { echo "Unavailable"; } } ?> Hi guys, I thought of creating a shopping cart in my website. I understand that shopping cart programme is all about $_SESSION or $_COOKIE, but I was just thinking, are there any books or websites which I can learn from? Would appreciate any advice. Thank you Wilson I need a shopping cart that will allow me to restrict access to customers from certain countries. If the customer is from certain other countries they will be directed to contact an office in that country. Any suggestions? |