Part of the EllisLab Network
   
 
Passing data between two functions in my controller class
Posted: 13 August 2007 03:22 AM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  21
Joined  07-19-2007

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:

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.

Profile
 
 
Posted: 13 August 2007 04:57 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  11
Joined  08-10-2007

Hi See64,

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?

Profile
 
 
Posted: 13 August 2007 05:00 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  11
Joined  08-10-2007

Actually, you will have a problem with index() so I think this will be better:

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();
    
}
Profile
 
 
Posted: 13 August 2007 05:28 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Avatar
Total Posts:  21
Joined  07-19-2007

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

Profile
 
 
Posted: 14 August 2007 10:25 AM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  749
Joined  12-26-2006

Hi See64,

Here is the method I would use:

<?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
  
*/  

?> 

 
Cheers,
 
John_Betong
 
 

 Signature 

Joke of the day - Bulletin Board Ideas     (ongoing development site)

My Hippy Trail    Source code   

Latest Project

Profile
 
 
   
 
 
‹‹ Godaddy problems      Using the date_helper ››