Old 01-05-2007, 01:26 AM   #1 (permalink)
Sun? What Sun?
 
Dennis56's Avatar
 
Join Date: Aug 2006
Location: дома
Posts: 951
Reputation: 10
Default Some help with php classes

Could someone tell me whats wrong with this code?
PHP Code:
class Cart {
   var 
$items;

   function 
add_item($item_id$num$money$cost) {
         
$this->items[$num] *= $cost;
        if(
$this->items[$money] > $cost){
           
$this->items[$cost] -= $money;
           
$this->items[$item_id] += $num;
           unset(
$this->items[$cost]);
           return 
true;
       }elseif(
$this->items[$money] == $cost){
           unset(
$this->items[$money]);
           unset(
$this->items[$cost]);
           
$this->items[$item_id] += $num;         
           return 
true;
       }else {
           unset(
$this->items[$cost]);
           return 
false;
       }
   }

   function 
remove_item($item_id$num) {
       if (
$this->items[$item_id] > $num) {
           
$this->items[$item_id] -= $num;
           return 
true;
       } elseif (
$this->items[$item_id] == $num) {
           unset(
$this->items[$item_id]);
           return 
true;
       } else {
           return 
false;
       }
   }
    function 
print_cart(){
        
$keys array_keys($this->items);
        foreach (
$keys as $key) {
            print 
$key"=";
            print 
$this->items[$key]."<br>";
    }
    }
}
$ncart = new Cart;
$ncart->add_item("aa"12010);
$ncart->add_item("bb"72010);
$ncart->print_cart(); 
The output of the code is
PHP Code:
1=0
7
=
it should be
PHP Code:
aa=1
bb
=
It appears to only show the cost and not the name of the item

It for learning purposes not actually going to use it anywhere
Dennis56 is offline  
Old 01-05-2007, 01:59 AM   #2 (permalink)
McFox Sandwich
 
Join Date: Jul 2006
Posts: 463
Reputation: 10
Default

Umm as you know I don't know PHP but just trying to help.
Code:
$ncart = new Cart; 
$ncart->add_item("aa", 1, 20, 10); 
$ncart->add_item("bb", 7, 20, 10); 
$ncart->print_cart();
I'm not sure if it is case sensative but it looks like you have Cart and cart there. Maybe change them both to lowercase or capital?
Cj Shadows is offline  
Old 01-05-2007, 02:06 AM   #3 (permalink)
Sun? What Sun?
 
Dennis56's Avatar
 
Join Date: Aug 2006
Location: дома
Posts: 951
Reputation: 10
Default

No its different the function is with a lowercase and the class is uppercase so its right the way i have it

Last edited by Dennis56; 01-05-2007 at 02:11 AM.
Dennis56 is offline  
Old 01-05-2007, 02:10 AM   #4 (permalink)
McFox Sandwich
 
Join Date: Jul 2006
Posts: 463
Reputation: 10
Default

Oh okay sorry I couldnt help you :/
Cj Shadows is offline  
Old 01-05-2007, 02:12 AM   #5 (permalink)
Sun? What Sun?
 
Dennis56's Avatar
 
Join Date: Aug 2006
Location: дома
Posts: 951
Reputation: 10
Default

No Problem im suspecting something with the array and the foreach loop

ok nvm guys i fixed it i should have wrote it like this
PHP Code:
class Cart {
   var 
$items;
   var 
$money;

   function 
add_item($item_id$num$cost) {
         
$money 20;
        if(
$money $cost){
           
$this->items[$cost] -= $money;
           
$this->items[$item_id] += $num;
           unset(
$this->items[$cost]);
           return 
true;
       }elseif(
$money == $cost){
           unset(
$money);
           unset(
$this->items[$cost]);
           
$this->items[$item_id] += $num;         
           return 
true;
       }else {
           unset(
$this->items[$cost]);
           return 
false;
       }
   }

   function 
remove_item($item_id$num) {
       if (
$this->items[$item_id] > $num) {
           
$this->items[$item_id] -= $num;
           return 
true;
       } elseif (
$this->items[$item_id] == $num) {
           unset(
$this->items[$item_id]);
           return 
true;
       } else {
           return 
false;
       }
   }
    function 
print_cart(){
        
$keys array_keys($this->items);
        foreach (
$keys as $key) {
            print 
$key"=";
            print 
$this->items[$key]."<br>";
    }
    }
}
$ncart = new Cart;
$ncart->add_item("aa"110);
$ncart->add_item("bb"710);
$ncart->print_cart(); 

Last edited by Dennis56; 01-05-2007 at 05:31 AM. Reason: -=Doublepost=-
Dennis56 is offline  
 

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:12 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.6.0
OnRPG, Copyright ©2003-2011, Game Entertainment Enterprises