CodeIgniter Forums

Full Version: Problems retreiving session data on View page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've been having some really crazy problems with my session userdata. It seems like it just stops showing up in my views page after making some edits, and I cannot figure out why. I am using notepadd++ to do my editing of the php files, so I just "undo" the edits, but then it still will not work. It makes no sense. I'm a novice so I will do my best to explain.

Website visitor fills out a form. The form is validated with the following function in the controller:

PHP Code:
function _mainsearchvalidate($searchtype)
 {
 
$this->form_validation->set_rules('fname''First Name''required|trim|xss_clean');
 
$this->form_validation->set_rules('lname''Last Name''required|trim|xss_clean');
 
$this->form_validation->set_rules('age''Age''trim|xss_clean');
 
$this->form_validation->set_rules('city''City''');
 
$this->form_validation->set_rules('state''State''required'); 
 
 
 if(
$this->form_validation->run() == FALSE){
 
 
$data['search_type'] = $searchtype;
 
$this->themes->view('searches/home'$data);
 
 } else {
 
 
$data['search_type'] = $searchtype;
 
 
 
$tempfname $this->input->post('fname');
 
$templname $this->input->post('lname');
 
$currentstate $this->input->post('state');

 
 
$originalage $this->input->post('age');
 
 
 
 if (!empty(
$originalage)){
 
 
$numberofrows rand(47); 
 
 } else { 
 
 
$numberofrows rand(925);
 } 
 
 
 
 
 if (!empty(
$originalage) && ($originalage 18)) {
 
$originalage rand(1821);
 }
 elseif (!empty(
$originalage) && ($originalage 102)) {
 
 
$originalage rand(81101);
 }
 
 elseif (empty(
$originalage)) {
 
 
$mainpersonage rand(18,78);
 
 }
 
 
 if (empty(
$mainpersonage)) {
 
 
$mainpersonage $originalage;
 }
 
 
 
 
$resultsarray[0] = $numberofrows;
 
$resultsarray[1] = $this->input->post('fname');
 
$resultsarray[2] = $this->input->post('lname');
 
$resultsarray[3] = $mainpersonage;
 
$resultsarray[4] = $this->input->post('state');
 
$resultsarray[5] = $this->_statephone($this->input->post('state'));
 
 
$resultsarray[6] = $this->input->post('fname');
 
$resultsarray[7] = $this->input->post('lname');
 
$resultsarray[8] = $this->_agegenerator($originalage); 
 
$resultsarray[9] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[10] = $this->_createnames($tempfname$templname);
 
$resultsarray[11] = $this->_agegenerator($originalage); 
 
$resultsarray[12] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[13] = $this->_createnames($tempfname$templname);
 
$resultsarray[14] = $this->_agegenerator($originalage); 
 
$resultsarray[15] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[16] = $this->_createnames($tempfname$templname);
 
$resultsarray[17] = $this->_agegenerator($originalage); 
 
$resultsarray[18] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[19] = $this->_createnames($tempfname$templname);
 
$resultsarray[20] = $this->_agegenerator($originalage); 
 
$resultsarray[21] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[22] = $this->_createnames($tempfname$templname);
 
$resultsarray[23] = $this->_agegenerator($originalage); 
 
$resultsarray[24] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[25] = $this->_createnames($tempfname$templname);
 
$resultsarray[26] = $this->_agegenerator($originalage); 
 
$resultsarray[27] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[28] = $this->_createnames($tempfname$templname);
 
$resultsarray[29] = $this->_agegenerator($originalage); 
 
$resultsarray[30] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[31] = $this->_createnames($tempfname$templname);
 
$resultsarray[32] = $this->_agegenerator($originalage); 
 
$resultsarray[33] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[34] = $this->_createnames($tempfname$templname);
 
$resultsarray[35] = $this->_agegenerator($originalage); 
 
$resultsarray[36] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[37] = $this->_createnames($tempfname$templname);
 
$resultsarray[38] = $this->_agegenerator($originalage); 
 
$resultsarray[39] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[40] = $this->_createnames($tempfname$templname);
 
$resultsarray[41] = $this->_agegenerator($originalage); 
 
$resultsarray[42] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[43] = $this->_createnames($tempfname$templname);
 
$resultsarray[44] = $this->_agegenerator($originalage); 
 
$resultsarray[45] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[46] = $this->_createnames($tempfname$templname);
 
$resultsarray[47] = $this->_agegenerator($originalage); 
 
$resultsarray[48] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[49] = $this->_createnames($tempfname$templname);
 
$resultsarray[50] = $this->_agegenerator($originalage); 
 
$resultsarray[51] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[52] = $this->_createnames($tempfname$templname);
 
$resultsarray[53] = $this->_agegenerator($originalage); 
 
$resultsarray[54] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[55] = $this->_createnames($tempfname$templname);
 
$resultsarray[56] = $this->_agegenerator($originalage); 
 
$resultsarray[57] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[58] = $this->_createnames($tempfname$templname);
 
$resultsarray[59] = $this->_agegenerator($originalage); 
 
$resultsarray[60] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[61] = $this->_createnames($tempfname$templname);
 
$resultsarray[62] = $this->_agegenerator($originalage); 
 
$resultsarray[63] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[64] = $this->_createnames($tempfname$templname);
 
$resultsarray[65] = $this->_agegenerator($originalage); 
 
$resultsarray[66] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[67] = $this->_createnames($tempfname$templname);
 
$resultsarray[68] = $this->_agegenerator($originalage); 
 
$resultsarray[69] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[70] = $this->_createnames($tempfname$templname);
 
$resultsarray[71] = $this->_agegenerator($originalage); 
 
$resultsarray[72] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[73] = $this->_createnames($tempfname$templname);
 
$resultsarray[74] = $this->_agegenerator($originalage); 
 
$resultsarray[75] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[76] = $this->_createnames($tempfname$templname);
 
$resultsarray[77] = $this->_agegenerator($originalage); 
 
$resultsarray[78] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[79] = $this->_createnames($tempfname$templname);
 
$resultsarray[80] = $this->_agegenerator($originalage); 
 
$resultsarray[81] = $this->_statephone($this->input->post('state')); 
 
 
$resultsarray[82] = $this->recordschecklistgen();
 
$resultsarray[83] = $this->recordschecklistgen(); 
 
$resultsarray[84] = $this->recordschecklistgen(); 
 
$resultsarray[85] = $this->recordschecklistgen();
 
$resultsarray[86] = $this->recordschecklistgen();
 
 
$resultsarray[87] = $this->recordschecklistgen(); 
 
$resultsarray[88] = $this->recordschecklistgen();
 
$resultsarray[89] = $this->recordschecklistgen(); 
 
$resultsarray[90] = $this->recordschecklistgen();
 
$resultsarray[91] = $this->recordschecklistgen();
 
 
$resultsarray[92] = $this->recordschecklistgen();
 
$resultsarray[93] = $this->recordschecklistgen(); 
 
$resultsarray[94] = $this->recordschecklistgen(); 
 
$resultsarray[95] = $this->recordschecklistgen();
 
$resultsarray[96] = $this->recordschecklistgen();
 
 
$resultsarray[97] = $this->recordschecklistgen(); 
 
$resultsarray[98] = $this->recordschecklistgen();
 
$resultsarray[99] = $this->recordschecklistgen(); 
 
$resultsarray[100] = $this->recordschecklistgen();
 
$resultsarray[101] = $this->recordschecklistgen(); 
 
 
 
$this->session->set_flashdata('type'$searchtype);
 
                       $this->session->set_userdata($resultsarray);
 
           
 
 redirect
('/search/results''refresh');
 
           
        
}
 
       
    


In my views page, I am retrieving the userdata with this:

PHP Code:
<?= ucwords($this->session->userdata[1]); ?>

This WAS working until I made some edits. I undid the edits and it's still not working. My views page is giving me this error for each reference to the userdata:

A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 1
Filename: searches/completed.php
Line Number: 11

I have been having some problems with this over the weekend while working on this page but I have always been able to "fix" it. Usually undoing edits will fix the problem, but one time when this happened undoing the edits didn't work. I fixed it by just switching these two lines of code around from the controller (near the bottom of what I posted for the controller's validate function code):

PHP Code:
$this->session->set_flashdata('type'$searchtype);
$this->session->set_userdata($resultsarray); 

That doesn't make sense to me that switching those two lines of code suddenly fixed the undefined offset error, but it did. Naturally, that's the first thing I tried when this happened this time, but that did not work for me. I have run the code form the controller and the view page through a php syntax error error checker and both pages were clean. So I am out of ideas.

Also I would like to add some information regarding the setup of the views page. Right now I am testing this new functionality I am designing, which is based off existing functionality on the site, just another branch of it with more features, by using an if statement. Basically,

PHP Code:
<?php if($search_type == 'thesearchtype'): ?>

new functionailty here

PHP Code:
<?php else: ?>

old functionality here

PHP Code:
<?php endif; ?>


I am doing this so I can test what I am designing while the visitors of the site can still use the old functionality.

Anytime the old functionality code is being used, I can refresh the search results page (the view page) and all of the information generated for the table stays the same. This is what I want. When search results are returned, those results should display any time a user refreshes the page. However, this does not happen with my new functionality. Instead, the search results on the views page return the results from the PREVIOUS search. Then when I refresh the page, the results from the intended search are displayed. So whenever the view page is displayed showing my search results from the new functionality, I must refresh the page again to display the actual search results. That is, if that damn undefined index error isn't getting in the way. Assuming I can get rid of the undefined index error and get my data to display on the views page, I will also need to address this problem where I need to refresh the page to see the actual results. I am not sure if there is a relation between these problems so I figured I would mention this.
You aren't using sessions properly when retrieving session data.
Code:
<?= ucwords($this->session->userdata[1]); ?>

Should be something like
Code:
<?= ucwords($this->session->userdata('field_you_stored_the_variable_as_in_session'); ?>

Like
Code:
$this->session->userdata('username');

If you don't use it as the userguide states, you will have problems (even if it "appears" to work in some cases)
(01-26-2015, 02:20 PM)CroNiX Wrote: [ -> ]You aren't using sessions properly when retrieving session data.

Code:
<?= ucwords($this->session->userdata[1]); ?>

Should be something like

Code:
<?= ucwords($this->session->userdata('field_you_stored_the_variable_as_in_session'); ?>

Like

Code:
$this->session->userdata('username');

If you don't use it as the userguide states, you will have problems (even if it "appears" to work in some cases)

Thanks but I don't want to store the data in an associative array, needs to be in a numeric array so that I can loop through the elements of the array using an index so I can put them into a table. Are you saying it's not possible to use a numeric array with session data?

The data from the old functionality was stored in an associate array, before I changed it to a numeric array so I could use the index variable with it for looping. But, I was having the same problems with the page needing to be refreshed before moving the data into a numeric array, and the data was all displaying fine for several hours after changing it to a numeric array. I see the docs only mentioning using an associative array, but are you sure it cannot be done with a numeric one?
I changed all of the array data back to an associative array in the controller and updated my code in the view to call the data in the associative array. It still does not work.

The code below from my view, calling the session userdata array element and putting it in a specific table cell, is exactly like how it is set up with the original functionality:

PHP Code:
<?= $this->session->userdata['1']; ?>

When I do the above now, even though it worked fine like this before, it just gives me the undefined index error.

The docs say to do this:

PHP Code:
<?= $this->session->userdata('1'); ?>

Doing this, the undefined error is gone for those table cells but no data is showing up. But even if I do this, I will have no way to loop through my session userdata array. I'm sire there is a way but I have no idea how to do it without an index. I need to put information into the array in a certain order so that it is displayed in the table in the same order. The number of rows is dynamic and I want different results in the table for each search, so each array element must be generated from a separate call to it's function.

Man this is so messed up. It WAS working both as an associative and as a numeric array. I make VERY small changes and check to see if the results are as expected. I just don't understand how it can not be working when I undid all of the edits made after the last known working configuration. Ah this is so frustrating!
Still don't have this resolved. I have spent about 10 hours now on this problem alone. From what I can tell, the problem is more with the session userdata or flashdata then the code itself. The code DOES work using both an associative and numeric array.

Sometimes I make edits and the changes are reflected on the next search (well, I have to refresh the page to see clear the results from the previous search and show the results from THIS search), and sometimes the edits never show up at all. Some times I have to clear all browsing data and then the results will show up again. Sometimes clearing the browser data doesn't work. And sometimes, clearing the browser data seems to cause problems, where the results were displaying fine before and after clearing browser data they are not. I have tried firefox and chrome, along with android phone. I really have no clue what is going on here.

I think if I could figure out the problem with the results not showing up until the page is refreshed, then everything else will work. But this problem needs to be addressed before I can really know how everything else is functioning.


Again, here is the code from the controller that is saving data to userdata and flashdata:

PHP Code:
$this->session->set_flashdata('type'$searchtype);
$this->session->set_userdata($resultsarray);

redirect('/search/results''refresh'); 

And here's the code from the results function in the controller:

PHP Code:
$data['search_type'] = $this->session->flashdata('type');
$this->themes->view('searches/completed'$data); 


So, how can I make it so that whenever a search is made, the results show up on the results page WITHOUT needing to refresh the page, and also so that if the user does refresh the results page, the same results are displayed and not fresh results? Is there a way to control what happens when a page is refreshed regarding flash/userdata?
Code:
$some_array = array(
  'first',
  'second'
);

//store the array as 'some_array' in session flashdata
$this->session->set_flashdata('some_array', $some_array);

another page:
Code:
//retrieve the array by it's key
$retrieved = $this->session->flashdata('some_array');

//now it's your regular numerically indexed array
echo $retrieved[1]; //second
Thanks for the help. I finally got it working, not exactly like you had it though. For some reason I was never able to load my associative array from the controller into a numeric array in the views page. However, once I added the array data into flashdata (i have the array set to both userdata and flashdata) I was able to call specific elements within my views page like this:

PHP Code:
<td class="txtcenter"><?= $this->session->userdata['8']; ?></td> 

And in the for loop for the dynamically generated rows, I just referenced them as if they where numeric (using index) and it worked. So basically I was able to pull data from the array using both ['1'] for a specific element and [$j] for an indexed element. I don't know how it works but it does.

Does echo not work when using it withing html tags? I keep reading about using echo but it never works within div tags, where I need to put the array elements.

Everything is working great except for refreshing the page. I want to make it so that whatever results were generated during the search are the exact same results displayed when the page is refreshed. Right now it just generates new results upon page refresh. How can I accomplish this?
By default, flashdata only lasts for a single request unless you use the session::keep_flashdata() method as described in the user guide, but that says it only lasts for "an additional request". Not sure why you can't use regular sessions for this like my last post. You just store a numeric array (like you want) within an associative array like the session class documentation describes.