Welcome Guest, Not a member yet? Register   Sign In
Flashdata and userdata in one function
#1

[eluser]CodeIgniterNoob[/eluser]
I have a function that needs to keep userdata and then echo a success statement on the next page using Flashdata. Its a page that edits posts, but I cant have them both because I get the "headers already sent" error. Can anyone help?

Code:
function edit($id=NULL)
    {
        if ($this->session->userdata('logged_in'))
        {
            echo 'Logged as: '.$this->session->userdata('username');
        }
        else
        {
            redirect('welcome/login','refresh');
        }
        
        if ($this->input->post('name'))
        {
            $this->MPages->updatePage();
            $this->session->set_flashdata('message','Page Updated!');
            redirect('admin/pages/index','refresh');
        }
#2

[eluser]beemr[/eluser]
Code:
function edit($id=NULL)
    {
        if ( ! $this->session->userdata('logged_in'))
        {
            redirect('welcome/login','refresh');
        }
        
        if ($this->input->post('name'))
        {
            $this->MPages->updatePage();
            $this->session->set_flashdata('message','Page Updated!');
            echo 'Logged as: '.$this->session->userdata('username');
            redirect('admin/pages/index','refresh');
        }
#3

[eluser]CodeIgniterNoob[/eluser]
The edit page needs to display the userdata, as well as the index page. But index page needs echo the flashdata passed from edit page. How should the index function handle it?
#4

[eluser]beemr[/eluser]
The way you have it structured, edit() will only ever redirect to other URLs and their respective controller/methods. You'll have to actually load a view from edit() to display anything from that URL. When redirected from edit(), index() will have access to the flashdata for its views.
#5

[eluser]CodeIgniterNoob[/eluser]
MAybe Im not presenting the question properly. All the functions have their views.

Index
Code:
function index()
    {
        $data['head_title'] = "Some title";
        $data['pages'] = $this->MPages->getAllPages();
        $data['cats'] = $this->MCats->getTopCategories();
        $this->template->load('mainTemplate', 'pages_home', $data);  
    }

Edit Page
Code:
function edit($id=NULL)
    {
        
        if ($this->input->post('name'))
        {
            $this->MPages->updatePage();
            $this->session->set_flashdata('message','Page Updated!');
            redirect('admin/pages/index','refresh');
        }    
        else
        {
            $id = $this->uri->segment(4);
            if ($id=="")
            {
                redirect('admin/pages/index','refresh');
            }
            else
            {
                $data['head_title'] = "Edit Page";
                $data['page'] = $this->MPages->getPage($id);
                $cats = $this->MCats->getCategoriesNav();
                $data['categories'] = $this->_flatten_cats($cats);
                $this->template->load('mainTemplate', 'pages_edit', $data);  
            }
        }
    }

And heres the parent
Code:
function __construct()
    {
        parent::Controller();
        $user_table = 'testusers';
        if ($this->session->userdata('logged_in'))
        {
            echo 'Logged as'.$this->session->userdata('username');
        }
        else
        {
            redirect('welcome/login','refresh');
        }
         }

What am I doing wrong?
#6

[eluser]beemr[/eluser]
Your "logged as" echo comes before any methods are called and therefore before any sessions can be set. Sessions need to be first in the return sequence.

Instead of affirming the "logged_in" session in your constructor, just test for its falsehood and redirect, then move your echo behind the session calls of each controller. Or better yet:
Code:
function __construct()
    {
        parent::Controller();
        $user_table = 'testusers';
        if ($this->session->userdata('logged_in'))
        {
            $data['logged_as'] = 'Logged as'.$this->session->userdata('username');
        }
        else
        {
            $data['logged_as'] = '';
            redirect('welcome/login','refresh');
        }
    }
and then make sure that you echo $logged_as in your view file.
#7

[eluser]CodeIgniterNoob[/eluser]
This worked perfectly. I actually came up with my own solution by just moving the echo statement into a view file where it belongs and all worked. But I adopted your idea and placed just the echo into the main template.

Thank you very much.
#8

[eluser]beemr[/eluser]
Welcome to CI Wink

Glad you found a way to make it work for you. CI's best hidden feature.




Theme © iAndrew 2016 - Forum software by © MyBB