NeoMarine Posted September 19, 2008 Share Posted September 19, 2008 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"? :-\ :-\ Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted September 19, 2008 Share Posted September 19, 2008 Please show your code. This sounds like a variable scope problem though. http://us2.php.net/variables.scope Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 19, 2008 Share Posted September 19, 2008 In the top of your function inside of myfile.php, add this line: <?php global $fileArray; ?> Quote Link to comment Share on other sites More sharing options...
NeoMarine Posted September 19, 2008 Author Share Posted September 19, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
NeoMarine Posted September 19, 2008 Author Share Posted September 19, 2008 In the top of your function inside of myfile.php, add this line: <?php global $fileArray; ?> that didn't work Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 19, 2008 Share Posted September 19, 2008 Where is get_include_contents defined? Why don't you use include? Quote Link to comment Share on other sites More sharing options...
NeoMarine Posted September 19, 2008 Author Share Posted September 19, 2008 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 Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 19, 2008 Share Posted September 19, 2008 OK, I'm lost. Your code is too complicated to follow all broken out like that. List it all in order with the file name right before each section of code so it's easier to follow. Quote Link to comment Share on other sites More sharing options...
NeoMarine Posted September 19, 2008 Author Share Posted September 19, 2008 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" Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 19, 2008 Share Posted September 19, 2008 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. Quote Link to comment Share on other sites More sharing options...
NeoMarine Posted September 19, 2008 Author Share Posted September 19, 2008 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...) Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted September 19, 2008 Share Posted September 19, 2008 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. Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 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. Quote Link to comment Share on other sites More sharing options...
Stooney Posted September 19, 2008 Share Posted September 19, 2008 Stay away from globals wherever possible, which is most of the time. Even if you have to pass a variable through 4 functions to get it in the right spot it's better than using a global. Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted September 19, 2008 Share Posted September 19, 2008 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. Quote Link to comment Share on other sites More sharing options...
Stooney Posted September 19, 2008 Share Posted September 19, 2008 What dyslexicdog is talking about is $_GET, $_POST, $_REQUEST super globals. PHP 6 is just making so that you can't refer to $_POST['var1'] as $var1. Globals will still exist in php6. Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.