开发者

CodeIgniter - Variable scope

I have a controller which I use for a login form. In the view, I have a {error} variable which I want to fill in by using the parser lib, when there is an error. I have a function index() in my controller, controlled by array $开发者_运维百科init which sets some base variables and the error message to '':

    function index()
    {
    $init = array(
                    'base_url'      =>      base_url(),
                    'title'         =>      'Login',
                    'error'         =>      ''
            );

    $this->parser->parse('include/header', $init);

    $this->parser->parse('login/index', $init);

    $this->parser->parse('include/footer', $init);
    }   

At the end of my login script, I have the following:

  if { // query successful }
  else 
        {
            $init['error'] = "fail";

            $this->parser->parse('login/index', $init);
        }

Now, of course this doesn't work. First of all, it only loads the index view, without header and footer, and it fails at setting the original $init['error'] to (in this case) "fail". I was trying to just call $this->index() with perhaps the array as argument, but I can't seem to figure out how I can pass a new $init['error'] which overrides the original one. Actually, while typing this, it seems to impossible to do what I want to do, as the original value will always override anything new.. since I declare it as nothing ('').

So, is there a way to get my error message in there, or not? And if so, how. If not, how would I go about getting my error message in the right spot? (my view: {error}. I've tried stuff with 'global' to bypass the variable scope but alas, this failed. Thanks a lot in advance.


$init musst be modified before generating your view.

To load your header and footer you can include the following command and the footer's equivalent into your view.

<?php $this->load->view('_header'); ?>

to display errors, you can as well use validation_errors()

if you are using the codeigniter form validation.

if you are using the datamapper orm for codeigniter you can write model validations, and if a query fails due to validation rule violation, you get a proper error message in the ->error property of your model.

Code for your model:

var $validation = array(
        'user_name' => array(
            'rules' => array('required', 'max_length' => 120),
            'label' => 'Name'
        )
    );


You might try this:

function index() {
    $init = array(
        'base_url' => base_url(), 
        'title' => 'Login',
        'error' => ''
    );
    $string = $this->parser->parse('include/header', $init, TRUE);
    $string .= $this->parser->parse('login/index', $init, TRUE);
    $string .= $this->parser->parse('include/footer', $init, TRUE);
    $this->parser->parse_string(string);
}

In parse()you can pass TRUE (boolean) to the third parameter, when you want data returned instead of being sent (immediately) to the output class. By the other hand, the method parse_string works exactly like `parse(), only accepts a string as the first parameter in place of a view file, thus it works in conjunction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜