Welcome Guest, Not a member yet? Register   Sign In
static variable - problem
#1

[eluser]penta997[/eluser]
I have a test function:
Code:
function test()
        {
            static $x = 0;
            $x++;
            echo $x;
        }

and when I call this function, $x still equals 1, the static don't work. Please, help
#2

[eluser]Cristian Gilè[/eluser]
Code:
<?php

class Controller_name extends Controller
{
          
  function __construct()
  {
    parent::__construct();
  }
  
  function test()
  {
    static $x = 0;
    $x++;
    echo $x;
  }  
  
  function index()
  {
    for($i=0;$i<10;$i++)
     $this->test();
  }
  
}

The previous example keeps in memory the static variable and value is incremented. Every new controller instance removes the static variable from memory.


Cristian Gilè
#3

[eluser]penta997[/eluser]
Ok thanks. But what if my functon is more compicated, and when I call this function the data is adding to a sesstion for ex:
Code:
function addCards($id=1)
        {

         static $licznik = 0;
         static $suma = 0;
        
        
         $query = $this->Kategorie_model->get_books_by_ID($id);

         if($query->num_rows() > 0)
          {
              $item = $query->row();

                
              $suma += ($item->BOOK_Price*1);
              $data['suma'] = $suma;
              
               $licznik++;
               $data['ilosc'] = $licznik;
              
              
                
          }
           $this->session->set_userdata($data);
                redirect('ksiegarnia/main');



        }

How many times I must call this function in loop?
#4

[eluser]Cristian Gilè[/eluser]
You can avoid static variables.

If I haven't misunderstood what you are attempting to do, this is a possible solution:

Code:
function addCards($id=1)
        {
        
         $suma = $this->session->userdata('suma') ? $this->session->userdata('suma') : 0;
         $licznik = $this->session->userdata('licznik') ? $this->session->userdata('licznik') : 0;

         $query = $this->Kategorie_model->get_books_by_ID($id);

         if($query->num_rows() > 0)
          {
              $item = $query->row();

              $suma += ($item->BOOK_Price*1);
              $data['suma'] = $suma;
              
              $licznik++;
              $data['licznik'] = $licznik;  
          }

          $this->session->set_userdata($data);
          redirect('ksiegarnia/main');

        }


Cristian Gilè
#5

[eluser]penta997[/eluser]
It's works. But tell me, please what means this line $suma = something ? something : 0.
#6

[eluser]Cristian Gilè[/eluser]
It's the ternary operator. If the session suma is set, $suma gets session suma value otherwise 0.


Cristian Gilè
#7

[eluser]penta997[/eluser]
Ok, thanks for help.
#8

[eluser]penta997[/eluser]
One more question SmileHow to set default value. Becouse if addCarts is not call, the value equals null, not zero but empty place.
#9

[eluser]Cristian Gilè[/eluser]
Code:
$this->session->set_userdata('suma', 0);
$this->session->set_userdata('licznik', 0);

You can set these sessions, for example, when the user is logged in.


Cristian Gilè




Theme © iAndrew 2016 - Forum software by © MyBB