Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iTim314

macrumors 6502
Original poster
Jun 5, 2005
337
6
U.S.
Lets say I have about 4 php files within a directory. I want to include all those files... but I only want to get a certain variable from those pages to be displayed on one page.

PHP:
	foreach(glob("ResourceFile-*.php") as $GlobalFile)
		{
			include_once($GlobalFile);
			echo $Variable;
		}

Inside each file, "ResourceFile-1.php, ResourceFile-2.php, etc," is a variable set as....

PHP:
$Variable .= "Content 1";
...respectively.

I know the include(), or include_once() isn't the way to go in the glob() script. I don't want all the variables on the ResourceFiles-*.php, just one specific one, so that It'll echo "Content 1, Content 2," etc.

Sorry if my wording is a bit weird, I really can't think of a better way to put it. How would I go about doing this?
 

angelwatt

Moderator emeritus
Aug 16, 2005
7,852
9
USA
Not sure why you're trying to do it this way. Why not have some preferences file that has the values from the PHP files and just parse it out? Or you can make one PHP file that simply setups these needed variables and include that file. I do this by having my mail variables or database variables in one file, and just include it where/when I need it.
 

alfaphlex

macrumors newbie
Feb 18, 2008
28
0
It's pretty hard to give you the right answer without knowing more details, but in general, what you have is a scope problem. IMO, the simplest solution would be to implement that snippet of code as a function, so that the included file dont affect any variables outside the function scope. Something like:
PHP:
function getVarFromResource($resource, $varname){
    foreach(glob($resource) as $GlobalFile)
       {
           include_once($GlobalFile);
           echo $$varname;
       } 
}
that way you'd be able to use it with any filename you wanna glob(). as well as any variable you want to display from the globbed files, ie:
PHP:
// the file you wanna glob()
$filename = "ResourceFile-*.php";

// the name of variable you wanna display
$varname = "Variable";

getVarFromResource($filename,$varname);

// now lets display any other variable in you globbed files
$varname = "Test";

// This would display any $Test variables
getVarFromResource($filename,$varname);

However, the problem with the above is that if any of the files that you include() outputs anything (i.e- echo), that content will be displayed automatically. To avoid that and display ONLY the variable you want, this would be more appropriate:
PHP:
function getVarFromResource($resource,$varname){
    foreach(glob($resource) as $GlobalFile)
       {
           // start output buffering (nothing after this gets displayed)
           ob_start();

           include_once($GlobalFile);
           
           // retrieve the variable you want
           $bufferedvar = $$varname;

           // clean the buffer (anything that would've been displayed is cleared)
           ob_clean();

           echo $bufferedvar;
       } 
}

If any of your other files have any echo statements, they'll be ignored. Any variable in the included files will not affect the global scope and you have a reusable function that you can apply to any future similar situations.

Hope any of this helps.
 

macgruder

macrumors 6502
Oct 29, 2007
280
0
UK
PHP:
$i=0;
 foreach(glob("ResourceFile-*.php") as $GlobalFile)
        {

            include_once($GlobalFile);
            $foo[$i++] = $Variable;
          
        }

Then grab what you need from the array.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.