Welcome Guest, Not a member yet? Register   Sign In
How to store information in a variable in a Model
#1

[eluser]KJTED[/eluser]
I have a Model in which i have some functions and variables. I want to be able to store some information in those variables using the functions in the model... that works fine, but the next time i need access to the info stored in those variables the variables have been wiped.

I was wondering how I can stop that from happening?

I'm trying to store an array of strings but everytime I access that array it's wiped, despite being set using one of the other Model functions.

Help on this would be great. It sounds like such a simple thing.
#2

[eluser]coolfactor[/eluser]
Please provide code examples.

Are you using "class" variables? They would retain the value.

Code:
class Some_model extends Model {
    var $a_class_variable = 'default value';

    function a_function () {
        $this->a_class_variable = 'new value';
    }
}

Another option is to use static variables if they only need to be _accessed_ inside a given function.
#3

[eluser]KJTED[/eluser]
Sorry, here is the example.

Code:
class Listings extends Model {

    var $id = '';
    var $image = '';
        var $sortArray;
    
    function Listings()
    {
        parent::Model();
    }
    
    function get_listings($num, $offset ,$sortArray = NULL)
    {    
                $this->sortArray = $sortArray;
        if(isset($this->sortArray)) {
            foreach ($this->sortArray as $sortBy) {
                $this->db->orderby($sortBy, "asc");
            }    
        }
        $query = $this->db->get('enquiries', $num, $offset);
        return $query;
    }
}

So I set the class variable $sortArray to the value past into the function. Now, next time I want to access the class variable $sortArray it should have the previously stored information in it, but it doesn't.
#4

[eluser]Michael Wales[/eluser]
Are you accessing it via:
Code:
$this->Listings->sortArray;

That should work, correct? I'm a bit week on OOP, surprisingly.
#5

[eluser]KJTED[/eluser]
I'd like to access it in another function of the same Model class, so that syntax probably won't work. I'm wondering if i need to store the information in a session variable rather than a class variable, but again I've had problems accessing my custom session variables.

Once more the CodeIgniter documentation is letting me down. It explains how to add custom session variables but doesn't actually tell you how to access them once you create them. It's starting to really get to me Sad
#6

[eluser]coolfactor[/eluser]
[quote author="KJTED" date="1189714778"]
So I set the class variable $sortArray to the value past into the function. Now, next time I want to access the class variable $sortArray it should have the previously stored information in it, but it doesn't.[/quote]

When is "next time" exactly?
#7

[eluser]coolfactor[/eluser]
--deleted-
#8

[eluser]Michael Wales[/eluser]
Quote:Once more the CodeIgniter documentation is letting me down. It explains how to add custom session variables but doesn’t actually tell you how to access them once you create them. It’s starting to really get to me Sad

Uhm... yes it does. The fourth header in the documentation reads "Retrieving Session Data".

Quote:Any piece of information from the session array is available using the following function:
Code:
$this->session->userdata('item');

Where item is the array index corresponding to the item you wish to fetch. For example, to fetch the session ID you will do this:
Code:
$session_id = $this->session->userdata('session_id');

Note: The function returns FALSE (boolean) if the item you are trying to access does not exist.
#9

[eluser]Michael Wales[/eluser]
Quote:I’d like to access it in another function of the same Model class, so that syntax probably won’t work. I’m wondering if i need to store the information in a session variable rather than a class variable, but again I’ve had problems accessing my custom session variables.

As long as you are calling your second function after the one that initialized the variable you should be able to access that variable.

Code:
class Foo {
  var $widget = '';

  function bar1() {
    $this->widget = 'blue';
  }

  function bar2() {
    return $this->widget;
  }

}
bar2 would return '' initially.
If you run bar1, then bar2, it would return 'blue'
#10

[eluser]KJTED[/eluser]
[quote author="walesmd" date="1189716612"]
Quote:Once more the CodeIgniter documentation is letting me down. It explains how to add custom session variables but doesn’t actually tell you how to access them once you create them. It’s starting to really get to me Sad

Uhm... yes it does. The fourth header in the documentation reads "Retrieving Session Data".

Quote:Any piece of information from the session array is available using the following function:
Code:
$this->session->userdata('item');

Where item is the array index corresponding to the item you wish to fetch. For example, to fetch the session ID you will do this:
Code:
$session_id = $this->session->userdata('session_id');

Note: The function returns FALSE (boolean) if the item you are trying to access does not exist.
[/quote]

No it doesn't. It shows you how to add an array of additional information to the session, but doesn't show you how to access elements of that array from the additional information. Or am I just reading it all wrong?

I'm thinking that because PHP is intantial then the next time I access a method of that class the class variables are reinitialised back to their default values, hence the inability to store information in them... session variables would seem to be the best alternative then.




Theme © iAndrew 2016 - Forum software by © MyBB