Welcome Guest, Not a member yet? Register   Sign In
Session variable disappears after redirect
#1

[eluser]ramabodhi[/eluser]
Hello, I'm having a problem displaying results with my user search function.

I had everything working, and it was simple.. The search form on the main page links to search/basic

Here what I had BEFORE that worked PERFECTLY

Code:
basic()
{
   1. form validation
   2. run query
   3. set result set as session variable 'search_results'
   4. load search view
}

Heres how the search view works
Code:
<? $results = $this->session->userdata('search_results') ?>


Heres the SMALL changes I've made to the search controller, and now in my view i'm getting "invalid argument for foreach"

Code:
basic()
{
   1. form validation
   2. run query
   3. set result set as session variable 'search_results'
   4. redirect('search/results');
}
results
{
   $this->load->view('search/search_results');
}

I opted to do it this way for result sorting, because some of the variables that one can sort by are not in the database (so using order in my query is not possible), they are added to the array after the query before it is stored as a session variable.

Additionally When someone clicks the column header last login, to sort results by last login, i could not have it redirect to search/basic because there would be no form data,
which is why i made the search/results so that it can call a function to sort the array and then reload the view without having to go through the validation, etc...

So anyways, when i use the first example my session variable is fine... When i redirect the search function to results function the session variable becomes empty...

Any advice on how to fix this, or just another, better way of dealing with my search results, I would greatly appreciate it, thanks Smile

ps: i have tried using the 'refresh' option with redirect, alas no success there either
#2

[eluser]Dam1an[/eluser]
It could be that you're storing too much data in the session, there's a 4k limit, and its encrypted, so you have less space for session data then you think

That may be cauing it to reset, maybe someone can veryify if this is the case?
Alternativly, limit the query to 1 row, that should easily be within the limit, if that works, then my theory is probably corretc
#3

[eluser]ramabodhi[/eluser]
Yes you are right, I'm silly to have missed that, sorry I'm somewhat of a noob

so i'm thumbing through documentation, but whats the best way for me to store the search results in the search controller for access/modification from anywhere in my site?

[edit] I'm playing with $this->load->vars($array), but am not quite sure how to get it to work
#4

[eluser]Dam1an[/eluser]
If you want to carry on storing the data in the session, and I use that phrase loosely, you can store a session id in the session which is a key into a database, you can then store as much info as you need. I've never needed to do that myself, but I'm sure someone else could point you in the right direction
#5

[eluser]ramabodhi[/eluser]
that may be unneccessary, i was merely using sessions as a way of compensating for my lack of knowledge.

What I really need is a variable $search_results available anywhere in my search controller...

i declared global $search_results in my contructor, set it as the query results in the basic function, and they get recalled by the view in the results function...

my view is saying the variable doesn't exist, so i'm not identifying its scope properly or in the right place..

wheres the appropriate place/code for me to declare a variable so its global to a controller?
#6

[eluser]Dam1an[/eluser]
Look at the views page in the user guide
You need to pass an array of variable as the second argument in the load view command
#7

[eluser]ramabodhi[/eluser]
I've done that, the variable makes it to the view, but its not going between functions in my controller

Code:
class Search extends Controller
{
    function Search()
    {
        parent::Controller();
    global $search_results;
    }
    function Basic()
    {
        $search_results = $query_results;
    {
    function results()
    {
        $data['results'] = $search_results;
        $this->load->view('search/search_view', $data);
    }
}

so the variable, define in the controller, who's value is set in the basic() comes up as undefined in results()... so tells me i'm not defining it as a global variable in the proper place..
#8

[eluser]_www_[/eluser]
Hello ramabodhi,

that problem has nothing to do with codeigniter, its about php.

The "global" statement simply tells something like: In the global scope, there is a variable named "X" and i want access it. Thats quite different from how pretty much all other languages i know work with this concept.
A global variable is not accessable by default to everyone. You have to declare, that you want to access it in a function.

I guess, what you want to do is simply:

<code>
class Search extends Controller
{
var $searchresults;

function Search()
{
parent::Controller();
}
function Basic()
{
$this->search_results = $query_results;
}
function results()
{
$data['results'] = $this->search_results;
$this->load->view('search/search_view', $data);
}
}
</code>

Anyway, controller functions are normally called by HTTP Requests. No variables are remebered between 2 HTTP Requests. A HTTP-Request creates a new instance of the controller and calls the function specified by the URL. That's why your need somehow seems odd to me.

Hope i could be of help




Theme © iAndrew 2016 - Forum software by © MyBB