In order to include a specific file from a directory and pass data or variables into that page, I am trying to create a view($path,$data) function in PHP. I was able to create $path and include the defined path, but my next step is to pass the $data values into my included page. I also want to turn each array label into a variable.
My php class is below classes.php.
define("SITE_NAME", "process");
class helpers {
public function view($path, $data)
{
$dir = $_SERVER['DOCUMENT_ROOT'].SITE_NAME."/";
include($dir.$path.".php");
return extract($data);
}
}
On my index.php:
require_once('classes.php');
$helper = new Helpers();
$data['title'] = "My Test";
$data['test'] = "test1";
$helper->view('test',$data);
So, I'm trying to echo $title on my test.php, which I assume will yield the result of My Test. To see if I'm getting the values from index.php, I was able to output $data using print r.
Array ( [title] => My Test [test] => test1 )
Any guidance? How can I accomplish this? I was attempting the extract() function but am unsure about my syntax. I appreciate it.