0

[I'm learning PHP now and using this Polish site: ][1]http://phpkurs.pl/ponowne-uzycie-kodu/

I'm stuck on this example:

<?php

class Koszyk
{
var $artykuly;

   function dodaj($numer, $ilosc)
   {
      $this->artykuly["$numer"] += $ilosc;
   }

   function usun($numer, $ilosc)
   {
      if($this->artykuly["$numer"]>$ilosc)

         $this->artykuly["$numer"]-=$ilosc;

      else

         $this->artykuly["$numer"]=0;
   }

   function wyswietl()
   {
      while(list($k, $v) = each($this->artykuly))
         if($v>0)
            echo "Artykul nr $k - $v sztuk<br>";
   }
}

$koszyk = new Koszyk;
$koszyk->dodaj('20', 2);
$koszyk->dodaj('12', 4);
$koszyk->dodaj('20', 5);
$koszyk->usun('12', 4);
$koszyk->wyswietl();
?>

I know that var is not needed because it was used in PHP 4, but the code is not working and I'm getting errors. I can't figure out what is the problem. I think it's something simple but it is example so it should be working.

Cisum Inas
  • 11,552
  • 11
  • 40
  • 55
obiektywny
  • 11
  • 3

1 Answers1

0

make these changes in your code it works for me

   var $artykuly   =   array();

    function dodaj($numer, $ilosc)
       {
        if(!isset($this->artykuly[$numer])){
             $this->artykuly[$numer]="";
           }

          $this->artykuly[$numer] += $ilosc;
       }

the $this->artykuly[$numer] value is not declared only after declaring you can use

$this->artykuly[$numer] += $ilosc; statment

Ronser
  • 1,855
  • 6
  • 20
  • 38