PHP using extract to pass array values as variables and display it on a page

0 votes

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.

Aug 6 in PHP by Kithuzzz
• 11,440 points
103 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
0 answers

How to GET data from API and show it on PHP page?

I want to use some data from ...READ MORE

Jul 31 in PHP by Kithuzzz
• 11,440 points
72 views
0 votes
1 answer

How to call function of one php file from another php file and pass parameters to it?

Hello @kartik, Include the first file into the ...READ MORE

answered Aug 27, 2020 in PHP by Niroj
• 82,780 points
15,109 views
0 votes
0 answers

How to extract img src, title and alt from html using php?

I am working on a page where ...READ MORE

Jun 3 in PHP by Kichu
• 19,040 points
95 views
0 votes
0 answers

How to pass a PHP variable using the URL

I need to pass some PHP variables ...READ MORE

Jun 19 in PHP by narikkadan
• 19,280 points
112 views
0 votes
0 answers

How to redirect a page using onclick event in php?

I tried this, but it didn't work: <input ...READ MORE

Jun 20 in PHP by Kithuzzz
• 11,440 points
109 views
0 votes
1 answer
0 votes
1 answer

Wordpress Critical Error Due to Plugin Conflict with my Functions.php

// WooCommerce Stock message add_filter( 'woocommerce_get_availability', 'mw_get_availability', ...READ MORE

answered Feb 26 in Others by narikkadan
• 19,280 points
307 views
0 votes
0 answers

PHP function to generate v4 UUID

I am working on a function that ...READ MORE

Jun 4 in PHP by Kichu
• 19,040 points
120 views
0 votes
0 answers

How to base64 encode php in this format?

Can someone please guide me on how ...READ MORE

Jun 10 in PHP by narikkadan
• 19,280 points
180 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP