Current location: Hot Scripts Forums » Programming Languages » PHP » Php Help, shopping cart!


Php Help, shopping cart!

Reply
  #1 (permalink)  
Old 05-30-10, 09:29 PM
cerapax cerapax is offline
Newbie Coder
 
Join Date: Apr 2010
Posts: 23
Thanks: 2
Thanked 1 Time in 1 Post
Php Help, shopping cart!

Hi everyone,

I just can't seem to be able to delete, add or modify on my shopping cart!

Everytime I press on the "add to cart" button it adds ok, but then I can't remove or modify.

I'll provide whatever is needed to be able to receive help!

can anyone help please?

==================================================


PHP Code:

<?php
session_start
();
mysql_connect('localhost''root''') or die(mysql_error ());
mysql_select_db("phplogin") or die(mysql_error());
?>

<?php

$cart 
$_SESSION['cart'];
$action $_GET['action'];

    switch (
$action)
        {
            
            case
'add':
            
                if(
$cart)
                    {
                        
$cart .= ','.$_GET['id'];
                    }
                
                    else
                        {
                            
$cart=$_GET['id'];
                        }
                        
                        break;
            
            
            case
'delete':
            
                if(
$cart)
                    {
                        
$items explode(',',$cart);
                        
$newcart '';
                        
                        foreach (
$items as $item)
                                {    
                                    if(
$_GET['id'] != '$item')
                                        {
                                        
                                        if(
$newcart != '')
                                            {
                                                
$newcart .= ','.$item;
                                            }
                                                else
                                                {
                                                    
$newcart $item;
                                                }
                                        }
                                }
                    }
                    
                    break;
                    
            case 
'update':
                if (
$cart
                {
                    
$newcart '';
                    
                    foreach (
$_GET as $key => $value)
                        {
                
                        
                            if (
stristr($key,'qty')) 
                            {
                                
$id str_replace('qty','',$key);
                                
$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
                                
$newcart '';
                            }
                            echo 
$items;
                                foreach (
$items as $item
                                {
                                    
                                    if (
$id != $item
                                        {
                                            
                                        if (
$newcart != ''
                                            {
                                                
$newcart .= ','.$item;
                                            } 
                                            
                                            else 
                                                {
                                                    
$newcart $item;
                                                }
                                        }
                                }
                        }
                }
                    
                    for (
$i=1;$i<=$value;$i++) 
                        {
                        
                        if (
$newcart != ''
                            {
                            
$newcart .= ','.$id;
                            }
                            
                            else 
                                {
                                
$newcart $id;
                                }
                        }
                        break;
                }
                
    
//$cart = $newcart;
    
    
$_SESSION['cart'] = $cart;

?>

<?php

function displayShoppingCart()
{
    
$cart $_SESSION['cart'];
        if (!
cart)
            {
                return 
'<p> You have no items in your shopping cart </p>';
            }
        
            else
                {
                    
$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>";
                }

}

echo 
displayShoppingCart();

?>




<?php

function showCart() {
    
$cart $_SESSION['cart'];
    
    if (
$cart) {
        
$items explode(',',$cart);
        
$contents = array();
        
        foreach (
$items as $item) {
            
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1;
        }
        
$output[] = '<form action="cart.php?action=update" method="get" id="cart">';
        
$output[] = '<table>';
        
        foreach (
$contents as $id=>$qty) {
            
$sql 'SELECT * FROM products WHERE Prodid = '."'".$id."'";
            
            
            
$result mysql_query($sql) or die(mysql_error());
            
$row mysql_fetch_array($result);
            
            
extract($row);
            
$output[] = '<tr>';
            
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
            
/*$output[] = '<td>'.$title.' by '.$author.'</td>';*/
            
$output[] = '<td>&pound;'.$price.'</td>';
            
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
            
$output[] = '<td>&pound;'.($price $qty).'</td>';
            
$total += $price $qty;
            
$output[] = '</tr>';
        }
        
$output[] = '</table>';
        
$output[] = '<p>Grand total: &pound;'.$total.'</p>';
        
$output[] = '<div><button type="submit">Update cart</button></div>';
        
$output[] = '</form>';
    } else {
        
$output[] = '<p>Your shopping cart is empty.</p>';
    }
    return 
join('',$output);
}

echo 
showCart();



?>

Last edited by cerapax; 05-30-10 at 09:34 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-31-10, 05:23 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
PHP Code:

'$item' 

is causing it: the value of that string is actually $item and not the value of the variable $item
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What Shopping Cart Should I Use brookey12 General HotScripts Site Discussion 5 10-12-09 07:10 PM
Megento shopping cart integration sandeep.kumar PHP 0 01-14-09 05:46 AM
MySQL & PHP Shopping cart question mcrob PHP 9 04-14-08 05:07 PM
Custom Shopping Cart ($$$$) SpidersandWebs Job Offers & Assistance 5 11-16-05 12:54 AM
pro shopping cart pi3rc3 Script Requests 0 06-09-05 08:36 PM


All times are GMT -5. The time now is 03:57 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.