The included file is not behaving as you expect it to. To pull this off you will have to create a global out of the variable (but don’t do it!). You can print_r()
the array in the controller, because you’re fetching it from the model. But after that you include the view. When you place that array in an included file, without defining it in the file itself, you’re not going to get a result because only the including file has access to any defined variables. Not the other way around.
So if you want this to work you’re going to either have to make the variable $arr
global (not recommended), or include the controller in view.php
. (Note that this is not MVC!) If you want this to be MVC you’ll have to use classes for the controllers and use those objects in the view. To find a better explanation for this please refer to the links down below.
I am personally fond of using of a templating engines (also not MVC related). I’m quite fond of Smarty, but I’ve heard good stories about mustache too.
These templating engines provide you with a way to pass variables to your templates, without including the controller file. For example in Smarty:
PHP:
$variable="Hello World!";
$smarty->assign('variable', $variable);
$smarty->display('helloworld.tpl');
Smarty, helloworld.tpl:
{$variable}
Output:
Hello World!
I’d also recommend you to read these, they might help you out more than I’ve been able to explain.
- Setting up MVC in PHP: http://www.sitepoint.com/the-mvc-pattern-and-php-1/
- Including files in PHP: Passing a variable from one php include file to another: global vs. not
Edit
Teresko is right that you should not be using globals. I just specified them here, because it’s possible to use them (definitely not recommended).
Read this question for a couple of why’s: Stop using `global` in PHP
7
solved How do I organize this list into mvc?