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

[eluser]Mareshal[/eluser]
Code:
function index(){
        
        $data['query'] = $this->programs->listing();//programs is a model previously loaded
        
        $data['newsletterCount'] = $this->newsletter->count();//newsletter is a model previously loaded
        
        $this->load->view('main/index', $data);
        
    }
    
    function subscribe(){
        
        $subs = $_POST;
            
        $email = $subs['email'];
            
        $this->newsletter->add($email);  
            
        $subs['done'] = "done";
        
        $this->load->view('main/index', $subs);
            
        //redirect(base_url());
            
        }
    }

How can I make the function subscribe() to load the same view as index() but maintain the $data array from index() too ?
#2

[eluser]jedd[/eluser]
I'm not sure that's the answer.

You may find it easier to just generate the common bits of your $data array in your constructor, so that they'll be visible from each of your methods (index(), subscribe(), etc). So to spell it out, in this instance, you'd shift the first two lines of your index() out to the constructor, making your index(0) one line.
#3

[eluser]Mareshal[/eluser]
I've fixed it a bit, but the problem is now with css and javascript. They don't work without absolute path to object(http:// ... )
#4

[eluser]TheFuzzy0ne[/eluser]
You could always use flashdata.
#5

[eluser]Mareshal[/eluser]
flashdata(CI) == SESSIONS(PHP) right?

I have a functional version with cookies(but some clients maybe won't accept cookies).

1. Cookie is set inside this: $this->newsletter->add($email);

2. redirect(base_url());

3.
Code:
if(get_cookie('subscriber', TRUE)) {
            
            echo "Thank you. You have been subscribed to our newsletter";
            
            delete_cookie('subscriber');
            
            }

This is the working version, but I tought there is another to do this.

but as far as I know sessions are safer than cookies. anyway, I don't use cookie to keep database details.


EDIT: I found another way, but I don't know how safe is this. I am working with controller only to load models. Then from models I load the view, because is easier to work with functions inside models, than controllers. Is there necessary to load a view ONLY from controllers? Is it safer than loading from models?
#6

[eluser]TheFuzzy0ne[/eluser]
Using the CodeIgniter session library is probably much safer, especially if you use the database, since everything is still kept on the server side, and anything sent to the user can be encrypted.
#7

[eluser]Mareshal[/eluser]
Can you please read what I've edited in previous post? I think we both posted at same time Big Grin
#8

[eluser]n0xie[/eluser]
Code:
function index(){
        
    $this->data['query'] = $this->programs->listing();
    $this->data['newsletterCount'] = $this->newsletter->count();
    $this->renderpage();
}      

    
    function subscribe(){
        
        //you should sanitize here!!!!
        //let's use filter for now
        
        if (filter_var($this->input->post('email'), FILTER_VALIDATE_EMAIL))
        {            
          $this->newsletter->add($this->input->post('email'));  
          $this->data['subscription'] = TRUE;
        }
        else
        {
          $this->data['subscription'] = FALSE;
        }
      $this->renderpage();
    }

    function renderpage()
    {
      // you can add some generic info like keywords/meta/title here
      $this->load->view('main/index', $this->data);
    }

Code:
//view
if (isset($subscription) && $subscription === TRUE)
{
  echo 'thanks';
} else {
  // render subscription form. You could add another check for
  //$subscription === FALSE to tell an user his email is not valid
}
#9

[eluser]Mareshal[/eluser]
thanks, works Big Grin

But : Is there necessary to load a view ONLY from controllers? Is it safer than loading from models?
#10

[eluser]n0xie[/eluser]
Loading views from models is considered bad practice in Code Igniter.




Theme © iAndrew 2016 - Forum software by © MyBB