Jump to content

Cannot include array from another file...


Kane250

Recommended Posts

Hi all,

 

I'm at a dead-end here.  I have a php file that I can query to return an array containing a bunch of data about a job listing. I include and query it by calling it like so within another file on my site. 

 

Quick Notes:

  • The original file is in a different directory on my site than the page it's being included in.
  • I have tried returning the array at the end of the script.
  • I can print_r the array on the page I'm including the script on (so the URL path is correct).

 

If I go to this URL directly and print_r on the array, I see the entire array as expected.  Also, if I include the file on page 2 like I did above with print_r on the array in the original file, I see the array printed out on page 2.

 

include 'http://www.example.com/php/job-details.php?job=jobname&city=thecity';

 

However, if I do not print_r on the array in the original file and just include it on page 2, I cannot do anything with it and the array variable isn't found.

 

Is this a case of variable scope?  I'm so frustrated...

 

Here is the code I have in my original file:

 

<?
include('functions.php');

$jobtitlematch = $_GET["job"];
$jobcitymatch = $_GET["city"];
//echo $jobtitlematch;
//echo $jobcitymatch;

$url = 'http://somesite.com/something';
$xml = simplexml_load_file($url);

foreach($xml->job as $job) {
$jobtitle = makeURL((string)$job->title);
$jobcity = makeURL((string)$job->region);

if ($jobtitle == $jobtitlematch && $jobcity == $jobcitymatch) {		
	$jobdata[] = array(
		Title => ((string)$job->title),
		URL_Title => makeURL((string)$job->title),
		Location => ((string)$job->region),
		URL_Location => makeURL((string)$job->region),
		Department => ((string)$job->department),
		URL_Department => makeURL((string)$job->department),
		Overview => ((string)$job->joboverview),
		Responsibilities => ((string)$job->responsibilities),
		Qualifications => ((string)$job->qualifications),
		Keywords => ((string)$job->metakeywords)
		);
	}
return $jobdata;
//I have also tried return global $jobdata;
}
print_r($jobdata);
?>

 

Thanks in advance for any help you can provide...

Link to comment
https://forums.phpfreaks.com/topic/235455-cannot-include-array-from-another-file/
Share on other sites

you cannot include a URL

you should include a file not a url, for instance /var/www/a-file.php

you can include that file, but force the $_GET with the keys you want.

and you cannot use return without a function

 

if you want to use job-details both way, then force the $_GET vars

$_GET['job']='jobname';
$_GET['city']='thecity';
include_once 'php/job-details.php';

but if you don;t need to execute job-details.php directly from the browser, then just set the job and city vars as normal variables not $_GET and read them in the other file.

 

I'd recommed that you read more about php

 

I hope my reply was helpful

 

Goodluck

 

 

you cannot include a URL

you should include a file not a url, for instance /var/www/a-file.php

you can include that file, but force the $_GET with the keys you want.

and you cannot use return without a function

 

you can include a URL as long as URL fopen Wrappers is enabled...which it is by default

@fugix What version are you using, because its closed by default since php 5.2, and you have to have both directives on

allow_url_fopen and allow_url_include

 

it was closed because it is a major security risk, if you got it @fugix try to close it, for your server's security

 

for reference, study something from the manual: http://php.net/manual/en/filesystem.configuration.php

 

Since php code isn't included when you use a URL, none of the settings that affect if a URL can be used in an include statement are even relevant to the problem, especially since the OP can already use a URL in an include statement.

 

When you use a URL in an include, only the output from the php code gets included, the same as if you browsed to the file. This also takes 50-100 times longer then if you included the file through the file system.

 

Just do what BloodyMind suggested in reply #1 in the thread, include the file using the file system and if needed set up any $_GET variables that the included code needs.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.