473,703 Members | 3,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

shopping cart-checkout problem

3 New Member
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('f unctions.inc.ph p');
session_start() ;
do_html_header( "Checkout") ;
$cart = $_SESSION['cart'];
if($cart&&array _count_values($ cart))
{
display_cart($c art,false,0);
display_checkou t_form($HTTP_PO ST_VARS);
}
else
echo"<p>There are no items in your cart";
do_html_url("in dex.php","Conti nue 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_val ues() [function.array-count-values]: The argument should be an array in C:\...shopping_ cart\checkout.p hp 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 writeShoppingCa rt() {

$cart = $_SESSION['cart'];
if (!$cart) {
return '<p>You have no items in your shopping cart</p>';
} else {
// Parse the cart session variable
$items = explode(',',$ca rt);
$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(',',$ca rt);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($content s[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.ph p?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&i d='.$id.'" class="r">Remov e</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$'.($pric e * $qty).'</td>';
$total += $price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>Z$'.$to tal.'</strong></p>';
$output[] = '<div><button type="submit">U pdate 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 7294
cassbiz
202 New Member
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_val ues() [function.array-count-values]: The argument should be an array in C:\...shopping_ cart\checkout.p hp 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
2899
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" until they decide they are ready to send a mass email to all the people in their cart. They will be able to view the people in their cart, add and remove as desired, and then simply fill out a form and the message will be mailed to all those in...
2
3027
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 and works good. I need to create a shopping cart in ASP and any useful info or tips would be helpful. Thanks. Erik Roessing
2
2858
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 im going. Just a couple of hints will do, Im familiar with vb script but not java based code , and im wondering how to store what they select so I can move around different product ranges. Don
2
3311
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 storing the necessary product information. There could be several fields involved... Then there ought to be another javascript method that can create a new document based on the list a user has built selecting product from several
1
3578
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 be able to come along and add it to their shopping cart. thanks joy
5
20519
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 you can reuse. Those are good but I'd like to learn how to create one from scratch. Thanks. VM
2
1649
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 own. Before diving in though, I'd like to have any recommendations or suggestions from any of you who have done or seen "bare bones" shopping carts/store fronts. I'd like to integrate it with an existing SQL Server database that holds the customer,...
2
2299
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 screen generation routines (already written) that take an XML stream as input from various "search for products" forms. That way I can run queries in one window and display the dynamic results in another. The searching functions will probably
6
1874
by: frank | last post by:
can anyone point me to a free shopping-cart script?
1
7669
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 help. Below is the code for both shopping cart, products and function.php shopping cart code: <? include("db.php"); include("functions.php"); if($_REQUEST=='delete' && $_REQUEST>0){ remove_product($_REQUEST);
0
8659
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9240
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8952
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5920
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2426
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2055
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.