• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sessions

#1
[eluser]southdevonwebdesign[/eluser]
I have a very confusing sessions problem with my application, I have an admin controller class which loads an orders model, in the order model it sets a variable called sortOrder which holds a SQL field to sort a database table. It also saves this to a session variable called sortOrder, which should be reloaded in the class constructor, however it flat refuses to take a variable as a value, it is however perfectly happy to take text. Am I missing something completely obvious with this?

Code

SortOrder function - saves the current order (doesn't save the session vars)

Code:
function sortOrderResults(){

        if($this->uri->segment(4)!=""){

            switch($this->uri->segment(4)){
                
                case "orderRef":
                    $sort = 'a.orderRef';
                    break;
                case "customerName":
                    $sort = 'b.firstName,b.lastName';
                    break;
                case "dateCreated":
                    $sort = 'a.dateCreated';
                    break;
                case "from_date":
                    $sort = 'a.from_date';
                    break;
                case "to_date":
                    $sort = 'a.to_date';
                    break;
                case "days":
                    $sort = 'a.days';
                    break;
                case "valet":
                    $sort = 'a.valet';
                    break;
                case "verified":
                    $sort = 'a.verified';
                    break;
                case "status":
                    $sort = 'a.status';
                    break;
                case "totalAmount":
                    $sort = 'a.totalAmount';
                    break;              
            }    
            
            if($this->sortResults==$sort){
                if($this->orderResults == 'DESC'){
                    $this->orderResults = 'ASC';
                }
                else{
                    $this->orderResults = 'DESC';
                }
            }
            $this->sortResults = $sort;
        }      

        $this->session->set_userdata('sortResults',$this->sortResults);
        $this->session->set_userdata('orderResults',$this->orderResults);
    }


Class constructor - should read the session variable back in

Code:
var $sortResults = 'a.from_date';
    var $orderResults = 'DESC';    
    
    function Model_order()
    {
        parent::Model();    
        if($this->session->userdata('sortResults'))
            $this->sortResults = $this->session->userdata('sortResults');
        if($this->session->userdata('orderResults'))
            $this->orderResults = $this->session->userdata('orderResults');
    }


This code works

Code:
function sortOrderResults(){

        if($this->uri->segment(4)!=""){

            switch($this->uri->segment(4)){
                
                case "orderRef":
                    $sort = 'a.`orderRef`';
                    $this->session->set_userdata('sortResults','`a.orderRef`');
                    break;
                case "customerName":
                    $sort = 'b.`firstName`,b.`lastName`';
                    $this->session->set_userdata('sortResults','b.`firstName`,b.`lastName`');
                    break;
                case "dateCreated":
                    $sort = 'a.`dateCreated`';
                    $this->session->set_userdata('sortResults','a.`dateCreated`');
                    break;
                case "from_date":
                    $sort = 'a.`from_date`';
                    $this->session->set_userdata('sortResults','a.`from_date`');
                    break;
                case "to_date":
                    $sort = 'a.`to_date`';
                    $this->session->set_userdata('sortResults','a.`to_date`');
                    break;
                case "days":
                    $sort = 'a.`days`';
                    $this->session->set_userdata('sortResults','a.`days`');
                    break;
                case "valet":
                    $sort = 'a.`valet`';
                    $this->session->set_userdata('sortResults','a.`valet`');
                    break;
                case "verified":
                    $sort = 'a.`verified`';
                    $this->session->set_userdata('sortResults','a.`verified`');
                    break;
                case "status":
                    $sort = 'a.`status`';
                    $this->session->set_userdata('sortResults','a.`status`');
                    break;
                case "totalAmount":
                    $sort = 'a.`totalAmount`';
                    $this->session->set_userdata('sortResults','a.`totalAmount`');
                    break;            
            }    
            
            if($this->sortResults==$sort){
                if($this->orderResults == 'DESC'){
                    $this->orderResults = 'ASC';
                    $this->session->set_userdata('orderResults','ASC');
                }
                else{
                    $this->orderResults = 'DESC';
                    $this->session->set_userdata('orderResults','DESC');
                }
            }
            $this->sortResults = $sort;
        }      

        
      
    }

#2
[eluser]skunkbad[/eluser]
So if you debug and test for the value of $this->sortResults and $this->orderResults right before they are to be set, what do you see?

#3
[eluser]southdevonwebdesign[/eluser]
I see the expected values for the chosen sort order. It just never actually puts them in the session, I have tried both autoload and loading the session library in the class constructor.

My original version had a generic write all class properties to the session and then reload them when re-initialising, but this just gave me even more headaches, so I reverted to setting the session where and when was needed to try and trap the problem.

#4
[eluser]skunkbad[/eluser]
I had a case where a CI session value wasn't being set because of a 404 error coming from a resource that didn't exist. Check your server logs and make sure your script isn't trying to call any photos, css files, javascript, etc. that don't exist. I know it sounds weird, but it happened to me.

#5
[eluser]southdevonwebdesign[/eluser]
oddly enough the 404 error messages fixed the problem, seems I had a pre-loader script fetching images (or trying to) from the wrong folder, weirdly this broke the sessions in firefox, but not it seems in internet explorer, go figure.

Hopefully this is the last time I will tear my hair out over disappearing sessions, thanks for the help.

#6
[eluser]skunkbad[/eluser]
[quote author="southdevonwebdesign" date="1272511368"]oddly enough the 404 error messages fixed the problem, seems I had a pre-loader script fetching images (or trying to) from the wrong folder, weirdly this broke the sessions in firefox, but not it seems in internet explorer, go figure.

Hopefully this is the last time I will tear my hair out over disappearing sessions, thanks for the help.[/quote]

It would be nice if there was a hint of this in the docs, since if it has happened to you and me, I'm sure it has happened to hundreds of other people.

#7
[eluser]southdevonwebdesign[/eluser]
To be honest I was all set to write my own session handler which would store data in a database table, I am sure alot of people would get so annoyed by it they would give up.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.