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, 2022 in PHP by Kithuzzz
• 38,010 points
1,062 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, 2022 in PHP by Kithuzzz
• 38,010 points
10,360 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,880 points
24,183 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, 2022 in PHP by Kichu
• 19,050 points
828 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, 2022 in PHP by narikkadan
• 63,420 points
4,434 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, 2022 in PHP by Kithuzzz
• 38,010 points
1,423 views
0 votes
1 answer

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

modify your code like this and will ...READ MORE

answered Feb 24, 2022 in Others by narikkadan
• 63,420 points
762 views
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, 2022 in Others by narikkadan
• 63,420 points
907 views
0 votes
0 answers

PHP function to generate v4 UUID

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

Jun 4, 2022 in PHP by Kichu
• 19,050 points
1,111 views
0 votes
0 answers

How to base64 encode php in this format?

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

Jun 10, 2022 in PHP by narikkadan
• 63,420 points
1,072 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP