Welcome Guest, Not a member yet? Register   Sign In
What is the best way to echo out error messages from the Controller to the View
#1

[eluser]Namsu[/eluser]
I have a login script which authenticates credentials against the DB. I am quite new CI but not to PHP. I want to output my code in the view file above the form. I have found that I can use a class variable and call it using $this->varname or use flashdata through sessions. In your experience, what advice can you give for someone like me who has data passing through from the controller to the view with the least code possible. Bearing in mind I have tried the guide method:

-------------------------------------
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);

$this->load->view('blogview', $data);

-------------------------------------

Problem with the load->view code is that in the view I cannot define a variable in the conditional if it hasn't been established, as I am checking to see if it is == 1. My $data array is only being initialized and is only being called on an else statement in the controller. For example:

1)User submits forms (fields are not blank)
2)CI validates and checks against the database
3)CODE: if {credentials match = redirect}
else { dynamic data for view }
#2

[eluser]mddd[/eluser]
The problem of the 'undefined variable' is easy to solve: in the view, don't check for $var==1 but check for isset($var) && $var==1. That's all.
Another solution can be to set the variable to an empty value and pass it.

The example you give is one of the easiest of giving variables to the view. Another way is this:
Code:
$data = array(
          ‘title’ => ‘My Title’,
          ‘heading’ => ‘My Heading’,
          ‘message’ => ‘My Message’
      );
$this->load->vars($data);  // now, the information is available in all views from here on
$this->load->view(‘blogview’); // you don't need to include $data here anymore
The advantage of this way is that you can call $this->load->vars() multiple times to load different pieces of information.
They will all be available to your views.
I think (not 100% sure) that they overwrite if there are duplicate keys. That means you could start by loading some 'empty' variables (like 'user_logged_in'=>false) and that would solve the problem of undefined variables as well. Only if the user is logged in you would do another $this->load->vars() and overwrite the empty values.
#3

[eluser]Namsu[/eluser]
I tried the isset code before didn't work let me try it again. Let me get a feel for your coding practice, is this how you normally perform a new output every time (errors and messages) the way I have done it or do you think there is a better and more common tactic.
#4

[eluser]Namsu[/eluser]
In my controller I have:

else {

$data = array( 'errors' => 1 );
$this->load->view('yascms/login', $data);

}

and in my view I have the line:

<?php if (isset($errors) && $errors == 1) { echo "Credentials invalid, your IP Address is logged"; }; ?>

I have a blank page. Not sure what is wrong with this code. If you can help me out that would be great.
#5

[eluser]mddd[/eluser]
I think the ; after your closing } should not be there.
Otherwise this seems fine to me.

Sometimes you don't get to see any errors because CI displays the view inside an output buffer.
You can always check the php error log to make sure you know what the problem is.




Theme © iAndrew 2016 - Forum software by © MyBB