Welcome Guest, Not a member yet? Register   Sign In
Passing data between two functions in my controller class
#1

[eluser]See64[/eluser]
I'm a bit of newbie and I'm trying to use CI to get my PHP programming straight. Now I ran into this problem, that I sorta asked before on here: link to thread. (I got an answer to my original problem, not the passing-of-variables question).

The question simply is: how do I pass variables from one function to another? I want all my functions to return to the index() one, and I want to able to send a status message, something like this:

Code:
class Test extends Controller {

    function Test()
    {
        parent::Controller();
    }

    function index()
    {
        $message = 'I want other functions to replace this text';
        $data['message'] = $message;
           $this->load->view('testview', $data);
    }

    function edit()
    {
             $this->message = 'How to send this message to index()';
             $this->index();
    }
}

How do I accomplish this? Many thanks in advance.
#2

[eluser]deand[/eluser]
Hi See64,

Code:
class Test extends Controller {

    function Test()
    {
        parent::Controller();
    }

    function index()
    {
           $data['message'] = $this->message;
           $this->load->view('testview', $data);
    }

    function edit()
    {
             $this->message = 'How to send this message to index()';
             $this->index();
    }
}

Does this help?
#3

[eluser]deand[/eluser]
Actually, you will have a problem with index() so I think this will be better:

Code:
class Test extends Controller {

    function Test()
    {
        parent::Controller();
    }

    function index()
    {
           if (isset($this->message)) {
                $data['message'] = $this->message;
           } else {
                $data['message'] = 'I want other functions to replace this text';
           }
           $this->load->view('testview', $data);
    }

    function edit()
    {
             $this->message = 'How to send this message to index()';
             $this->index();
    }
}
#4

[eluser]See64[/eluser]
Hi deand, thanks for your answer. This works like a charm, and that's the way I thought it would work.

The reason it didn't work for me, was that I also did a redirect after the $this->index();. That way it called index() twice, whereby $this->message would be empty the second time.

Oh, how I hate monday mornings Wink
#5

[eluser]John_Betong[/eluser]
Hi See64,

Here is the method I would use:

Code:
<?php

class Test extends Controller {

  function Test() {
    parent::Controller();
  }

  function index($data=array() ) {
    
    $default_message  = "I want the <b>'test/edit'</b> function to replace this text";
    $data['message']  = isset($data['message']) ? $data['message'] : $default_message;
    echo $data['message']; die;
    
  }

  function edit($message='Default_message will be sent to test and test/index') {
    
    $data['message'] = $message ;
    $this->index($data);
    }
}
/*

  URL =====>    http://localhost/
  Output ==>    I want the 'test/edit' function to replace this text

  URL =====>    http://localhost/test
  Output ==>    I want the 'test/edit' function to replace this text
  
  URL =====>    http://localhost/test/edit
  Output ==>    Default_message will be sent to test and test/index

  URL =====>    http://localhost/test/edit/CodeIgniter is wonderful
  Output ==>    CodeIgniter is wonderful
  
*/  

?&gt;
&nbsp;
Cheers,
&nbsp;
John_Betong
&nbsp;
&nbsp;
#6

[eluser]Unknown[/eluser]
Hi There,

I have an issue with this - I am writing an API style interface where different external triggers post data to my system and cause different actions to happen. The way data is sent is set, so I can't modify - and varies, so I have to have different circumstances.

Basically, I have a bunch of functions like this:

Code:
$user_info = $this->profile->get_user_by_email($this->input->get('Email'));
  if (!$user_info)
  {
   $user_info = $this->profile->get_user_by_email($this->input->get('Contact0Email'));
  }
  $this->profile->status($user_info->id,0);

$this->profile->status is the only thing that is different, the get_user_by_email stuff is repeated in a ton of functions. I'd love it to look something like this

Code:
$this->get_user_info_function();
$this->profile->status...

Is that possible? Or, because I'm using GET, does it have to be in the same function that the data is posted to?




Theme © iAndrew 2016 - Forum software by © MyBB