Jump to content

How to pass variable through php 'include' in a while/for loop


NeoMarine

Recommended Posts

I have passed an array into a php function. The array is structured like this:

 

$fileArray[0]['Name'] = "Joe";

$fileArray[0]['Age'] = "40";

$fileArray[1]['Name'] = "John";

$fileArray[1]['Age'] = "50";

 

I need to be able to do an "include" of html code located in another .php file... for that I used a function called "get_include_contents("myfile.php");"

 

In the php function if I say "return $fileArray[0]['Name'];" it will return "Joe" as expected, however if I put "echo $fileArray[0]['Name'];" inside of "myfile.php" and use the function get_include_contents to get the string from myfile.php, it will not echo out "Joe" because the string is not set??

 

How can I pass the array values into "myfile.php"? :-\ :-\

OK here is my code:

 

I have this function first of all:

 

function BuildFileDisplayHTMLTable($fileArray, $fileDisplayPath, $filesPerRow, $tableClass, $tdClass, $trClass){
	//Generates a table containing the desired number of "Files"

	$displayTable="";
	if ($filesPerRow==""){
		//Default files per row
		$filesPerRow=6;
	}

	if ($tableClass!=""){
		$displayTable .= "<table class=\"".$tableClass."\">";
	} else {
		$displayTable .=  "<table>";
	}

	$tdWidth=100/$filesPerRow;

	$i = 0;
	while ($i != sizeof($fileArray)) {
		//Check if number is divisible by files per row
		if ($i % $filesPerRow == 0){
			if ($trClass!=""){
				$displayTable .=  "<tr class=\"".$trClass."\">";
			} else {
				$displayTable .=  "<tr>";
			}
		}
		if ($tdClass!=""){
			$displayTable .=  "<td class=\"".$tdClass."\">";
		} else {
			$displayTable .=  "<td>";
		}
		$displayTable .=  get_include_contents($fileDisplayPath);
		$displayTable .=  "</td>";
		if (($i+1) % $filesPerRow == 0){
			$displayTable .=  "</tr>";
		}
		$i++;
	}

	$displayTable .=  "</table>";
	return $displayTable;
}

 

The contents of file "myfile.php":

 

<?php
echo $fileArray[$i]['name']."<the name";
?>

 

And the page where its all loaded from (i.e index.php) contains:

 

<?php
	$fileQuery=mysql_query("query to get all the files and their name/description/etc");		
	$fileArray = array();
	while ($fileProperty=mysql_fetch_array($fileQuery)){
		$fileArray[]=$fileProperty;
	}
	$filesPerRow=6;
	$tableClass="";
	$tdClass="linkbox3";
	$trClass="";
	$fileDisplayPath="myfile.php";
	echo BuildFileDisplayHTMLTable($fileArray, $fileDisplayPath, $filesPerRow, $tableClass, $tdClass, $trClass);
?>

Sorry, the function get_include_contents is defined beforehand.

 

Here is the code for that:

 

function get_include_contents($filename) {
	if (is_file($filename)) {
		ob_start();
		include $filename;
		$contents = ob_get_contents();
		ob_end_clean();
		return $contents;
	}
	return false;
}

 

Basically, instead of including the php file, I need to return a STRING, so I use get_include_contents to return me a STRING value of the contents of myfile.php

Ok sure... uhmmmm

 

First the functions are declared: (file: index.php)

 

<?php
function get_include_contents($filename) {
	if (is_file($filename)) {
		ob_start();
		include $filename;
		$contents = ob_get_contents();
		ob_end_clean();
		return $contents;
	}
	return false;
}

function [b]BuildFileDisplayHTMLTable[/b]($fileArray, $fileDisplayPath, $filesPerRow, $tableClass, $tdClass, $trClass){
	//Generates a table containing the desired number of "Files"

	$displayTable="";
	if ($filesPerRow==""){
		//Default files per row
		$filesPerRow=6;
	}

	if ($tableClass!=""){
		$displayTable .= "<table class=\"".$tableClass."\">";
	} else {
		$displayTable .=  "<table>";
	}

	$tdWidth=100/$filesPerRow;

	$i = 0;
	while ($i != sizeof($fileArray)) {
		//Check if number is divisible by files per row
		if ($i % $filesPerRow == 0){
			if ($trClass!=""){
				$displayTable .=  "<tr class=\"".$trClass."\">";
			} else {
				$displayTable .=  "<tr>";
			}
		}
		if ($tdClass!=""){
			$displayTable .=  "<td class=\"".$tdClass."\">";
		} else {
			$displayTable .=  "<td>";
		}
		$displayTable .=  get_include_contents($fileDisplayPath);
		$displayTable .=  "</td>";
		if (($i+1) % $filesPerRow == 0){
			$displayTable .=  "</tr>";
		}
		$i++;
	}

	$displayTable .=  "</table>";
	return $displayTable;
}
?>

 

Then the function is called: (file: index.php)

 

<?php
	$fileQuery=mysql_query("query to get all the files and their name/description/etc");		
	$fileArray = array();
	while ($fileProperty=mysql_fetch_array($fileQuery)){
		$fileArray[]=$fileProperty;
	}
	$filesPerRow=6;
	$tableClass="";
	$tdClass="linkbox3";
	$trClass="";
	[u]$fileDisplayPath="myfile.php";[/u]
	echo [b]BuildFileDisplayHTMLTable[/b]($fileArray, $fileDisplayPath, $filesPerRow, $tableClass, $tdClass, $trClass);
?>

 

And the function calls the contents of the file $fileDisplayPath (which is myfile.php). The content of myfile.php is:

 

<?php
echo $fileArray[$i]['name']."<the name";
?>

 

So essentially, I am trying to pass the $fileArray from index.php > function "BuildFileDisplayHTMLTable" > myfile.php

 

Also, myfile.php will need the $i variable declared in function "BuildFileDisplayHTMLTable"

Yeah, that creates all sorts of variable scope issues. To get a variable into a function without it being an argument, you need "global $varname;" at the beginning of the function (inside the {}). So, try either adding $fileArray to your arguments for function get_include_contents or add global $fileArray to the top of your get_include_contents function.

Thank you!

 

By adding:

 

	global $fileArray;
global $i;

 

to the top of myfile.php

 

as well as

 

global $i;

 

inside the function BuildFileDisplayHTMLTable

 

It now works! (still a few kinks but I can figure the rest out with the scoping...)

GLOBALS ARE BAD!!!

 

Ok now that that's over, be very careful using globals, they are normally disabled in PHP5 and don't exist in PHP6. Please reevaluate you design you should be able to come up with a better way to build your script.

Globals are disabled?! Whaaaa?! Do you mean the global keyword is removed? Even this doesn't make sense.

 

From what I've read the only global namespace change is the fact that register_globals will no longer be an option.

Globals are disabled?! Whaaaa?! Do you mean the global keyword is removed? Even this doesn't make sense.

 

From what I've read the only global namespace change is the fact that register_globals will no longer be an option.

Register Globals will disappear

 

Say good bye to that setting because it will be finally removed. php.ini also won’t include that option, and if you include it yourself you’ll get the E_CORE_ERROR. It means that PHP 6 will finish the epoch of the PHP 3 scripts. It is very serious but necessary step.

There was no mention of register_globals before my post. I understand what will change in PHP6... and it has nothing to do with this:

 

GLOBALS ARE BAD!!!

 

Ok now that that's over, be very careful using globals, they are normally disabled in PHP5 and don't exist in PHP6. Please reevaluate you design you should be able to come up with a better way to build your script.

 

This thread is talking about using the global keyword to make global variables available to a function's scope. Nothing in PHP6 will prevent this.

 

I'm just clearing up any confusion that might come from such a misguided statement.

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.