shopping cart-checkout problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jecha
    New Member
    • Feb 2007
    • 3

    shopping cart-checkout problem

    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
  • cassbiz
    New Member
    • Oct 2006
    • 202

    #2
    Originally posted by jecha
    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
    Code:
    <?
    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_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

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

    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.

    Code:
    function show_array($array) {
        foreach ($array as $value) {
            if (is_array($value)) {
                show_array($value);
            } else {
                echo $value . "<br>";
            }
        }
    }
    
    show_array($array);

    Comment

    Working...