Welcome Guest, Not a member yet? Register   Sign In
Data transfer between controller functions
#11

[eluser]TheFuzzy0ne[/eluser]
It's not to do with safety, but more to do with the MVC pattern. The whole point of MVC is that you keep your models and views separated. Smile
#12

[eluser]Mareshal[/eluser]
I tried again the following code
MAIN controller
Code:
function index(){      
        $this->data['query'] = "my query";
        $this->renderpage();
    }
    
    function subscribe(){
        $this->data['sub'] = "subscription done";
        $this->renderpage();
    }
    
    function renderpage(){
        $this->load->view('main/index', $this->data);
    }

VIEW index.php
Code:
<?php
echo $query;
echo $sub;
?>

I tried to call following URLs:
1st - http://localhost/testme -> returns "my query" and "Undefined variable: sub"
2nd - http://localhost/testme/index.php/main/subscribe -> "Undefined variable: query" and "subscription done"
3rd - http://localhost/testme/index.php/main/renderpage -> returns "my query" and "subscription done" ONLY WITH THIS CODE which is not correct in my opinion
Code:
function __construct() {
        parent::Controller();
        $this->data['query'] = "my query";
        $this->data['sub'] = "subscription done";
    }

    function renderpage(){
        $this->load->view('main/index', $this->data);
    }

Also I tried with or without $this in some cases, and no success.

How can I get 3rd result with 1st code ?
#13

[eluser]TheFuzzy0ne[/eluser]
Why have a separate method to render the page, when you can just do $this->load->view('main/index', $data)? If you absolutely must have that method, you should consider prefixing it with an underscore so it can't be access via the URI.
#14

[eluser]Mareshal[/eluser]
I tried n0xie's suggestion.

I only try to use the data from another function in same controller, and that was only a simple example.
#15

[eluser]garymardell[/eluser]
Your getting the undefined variables as with your example you set $this->data["sub"] in one controller function and the query in another. Now when you go to say localhost/main/subscribe it calls the subscribe function. This sets your data["sub"] but query is still empty. Now if you were to define in your contruct both of the variables to be inially set to "" or something then you wouldn't get the error of missing the other param.
#16

[eluser]Mareshal[/eluser]
yes, I know why I am getting undefined variable, but how to use function in function in same controller?

How to load a 2nd view in 1st view, while a controller is sending data to 2nd view? I don't know if you understood.

VIEW 1st.php
$this->load->view('2nd.php');

but in the same time, or previously a controller is sending data to 2nd view. I am trying to load the entire data in 1st view only. Like I would have a controller sending data to header, but header is loaded inside index.php.
#17

[eluser]TheFuzzy0ne[/eluser]
I feel that you need to have a look around a bit more for inspiration. Please see my [url="http://ellislab.com/forums/viewthread/107773/"]CodeIgniter Resources thread[/url]. It might help you get a clearer picture of how to do things.
#18

[eluser]Mareshal[/eluser]
thanks, but I've already clicked that link from your sig yesterday. I read most of the content form there, but now there are 2 cases, I don't see the solution to my problem.

Am I blind or am I blind? :|
#19

[eluser]garymardell[/eluser]
Okay, i don't think i quite understand what your trying to achieve. You trying to share the same data over two pages. As in you want the same data after redirecting the user? So you send the user to local/x and then have some data and then if they go to local/y the controller has access to the data from before.

Or is it you just want to call the other controller method from another controller method.
#20

[eluser]Mareshal[/eluser]
User is in /local . after subscribe user is sent to /local/subscribe/ . I want here same data like /local + some other data .

And also inside a controller I am trying to call a function from another(controller1/function1 calls controller1/function2), or call in controller1/function1 -> controller2/function2.




Theme © iAndrew 2016 - Forum software by © MyBB