472,803 Members | 975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,803 software developers and data experts.

shopping cart-checkout problem

3
I'm implementing a shopping cart but am having a problem in checking out a person who has added item in his/her shopping busket.The code for the checkout.php script is given below
<?
require_once('functions.inc.php');
session_start();
do_html_header("Checkout");
$cart = $_SESSION['cart'];
if($cart&&array_count_values($cart))
{
display_cart($cart,false,0);
display_checkout_form($HTTP_POST_VARS);
}
else
echo"<p>There are no items in your cart";
do_html_url("index.php","Continue Shopping");
?>
After a person has added vehicles to his/her cart and now wants to checkout, the checkout.php script generates this error:

'There are no items in your cart Continue Shopping :PHP Warning: array_count_values() [function.array-count-values]: The argument should be an array in C:\...shopping_cart\checkout.php on line 12' where line 12 is

if($cart&&array_count_values($cart))
I have realised that the code is not even reading whether or not a person has added something in his/her cart , but don't no where the error is.
The fucntions in the shopping cart are found in the function.inc below:

function.inc

<?php
$table_width = '995';
function do_html_header($title = '')
{
// print an HTML header including cute logo :)

global $table_width;
?>
<html>
<head>
<title><?php echo $title?></title>
</head>
<body>

<table width = <?php echo $table_width?> cellspacing = 0 cellpadding = 6>
<tr>
<td bgcolor = "#0066CC" width = 110>
</td><td bgcolor = "#0066CC">
<h1><?php echo $title?></h1></td>
</tr>
</table>

<?php
}

function do_html_url ($url, $title) {
?>
<a href="<?=$url?>"><?=$title?></a>
<?
}

function writeShoppingCart() {

$cart = $_SESSION['cart'];
if (!$cart) {
return '<p>You have no items in your shopping cart</p>';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
}
}

function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM vehicles WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$model.' '.$make.'</td>';
$output[] = '<td>Z$'.$price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>Z$'.($price * $qty).'</td>';
$total += $price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>Z$'.$total.'</strong></p>';
$output[] = '<div><button type="submit">Update cart</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
?>
your help is greatly appreciated
May 7 '07 #1
1 7162
cassbiz
202 100+
I'm implementing a shopping cart but am having a problem in checking out a person who has added item in his/her shopping busket.The code for the checkout.php script is given below
Expand|Select|Wrap|Line Numbers
  1. <?
  2. require_once('functions.inc.php');
  3. session_start();
  4. do_html_header("Checkout");
  5. $cart = $_SESSION['cart'];
  6. if($cart&&array_count_values($cart))
  7. {
  8. display_cart($cart,false,0);
  9. display_checkout_form($HTTP_POST_VARS);
  10. }
  11. else
  12.  echo"<p>There are no items in your cart";
  13.  do_html_url("index.php","Continue Shopping");
  14.  ?>
  15.  
After a person has added vehicles to his/her cart and now wants to checkout, the checkout.php script generates this error:

'There are no items in your cart Continue Shopping :PHP Warning: array_count_values() [function.array-count-values]: The argument should be an array in C:\...shopping_cart\checkout.php on line 12' where line 12 is

if($cart&&array_count_values($cart))

I have realised that the code is not even reading whether or not a person has added something in his/her cart , but don't no where the error is.
The fucntions in the shopping cart are found in the function.inc below:

function.inc

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $table_width = '995';
  3. function do_html_header($title = '')
  4. {
  5.  // print an HTML header including cute logo :)
  6.  
  7.  global $table_width;
  8. ?>
  9.  <html>
  10.  <head>
  11.  <title><?php echo $title?></title>
  12.  </head>
  13.  <body>
  14.  
  15. <table width = <?php echo $table_width?> cellspacing = 0 cellpadding = 6>
  16.  <tr>
  17.  <td bgcolor = "#0066CC" width = 110>
  18.  </td><td bgcolor = "#0066CC">
  19. <h1><?php echo $title?></h1></td>
  20.  </tr>
  21.  </table>
  22.  
  23. <?php
  24. }
  25.  
  26. function do_html_url ($url, $title) {
  27. ?>
  28. <a href="<?=$url?>"><?=$title?></a> 
  29. <?
  30. }
  31.  
  32. function writeShoppingCart() {
  33.  
  34.     $cart = $_SESSION['cart'];
  35.     if (!$cart) {
  36.         return '<p>You have no items in your shopping cart</p>';
  37.     } else {
  38.         // Parse the cart session variable
  39.         $items = explode(',',$cart);
  40.         $s = (count($items) > 1) ? 's':'';
  41.         return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
  42.     }
  43. }
  44.  
  45. function showCart() {
  46.     global $db;
  47.     $cart = $_SESSION['cart'];
  48.     if ($cart) {
  49.         $items = explode(',',$cart);
  50.         $contents = array();
  51.         foreach ($items as $item) {
  52.             $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
  53.         }
  54.         $output[] = '<form action="cart.php?action=update" method="post" id="cart">';
  55.         $output[] = '<table>';
  56.         foreach ($contents as $id=>$qty) {
  57.             $sql = 'SELECT * FROM vehicles WHERE id = '.$id;
  58.             $result = $db->query($sql);
  59.             $row = $result->fetch();
  60.             extract($row);
  61.             $output[] = '<tr>';
  62.             $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
  63.             $output[] = '<td>'.$model.'  '.$make.'</td>';
  64.             $output[] = '<td>Z$'.$price.'</td>';
  65.             $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
  66.             $output[] = '<td>Z$'.($price * $qty).'</td>';
  67.             $total += $price * $qty;
  68.             $output[] = '</tr>';
  69.         }
  70.         $output[] = '</table>';
  71.         $output[] = '<p>Grand total: <strong>Z$'.$total.'</strong></p>';
  72.         $output[] = '<div><button type="submit">Update cart</button></div>';
  73.         $output[] = '</form>';
  74.     } else {
  75.         $output[] = '<p>You shopping cart is empty.</p>';
  76.     }
  77.     return join('',$output);
  78. }
  79. ?>
  80.  
your help is greatly appreciated

You need to ensure that the items in the cart are actually there.

If your view cart is working then it is not carrying over to the checkout.

You can also check to see that the items are stored within the array. I recently used the below script to find an error in one of my scripts.

Expand|Select|Wrap|Line Numbers
  1. function show_array($array) {
  2.     foreach ($array as $value) {
  3.         if (is_array($value)) {
  4.             show_array($value);
  5.         } else {
  6.             echo $value . "<br>";
  7.         }
  8.     }
  9. }
  10.  
  11. show_array($array);
  12.  
May 7 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Robert | last post by:
Hello all.. Im a beginner in php and looking to do something similar to a shopping cart system. I want users to be able to view profiles on people and be able to add them to the "shopping cart"...
2
by: Erik | last post by:
Does anybody know of a free ASP shopping cart or some free ASP code to help someone get a shopping cart started. Or does anybody have any experience using a ASP shopping cart that is inexpensive...
2
by: Don Grover | last post by:
I am retrieving costs and product id's from a sql db. and need to build a shopping cart around it. How do I store the selected items and qty req so I can move into another catalog and total up as...
2
by: Paul Bruneau | last post by:
Hi, I hope someone can help me make a working shopping cart, as a learning tool. If I have a "Product Demo" html page with a "Buy Me" button, there must be a simple javascript method of...
1
by: madison | last post by:
Hi, I am trying to start a website using paypals shopping cart function. If i have 10 items and they sell out, how do I make it so the item is then listed as sold out. The next person would not...
5
by: VM | last post by:
I'm interested in creating a simple shopping cart in C#. Are there sites that show you, step by step, how to create a simple cart? The sites I've found only show you pre-made shopping carts that...
2
by: Guadala Harry | last post by:
I'm looking to implement some *minimal* shopping cart. I've looked at a few 3rd party packages, and they're all way too involved for what I need. So, I'm thinking it might make sense to roll my...
2
by: G.E.M.P | last post by:
High Level Session Handling Design for a Shopping cart 0) What am I missing? 1) How does OSCommerce do it? I'm thinking about building a shopping cart from scratch, using a library of dynamic...
6
by: frank | last post by:
can anyone point me to a free shopping-cart script?
1
by: BenKen | last post by:
Hi all pls I'm having great difficulty in making my shopping cart work. I am a newbie in php with little understanding of it. Add to cart button doesn't work and shopping cart in general doesn't. Pls...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.