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"? :-\ :-\

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.