CodeIgniter Forums
How to call a method in the same class? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to call a method in the same class? (/showthread.php?tid=27638)

Pages: 1 2


How to call a method in the same class? - El Forum - 02-16-2010

[eluser]laytone[/eluser]
Is this what your wanting?

Code:
<?php

class Newsletter extends Controller {
    
    public $data_template;
      
    function Newsletter()
    {
        parent::Controller();
        
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('array');
        $this->load->helper('my_addjsfile_helper');
        $this->load->helper('my_addbodytag_helper');
        $this->load->model('Template_model');
    }
      
    function index()  
    {          
        $this->data_view['result2'] = null;  //this is used for what?
        
        $this->_setIndexTemplateData();

        $this->load->view ('common/template_view', $this->data_template); # Aqui leio o template comum em "common/template_view.php"        
    }    
      
    function _setIndexTemplateData(){
        $this->data_template['function_name'] = 'newsletter_index';
        $this->data_template['title'] = 'Newsletter';
        $this->data_template['resultTemp'] = $this->Template_model->getZonasComAnunciosActivos();
    }
    
    function subscricaopasso2()  
    {  
        $this->load->library('form_validation');
        $this->data_view['result2'] = null; //Again,  what is this doing?
        $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');

        if($this->form_validation->run() == FALSE)
        {
            //Form Didn't Validate,  relod the default data into $this->data_template and load the view...
            $this->_setIndexTemplateData();
            $this->load->view ('common/template_view', $this->data_template);
        }
        else
        {
            //Your form validated so do this
            $this->data_template['function_name'] = 'newsletter_subscricaopasso2';
            $this->data_template['title'] = 'Subscrição da Newsletter, Passo 2';
            $this->data_template['resultTemp'] = $this->Template_model->getZonasComAnunciosActivos();
                
           $this->load->view ('common/template_view', $this->data_template);
        }
    }

}

?>



How to call a method in the same class? - El Forum - 02-16-2010

[eluser]laytone[/eluser]
After the page goes to the user the data that was in $data_template is gone erased poof no longer exists.... It wont be there its gone.

1. Function index() is run
2. The page is sent to the user and everything no longer exists
3. The user submits the form to function2 and everything is brand new the variables from when you ran index() no longer exists.
4. Function 2 runs and the page is sent back to the user and no longer exists.
5. The user makes another request and everything is brand new again!

If you call function2 from function1 then they will both run.. but that wont do you any good.

[quote author="Sinclair" date="1266367859"]Hi,

I have done it, but is not working...

The data_template variable have the same name in Index() and in Subscricaopasso2(). How can I call it in the correct way?

Here is the code:

Code:
<?php

class Newsletter extends Controller {
    
    public $data_template;
      
    function Newsletter()
    {
        parent::Controller();
        
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('array');
        $this->load->helper('my_addjsfile_helper');
        $this->load->helper('my_addbodytag_helper');
        $this->load->model('Template_model');
    }
      
    function index()  
    {  
                
        
        #BEGIN PASSAGENS P O TEMPLATE PRINCIPAL
        # Aqui coloco as configuracoes estáticas da funcao
        $function_name = 'newsletter_index'; # [nomedocontroller_nomedafuncao]
        # Aqui coloco as configuracoes dinamicas da funcao
        $title = 'Newsletter'; # Aqui defino o título da página
        #Aqui passo as zonas para o template principal
        $resultTemp = $this->Template_model->getZonasComAnunciosActivos();
        #END PASSAGENS P O TEMPLATE PRINCIPAL
        
                
        
        # Neste bloco defino a passagem de variáveis para a view
        #$this->data_view['result'] = $this->Tag_model->getTodasAsTags();
        #$this->data_view['result2'] = $this->Tag_model->getTodasAsTagsCloud();
        $this->data_view['result2'] = null;
                
        
        # Neste bloco defino a passagem de variáveis para o template_view
        $this->data_template['function_name'] = $function_name;
        $this->data_template['title'] = $title;
        $this->data_template['resultTemp'] = $resultTemp;
        
        
        $this->load->view ('common/template_view', $this->data_template); # Aqui leio o template comum em "common/template_view.php"        
    }    
      
    
    function subscricaopasso2()  
    {  
        $this->load->library('form_validation');
        
        #BEGIN PASSAGENS P O TEMPLATE PRINCIPAL
        # Aqui coloco as configuracoes estáticas da funcao
        $function_name = 'newsletter_subscricaopasso2'; # [nomedocontroller_nomedafuncao]
        # Aqui coloco as configuracoes dinamicas da funcao
        $title = 'Subscrição da Newsletter, Passo 2'; # Aqui defino o título da página
        #Aqui passo as zonas para o template principal
        $resultTemp = $this->Template_model->getZonasComAnunciosActivos();
        #END PASSAGENS P O TEMPLATE PRINCIPAL
        
        
        
        
        # Neste bloco defino a passagem de variáveis para a view
        #$this->data_view['result'] = $this->Tag_model->getTodasAsTags();
        #$this->data_view['result2'] = $this->Tag_model->getTodasAsTagsCloud();
        $this->data_view['result2'] = null;
                
        
        
        # Neste bloco defino a passagem de variáveis para o template_view
        $this->data_template['function_name'] = $function_name;
        $this->data_template['title'] = $title;
        $this->data_template['resultTemp'] = $resultTemp;
        
        
        # Neste bloco faço as validações
        $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
        
        if($this->form_validation->run() == FALSE)
        {
            $this->load->view ('common/template_view', Newsletter::index()->data_template); # Aqui leio o template comum em "common/template_view.php"
            #$this->load->view ('common/template_view', $this->data_template); # Aqui leio o template comum em "common/template_view.php"
        }
        else
        {
            null;
        }        
                
    }
      
  
      
}

?>



Best Regards,[/quote]


How to call a method in the same class? - El Forum - 02-16-2010

[eluser]Sinclair[/eluser]
Hi, thanks for the reply.

What I'm trying to do is a Multi Page Form, and I'm searching for ways of doing it. I think this way is not gonna work...

There are examples of Multi Page Forms?



Best Regards,


How to call a method in the same class? - El Forum - 02-16-2010

[eluser]laytone[/eluser]
Here are some threads about this:

http://ellislab.com/forums/viewthread/106318/
http://ellislab.com/forums/viewthread/64258/


How to call a method in the same class? - El Forum - 02-16-2010

[eluser]Sinclair[/eluser]
Hi, thanks for the reply.

I have read the posts. One thing I can't see about is the validation in multi page forms. I submit the form to the same method and then redirect to the next method or there is another way of doing the validation?

Best Regards,


How to call a method in the same class? - El Forum - 02-16-2010

[eluser]Sinclair[/eluser]
Problem solved... what a newbie that I'am in OOP..

Code:
<?php

class Newsletter extends Controller {
    
    public $data_template;
      
    function Newsletter()
    {
        parent::Controller();
        
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('array');
        $this->load->helper('my_addjsfile_helper');
        $this->load->helper('my_addbodytag_helper');
        $this->load->model('Template_model');
    }
      
    function index()  
    {          
        
        #BEGIN PASSAGENS P O TEMPLATE PRINCIPAL
        # Aqui coloco as configuracoes estáticas da funcao
        $function_name = 'newsletter_index'; # [nomedocontroller_nomedafuncao]
        # Aqui coloco as configuracoes dinamicas da funcao
        $title = 'Newsletter'; # Aqui defino o título da página
        #Aqui passo as zonas para o template principal
        $resultTemp = $this->Template_model->getZonasComAnunciosActivos();
        #END PASSAGENS P O TEMPLATE PRINCIPAL
        
                
        
        # Neste bloco defino a passagem de variáveis para a view
        #$this->data_view['result'] = $this->Tag_model->getTodasAsTags();
        #$this->data_view['result2'] = $this->Tag_model->getTodasAsTagsCloud();
        $this->data_view['result2'] = null;
                
        
        # Neste bloco defino a passagem de variáveis para o template_view
        $this->data_template['function_name'] = $function_name;
        $this->data_template['title'] = $title;
        $this->data_template['resultTemp'] = $resultTemp;
        
        
        $this->load->view ('common/template_view', $this->data_template); # Aqui leio o template comum em "common/template_view.php"        
    }    
      
    
    function subscricaopasso2()  
    {  
        $this->load->library('form_validation');
        
        #BEGIN PASSAGENS P O TEMPLATE PRINCIPAL
        # Aqui coloco as configuracoes estáticas da funcao
        $function_name = 'newsletter_subscricaopasso2'; # [nomedocontroller_nomedafuncao]
        # Aqui coloco as configuracoes dinamicas da funcao
        $title = 'Subscrição da Newsletter, Passo 2'; # Aqui defino o título da página
        #Aqui passo as zonas para o template principal
        $resultTemp = $this->Template_model->getZonasComAnunciosActivos();
        #END PASSAGENS P O TEMPLATE PRINCIPAL
        
        
        # Neste bloco defino a passagem de variáveis para a view
        #$this->data_view['result'] = $this->Tag_model->getTodasAsTags();
        #$this->data_view['result2'] = $this->Tag_model->getTodasAsTagsCloud();
        $this->data_view['result2'] = null;
                
        
        
        # Neste bloco defino a passagem de variáveis para o template_view
        $this->data_template['function_name'] = $function_name;
        $this->data_template['title'] = $title;
        $this->data_template['resultTemp'] = $resultTemp;
        
        
                # Neste bloco faço as validações
        $this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email');
        
        if($this->form_validation->run() == FALSE)
        {
            //Se as validações não estiverem bem, fica na mesma página. Aqui defino as function name, title e resulttemp da index()
            #BEGIN PASSAGENS P O TEMPLATE PRINCIPAL
            # Aqui coloco as configuracoes estáticas da funcao
            $function_name = 'newsletter_index'; # [nomedocontroller_nomedafuncao]
            # Aqui coloco as configuracoes dinamicas da funcao
            $title = 'Newsletter'; # Aqui defino o título da página
            #Aqui passo as zonas para o template principal
            $resultTemp = $this->Template_model->getZonasComAnunciosActivos();
            #END PASSAGENS P O TEMPLATE PRINCIPAL
            
            # Neste bloco defino a passagem de variáveis para o template_view
            $this->data_template['function_name'] = $function_name;
            $this->data_template['title'] = $title;
            $this->data_template['resultTemp'] = $resultTemp;
            
            $this->load->view ('common/template_view', $this->data_template); # Aqui leio o template comum em "common/template_view.php"
        }
        else
        {
            //Se as validações estiverem correctas vai para a página seguinte
            $this->load->view ('common/template_view', $this->data_template); # Aqui leio o template comum em "common/template_view.php"
        }
        
        ##########$this->load->view ('common/template_view', $this->data_template); # Aqui leio o template comum em "common/template_view.php"
        
                
    }
      
  
      
}

?>

It is working now.

Best Regards,


How to call a method in the same class? - El Forum - 02-16-2010

[eluser]laytone[/eluser]
This is the same as what I post above (#10) which is accomplished with less code and no repetition.

I'm glad you figured it out! CodeIgniter is a blast!