Jump to content

Display Shopping Cart $_SESSION variables and calculate total price?


luckyirish470

Recommended Posts

Hello,

I'm trying to get my shopping cart script to display the item picture, info, and price for every item selected while calculating a total price for all the items as the cart is updated.  The two related scripts are in .inc files and everything looks right to me...but when I run the scripts nothing is displayed and the total price displayed is $0.  I am using $_SESSION variables and my guess is I've done something wrong with them...but I have been staring at the code for weeks now trying to figure out what it is and I am running out of hair to pull out!  Any help would be GREATLY appreciated, thank you!  Here are the script .inc files....

------------------------------------------------------------------------------------------------------

PRODUCT DISPLAY PAGE......

------------------------------------------------------------------------------------------------------

<?php

session_start();

 

if(!isset($_SESSION['auth']) or $_SESSION['auth'] != "yes")

  {

    header("location: loginfinal.php");

header('Content-type: image/jpg');

    exit();

  } ?>

<?php

 

/*  Program name: page_links.inc

*  Description:  file builds a list of links from the database

*/

ini_set("display_errors","on");

error_reporting(E_ALL | E_STRICT);

ini_set("include_path","./includes");

?>

<html>

<head><title>Customer Products</title></head>

<body>

<?php

include("dbinfo.inc");

$cxn = mysqli_connect($host,$user,$passwd,$dbname)

        or die ("couldn't connect to server");

$query = "SELECT * FROM products WHERE category='$_GET[category]'";

$result = mysqli_query($cxn,$query)

        or die("Couldn't execute query ".mysqli_error($cxn));

while($row = mysqli_fetch_assoc($result))

{

  extract($row);

 

 

  echo "

<ul><li> <img src='images/$thumbnail'/> <br />  <a href='shoppingcart.php?productnumber=$productnumber&thumbnail=$thumbnail&productname=$productname&price=$price'>$productname </a><br /> $description <br/> \$$price </li>

</ul>";

 

}

 

?>

</body></html>

 

------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------

SHOPPING CART SCRIPT

------------------------------------------------------------------------------------------------------

<?php

session_start();

if(!isset($_SESSION['auth']) or $_SESSION['auth'] != "yes")

  {

    header("location: loginfinal.php");

    exit();

  } ?>

<?php

 

/*  Program name: shoppingcart.inc

*  Description:  file builds a list of links from the database

*/

ini_set("display_errors","on");

error_reporting(E_ALL | E_STRICT);

ini_set("include_path","./includes");

?>

<html>

<head><title>Shopping Cart</title></head>

<body>

<?php

$total=0;

include("dbinfo.inc");

$cxn = mysqli_connect($host,$user,$passwd,$dbname)

        or die ("couldn't connect to server");

 

if(!isset($_SESSION['items']))

{

  $_SESSION['items'][]= array('productnumber'=>'$productnumber', 'thumbnail'=>'$thumbnail', 'productname'=>'$productname', 'price'=> '$price');

  $items = $_SESSION['items'];

}

 

 

 

 

foreach($_SESSION['items'] as $productnumber)

{

$query = "SELECT * FROM products WHERE productnumber='$_SESSION[items]'"; //should be $_Get[productnumber]

 

$result = mysqli_query($cxn,$query)

        or die("Couldn't execute query ".mysqli_error($cxn));

 

 

 

while($row = mysqli_fetch_assoc($result))

{

  extract($row);

 

  echo "

<ul><li><img src='images/$_SESSION[thumbnail]'/> <br />  $_SESSION[productname]  <br/> \$$_SESSION[price] </li>

</ul>";

$total= $total + $price;

}

}

?>

<?php

 

 

echo"

<h3> Total Price </h3>  \$$total <br />

<a href='thankyou.php'>Submit Order </a>

<a href='products.php?total=$total'>Keep Shopping </a>";

?>

</body>

</html>

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.