Welcome Guest, Not a member yet? Register   Sign In
flashdata not working but userdata works
#1

[eluser]spider pig[/eluser]
I have a searchable list with pagination. I have used it in CodeIgniter 1.6.3 without problems but in version 1.7 it doesn't work.

I use flashdata to store the search text between pages but it's currently is not accepting a value. It tried it using userdata and it works fine.

Anyone have an idea what I am doing wrong.

Thanks

Here's the controller:
Code:
function search_results() {
         $this->main->load_settings();
         $this->load->helper('form');
        
         $pageText = $this->main->header($this->pageID);
         if (!$pageText)
             return;
        $html->al_main_content .= $pageText;
         $categories = $this->product_support_class->product_category_array();

        $searchtext = $this->session->flashdata('searchtext');
        if (isset($_POST['searchtext'])) {
            $data->searchtext = $this->input->post('searchtext');
        } elseif (isset($searchtext)) {
            $data->searchtext = $this->session->flashdata('searchtext');
        } else {
            $data->searchtext = NULL;
        }
         if (isset($_POST['cat'])) {
             $data->cat = $this->input->post('cat');
         } else {
             $data->cat = $this->uri->segment(3, 0);
         }
         $data->cat_list = form_dropdown('cat', $categories, $data->cat, 'class="AdvancedCategory"');
         $html->al_main_content .= $this->load->view('product_support/search_form', $data, TRUE);
        $html->al_main_content .= $this->product_support_class->search_results($data->searchtext, $data->cat, base_url().'product_support/search_results/'.$data->cat);

         $this->load->view('content', $html);        
         $this->load->view('footer');
     }

Here's the model:
Code:
function search_results($searchtext, $cat, $path) {
        $this->load->library('pagination');
        $data->al_table_rows = NULL;
        $html = '<h5 style="margin: 10px 0 5px 0;">Search Results</h5>'.LF;
        if ($searchtext != NULL) {
            $this->session->set_flashdata('searchtext', $searchtext);
            $html .= $this->session->flashdata('searchtext');
        }
        $sql = "select supportID from ".$this->db->dbprefix('product_support')." where supportPublish='Y'";
        if ($cat > 0)
            $sql .= " and supportCategory like '%|".$this->db->escape_str($cat)."|%'";
        if ($searchtext != NULL)
            $sql .= " and (supportProductName like '%".$this->db->escape_str($searchtext)."%' or supportText like '%".$this->db->escape_str($searchtext)."%')";
        $query = $this->db->query($sql);

        $config['base_url'] = $path;
        $config['total_rows'] = $query->num_rows();
        $config['uri_segment'] = 4;
        $config['per_page'] = '20';
        $config['full_tag_open'] = '<div class="pagination">Page - ';
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span>';
        $config['cur_tag_close'] = '</span>';
        $this->pagination->initialize($config);

        $sql = "select * from ".$this->db->dbprefix('product_support')." where supportPublish='Y'";
        if ($cat > 0)
            $sql .= " and supportCategory like '%|".$this->db->escape_str($cat)."|%'";
        if ($searchtext != NULL)
            $sql .= " and (supportProductName like '%".$this->db->escape_str($searchtext)."%' or supportText like '%".$this->db->escape_str($searchtext)."%')";
        $sql .= " limit ".$this->uri->segment(4, 0).",20";
        $query = $this->db->query($sql);
        $html .= $this->pagination->create_links();
        if ($query->num_rows() > 0) {
            $counter = 0;
            foreach ($query->result() as $row) {
                $data->al_table_rows .= '<tr'.$this->product_support_class->row_colour($counter).'><td><h5><a >supportID.'/'.htmlentities($row->supportPath).'">'.htmlentities($row->supportProductName).'</a></h5></td><td valign="top" class="textright">12/03/2008</td></tr>'.LF;
                $counter++;
            }
        } else {
            $data->al_table_rows .= '<tr><td colspan="2"><div style="text-align: center; padding: 50px 0;"><h5>No product support documents match the search criteria.</h5></div></td></tr>'.LF;
        }
        $html .= $this->load->view('product_support/results_table', $data, TRUE);
        $html .= $this->pagination->create_links();
        return $html;
    }

Session settings:
Code:
$config['encryption_key'] = "password";

$config['sess_cookie_name']        = 'cisession';
$config['sess_expiration']        = 7200;
$config['sess_encrypt_cookie']    = TRUE;
$config['sess_use_database']    = FALSE;
$config['sess_table_name']        = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent']    = TRUE;
$config['sess_time_to_update']     = 300;
#2

[eluser]rt30000[/eluser]
flashdata will only work on the next page load from what I understand. Try redirecting back to that same page after setting it just to see if it then appears.
#3

[eluser]hugle[/eluser]
[quote author="rt30000" date="1227215155"]flashdata will only work on the next page load from what I understand. Try redirecting back to that same page after setting it just to see if it then appears.[/quote]


you are right.
I was playing some days ago with flashdata.
and yes you need to redirect to some other page to get that data.

Btw, maybe someone of you know/has a hack to override this? because imho it makes flashdata somehow useless ?

correct me if I am wrong
#4

[eluser]rt30000[/eluser]
You can also set a variable (ie $message) in your controller and then your view would display flashdata OR $message if either exist. That allows you to use either, and would solve your flashdata not loading until page reload issue.
#5

[eluser]basty_dread[/eluser]
not sure why it doesnt work on codeigniter 2.0, having problem also on flashdata..
the redirect is to same controller..

registration controller - > add_registration - > registration controller.
not sure what happen..
#6

[eluser]spider pig[/eluser]
I gave up on using flashdata and only use userdata. I have no problems with $this->session->userdata(). It works fine in CI 2.0.x
#7

[eluser]DrWaky[/eluser]
I just do the same, the $this->session->flashdata('item') doesn't work with flasdata in CI 2.0.x I have no problem with $this->session->userdata('item') ... but I wanted a volatile data...

I think that it is a BUG, cause at user guide you can see:

To read a flashdata variable:

Code:
$this->session->flashdata('item');

but it doesn't work.
#8

[eluser]WanWizard[/eluser]
No issues with flashdata here. If you create this test controller
Code:
class Flashdata extends MY_Controller {

    function __construct()
    {
        parent::__construct();

        $this->load->library('session');
    }

    function index()
    {
        if ($var = $this->session->flashdata('test'))
        {
            echo "Found data: $var";
        }
        else
        {
            $this->session->set_flashdata('test', 'flash data test');
            echo "Set data";
        }
    }
}
you'll see that it alternates between found and set, as designed.

You could have a flashdata issue if you use cookie based sessions (which you should not), use Internet Explorer, and redirects. Some IE versions ignore headers when they see a 'location' redirect in the header, which in case of session cookies means you lose that session update...
#9

[eluser]DrWaky[/eluser]
[quote author="WanWizard" date="1309317695"]No issues with flashdata here. If you create this test controller
Code:
class Flashdata extends MY_Controller {

    function __construct()
    {
        parent::__construct();

        $this->load->library('session');
    }

    function index()
    {
        if ($var = $this->session->flashdata('test'))
        {
            echo "Found data: $var";
        }
        else
        {
            $this->session->set_flashdata('test', 'flash data test');
            echo "Set data";
        }
    }
}
you'll see that it alternates between found and set, as designed.

You could have a flashdata issue if you use cookie based sessions (which you should not), use Internet Explorer, and redirects. Some IE versions ignore headers when they see a 'location' redirect in the header, which in case of session cookies means you lose that session update...[/quote]

But in this code the if
Code:
if ($var = $this->session->flashdata('test'))
works because it assign FALSE (an strange value of $this->session->flashdata('test')) to $var so, the calculated value to if condition is TRUE, so it work, cause the asignation in condition work fine

Try an echo of $var after de asignation please to see its content.
#10

[eluser]InsiteFX[/eluser]
If you find that you need to preserve a flashdata variable through an additional request, you can do so using the keep_flashdata() function.
Code:
$this->session->keep_flashdata('item');

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB