Welcome Guest, Not a member yet? Register   Sign In
undefined variables
#1

[eluser]cityzen[/eluser]
So I am somewhat new to CI and OOP, know PHP pretty well. I keep getting these "Undefined variable" notices. I am using a form to both and or update data which looks like:

Code:
<p><label for="title">Title:</label>&lt;input type="text" name="title" value="&lt;?= $title ?&gt;"/&gt;</p>
<p><label for="link">Link:</label>&lt;input type="text" name="link" value="&lt;?= $link ?&gt;" /&gt;</p>

and my controller looks like:

Code:
function add(){
        $data['title'] = '';
        $this->load->view('news/addmod', $data);            
    }

Do i really have to keep declaring or defining the variable in each function? It seems a bit round about to me. I'm really trying to understand and adhere to best coding practices so if anyone can let me know what the normal practice is for declaring variables like that, I would appreciate it. My background is in somewhat hacked PHP so I've never been really clean about declaring or defining all of my variables (since error/notice reporting is pretty lax). Also, is there a way to turn off that level of error reporting and only show actual warnings?

Thanks!

Mike
#2

[eluser]xwero[/eluser]
Turning off the error reporting doesn't stop the errors from happening so that is not a solution.

If you have variables that are recurring you can use the load->vars method. then you can do
Code:
__construct()
{
   $this->load->vars(array('title'=>''));
}

function add(){
        $this->load->vars(array('title'=>'Edit page'));
        $this->load->view('news/addmod');            
    }
This solves the “Undefined variable” notices
#3

[eluser]cityzen[/eluser]
ahh, maybe i'm missing a step by trying to assign my variables in my view (i know, i know) as opposed to in my controller via a model. The whole MVC way of thinking is new to me but now it's making sense. I do agree about errors, though. Just in one of those "hmm, i'll try CI for this project I'm doing" and finding that i'm further behind than I'd like Smile

When I get things rolling, though, it is a lot of fun!

Thanks again.




Theme © iAndrew 2016 - Forum software by © MyBB