Welcome Guest, Not a member yet? Register   Sign In
Variable in all functions in Controller
#1

[eluser]Jareish[/eluser]
Hi,

I have a page in my CMS which consists of different parts. For instance you can add a category in 1 sidebar, and edit settings in another.

Adding a category goes through a different function then saving settings. And thus generates other error feedback.

My controller looks something like this

Code:
<?php
class Projects extends Controller {
    function __construct()
    {
        parent::Controller();        
        $this->load->model('projects_model');
    }
    
    function index()
    {        
        redirect('site/members_area');        
    }
    function editProject(){
         $data['title'] = 'title';
         $data['projects'] = this->projects_model->getProject();
        
         $data['main_content'] = 'projects/editProject';
         $this->load->view('includes/template', $data);
    }
    function addCategory()
    {
        //add to db
        if(this->projects_model->addCategory())
        {
           //success
           $data['message'] = "succesfully added"
        }
        else {
            //failed
            $data['message'] = "failed"
        }
        
        //ok this is the part where I want $data['message'] in editProject and load the editProject view.
    }
}
?>

when you click on a project on the members page, it loads the editProject function, loading lots of stuff from the database, everything stored in $data.

What I want to do is, when I add a category, to send the $data['message'] to the editProject function because I don't want to load all the same stuff in the addCategory fucntion. I don't want to do this with a function parameter, because the editProject function has to work without it. So the best way would be (I think) is to set a variable in the controller, then use a redirect in the addCategory function.

the editProject() would read that variable from the controller (or leave it empty if nothing is in it). and send it to the corresponding view.

I've seen alot of tutorials, but from what I've seen, they all do a redirect to a different view on success/fail. But I want it to be the same view, with a reserverd Div for errors.

I've read stuff on the forum about adding a global $message in the constructor, and then again in every function you use
Code:
global $message;
$data['message'] = $message;

I then got something like this:
Code:
<?php
class Projects extends Controller {
    function __construct()
    {
        parent::Controller();        
        $this->load->model('projects_model');
        
        global $message;
    }
    
    function index()
    {        
        redirect('site/members_area');        
    }
    function editProject(){
        global $message;
        $data['message'] = $message;
         $data['title'] = 'title';
         $data['projects'] = this->projects_model->getProject();
        
         $data['main_content'] = 'projects/editProject';
         $this->load->view('includes/template', $data);
    }
    function addCategory()
    {
        //add to db
        global $message;
        if(this->projects_model->addCategory())
        {
           //success
           $message = "succesfully added"
        }
        else {
            //failed
            $message = "succesfully added"
        }
        
        //ok this is the part where I want $data['message'] in editProject
        redirect(.... etc)
    }
}
?>

But I still get and error (not defined). Any ideas what Im doing wrong?
#2

[eluser]Deveron[/eluser]
Hi Jareish,

here is a solution but it doesn't work with redirect.

Controller:
Code:
<?php
class Projects extends Controller {

    protected $message; // simple definition

    function __construct()
    {
        parent::Controller();        
        $this->load->model('projects_model');
      
    }
    
    function index()
    {        
        redirect('site/members_area');        
    }
    function editProject(){
        
        $data['message'] = $this->message; // Get the value of the message this way
        
        $data['title'] = 'title';
        $data['projects'] = this->projects_model->getProject();
        
        $data['main_content'] = 'projects/editProject';
        $this->load->view('includes/template', $data);
    }
    function addCategory()
    {
        //add to db
        global $message;
        if(this->projects_model->addCategory())
        {
           //success
           $this->message = "succesfully added"; // Set value for the message
        }
        else {
            //failed
            $this->message = "ops, we have a problem"; // Set value for the message
        }
        $this->editProject(); // Function Call to load the other stuff
        
    }
}
?>

In this case the URL will not change. It will still show addCategory.

Cheers
#3

[eluser]Jareish[/eluser]
Hi,

thanks mate, this seems solid Smile I tried to declare a variable using global $message in the class, but got errors. I shall read more about PHP visibility Smile You just made my code life easier and my code smaller. Now I can have 1 function to load a view, and other functions that alter that view without touching the core variables Big Grin

php scoping is one of the biggest issues I had to deal with (I find it alot easier in AS3 for some reason).

Cheers




Theme © iAndrew 2016 - Forum software by © MyBB