Jump to content

Recommended Posts

Hello, I'm trying to create a simple database driven shopping cart that merely displays the selected items and adds up the total price.  I think the error is in my $_SESSION variable but for the life of me I can't figure out what it is.  Right now the script can tell the number of items the user has put in the cart, but the script just displays the last item added to the cart over and over again regardless of whether or not the user selects different items(the number of times the item is displayed is equal to the number of total items the user has placed in the cart)  The total price calculation is working, but it is adding the prices for the same item over and over again (once again, this item that is displayed repeatedly is last item the user added to the cart) When I run the "print $_SESSION[items]' at the top of the code, the only thing that is displayed is "Array."  Any ideas where I'm going wrong?  Thank you very much in advance!

 

Here is the code that gets the information for the actual shopping cart.  This script displays the items the users can select to put in their shopping cart...

........................................................................

<?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>

 

...

End first script

...

 

Now here is the actual shopping cart script...

 

 

<?php

session_start();

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

  {

    header("location: loginfinal.php");

    exit();

  } ?>

<?php

print $_SESSION[items];

/*  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');

}

$_SESSION['items'][] = $_GET['productnumber'];

 

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

{

$query = "SELECT * FROM products WHERE productnumber='$_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/$_GET[thumbnail]'/> <br />  $_GET[productname]  <br/> \$$_GET[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>

 

...

 

 

Here is a link to the live site....http://notestotheman.com/php/lesson6/finalcountdown/loginfinal.php

username=bambi

password=1234567

Link to comment
https://forums.phpfreaks.com/topic/202122-shopping-cart-problem/
Share on other sites

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

{     

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

}

 

maybe...

$query = "SELECT * FROM products WHERE productnumber='$cat_id'";

 

I am having a hard time following the way you write code so I apologize if im wrong I will keep looking at it to see if i come up with anything else

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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