Jump to content

Recommended Posts

Hi all I am having troubles integrating a shopping cart.

$action = (isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view';
switch ($action) {
    case 'add' :
        addToCart();
        break;
    case 'update' :
        updateCart();
        break;
    case 'delete' :
        deleteFromCart();
        break;
    case 'view' :
}

Allows me to Delete and Update but not Add an item.

 

[code=php:0]$action = (isset($_POST['action']) && $_POST['action'] != '') ? $_POST['action'] : 'view';

Allows me to ADD an item to the cart(add action passed from previous page via form)  but not Delete or UPDATE

 

Im not sure if I should have attached the pages or just pasted them but heres the code

 

cart.php

<?php
require_once 'config.php';
require_once 'cart-functions2.php';
require_once 'functions.php';
require_once 'database.php';
$ID = $_POST["ID"];
$pSize = $_POST["size"];
$pcolour = $_POST["colour"];
?>
<?php
$action = (isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view';
switch ($action) {
    case 'add' :
        addToCart();
        break;
    case 'update' :
        updateCart();
        break;
    case 'delete' :
        deleteFromCart();
        break;
    case 'view' :
}
$ID = $_POST["ID"];
$cartContent = getCartContent();
$numItem = count($cartContent);

$pageTitle = 'Shopping Cart';


// show the error message ( if we have any )
displayError();

if ($numItem > 0 ) {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] . "?action=update"; ?>" method="post" name="frmCart" id="frmCart">
<table width="780" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
  <tr class="entryTableHeader">
   <td colspan="2" align="center">Item</td>
   <td align="center">Unit Price</td>
   <td width="75" align="center">Quantity</td>
   <td align="center">Total</td>
  <td width="75" align="center"> </td>
</tr>
<?php
$subTotal = 0;
for ($i = 0; $i < $numItem; $i++) {
    extract($cartContent[$i]);
    $productUrl = "index.php";
    $subTotal += $price * $ct_qty;
?>
<tr class="content">
  <td width="80" align="center"></td>
  <td><?php echo $pDesc; ?></td>
   <td align="right"><?php echo displayAmount($price); ?></td>
  <td width="75"><input name="txtQty[]" type="text" id="txtQty[]" size="5" value="<?php echo $ct_qty; ?>" class="box" onKeyUp="checkNumber(this);">
  <input name="hidCartId[]" type="hidden" value="<?php echo $ct_id; ?>">
  <input name="hidProductId[]" type="hidden" value="<?php echo $ID; ?>">
  </td>
  <td align="right"><?php echo displayAmount($price * $ct_qty); ?></td>
  <td width="75" align="center"> <input name="btnDelete" type="button" id="btnDelete" value="Delete" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=delete&cid=$ct_id"; ?>';" class="box">
  </td>
</tr>
<?php
}
?>

 

cart-functions

<?php
require_once 'config.php';

/*********************************************************
*                 SHOPPING CART FUNCTIONS
*********************************************************/

function addToCart()
{
$sid = session_id();
$ID = $_GET["ID"];
$pSize = $_GET["size"];
$pcolour = $_GET["colour"];   
    
    // current session id
    
    
    // check if the product is already
    // in cart table for this session
    $sql = "SELECT ID
            FROM cart
            WHERE ID = '$ID' AND ct_session_id = '$sid'";
    $result = dbQuery($sql);
    
    if (dbNumRows($result) == 0) {
        // put the product in cart table
        $sql = "INSERT INTO cart (ID, ct_qty, ct_session_id, ct_date)
                VALUES ('$ID', 1, '$sid', NOW())";
        $result = dbQuery($sql);
    } else {
        // update product quantity in cart table
        $sql = "UPDATE cart
                SET ct_qty = ct_qty + 1
                WHERE ct_session_id = '$sid' AND ID = '$ID'";        
                
        $result = dbQuery($sql);        
    }    
   
    header('Location: cart2.php'); 
                 
}

/*
    Get all item in current session
    from shopping cart table
*/
function getCartContent()
{
    $cartContent = array();

    $sid = session_id();
    $sql = "SELECT ct.ct_id, ct.ID, ct.ct_size, ct.ct_colour, ct.ct_qty, pd.pDesc, pd.price, pd.ID
            FROM cart ct, products pd
            WHERE ct_session_id = '$sid' AND ct.ID = pd.ID"; 
    
    $result = dbQuery($sql);
   
    while ($row = dbFetchAssoc($result)) {
    
        $cartContent[] = $row;
    }

    return $cartContent;
}

/*
    Remove an item from the cart
*/
function deleteFromCart($cartId = 0)
{
    if (!$cartId && isset($_GET['cid']) && (int)$_GET['cid'] > 0) {
        $cartId = (int)$_GET['cid'];
    }

    if ($cartId) {    
        $sql  = "DELETE FROM cart
                 WHERE ct_id = $cartId";

        $result = dbQuery($sql);
    }
    
    header('Location: cart2.php');    
}

/*
    Update item quantity in shopping cart
*/
function updateCart()
{
    $cartId     = $_POST['hidCartId'];
    $productId  = $_POST['hidProductId'];
    $itemQty    = $_POST['txtQty'];
    $numItem    = count($itemQty);
    $numDeleted = 0;
    $notice     = '';
    
    for ($i = 0; $i < $numItem; $i++) {
        $newQty = (int)$itemQty[$i];
        if ($newQty < 1) {
            // remove this item from shopping cart
            deleteFromCart($cartId[$i]);    
            $numDeleted += 1;
        }
                            
            // update product quantity
            $sql = "UPDATE cart
                    SET ct_qty = $newQty
                    WHERE ct_id = {$cartId[$i]}";
                
            dbQuery($sql);
       
    }

    exit;
}

function isCartEmpty()
{
    $isEmpty = false;
    
    $sid = session_id();
    $sql = "SELECT ct_id
            FROM cart ct
            WHERE ct_session_id = '$sid'";
    
    $result = dbQuery($sql);
    
    if (dbNumRows($result) == 0) {
        $isEmpty = true;
    }    
    
    return $isEmpty;
}

/*
    Delete all cart entries older than one day
*/
function deleteAbandonedCart()
{
    $yesterday = date('Y-m-d H:i:s', mktime(0,0,0, date('m'), date('d') - 1, date('Y')));
    $sql = "DELETE FROM cart
            WHERE ct_date < '$yesterday'";
    dbQuery($sql);        
}

?> 

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/224460-cart-problem-adddeleteupdate-items/
Share on other sites

It looks like you are mixing GET and POST

 

just use $_REQUEST['action'] as this will use both

$action = (isset($_REQUEST['action']) && $_REQUEST['action'] != '') ? $_REQUEST['action'] : 'view';
switch ($action) {
    case 'add' :
        addToCart();
        break;
    case 'update' :
        updateCart();
        break;
    case 'delete' :
        deleteFromCart();
        break;
    case 'view' :
}

 

 

 

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.