Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-09-2010, 12:48 PM   PM User | #1
GJ384
New to the CF scene

 
Join Date: Feb 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
GJ384 is an unknown quantity at this point
Trouble with a function which references an included array

Hey guys,

I'm having some trouble with a function which references an included array.

I have a tooltip set up to appear when the mouse rolls over a table cell. When the tooltop appears, it shows a set of figures which have been brought in from the array.

My include and function definition code looks like this, and appears before the <html> tag:
PHP Code:
<?
include("calcs/calcdata.php");
?>

<?php
    
function getToolTipData($selectedFood) {
        echo 
"<b>$selectedFood</b> - impact per ".$Foods[$selectedFood]['unit'].":<br /><table>";
        echo 
"<tr><td align=right>Carbon:</td><td>".$Foods[$selectedFood]['kgCO2e_u']."kg</td></tr>";
        echo 
"<tr><td align=right>Water:</td><td>".$Foods[$selectedFood]['LH2O_u']."L</td></tr>";
        echo 
"<tr><td align=right>Pasture Land:</td><td>".$Foods[$selectedFood]['haPasture_u']."ha</td></tr>";
        echo 
"<tr><td align=right>Cropland:</td><td>".$Foods[$selectedFood]['haCrop_u']."ha</td></tr>";
        echo 
"<tr><td align=right>Air Pollution:</td><td>".$Foods[$selectedFood]['ug_u']."micrograms</td></tr>";
        echo 
"<tr><td align=right>Species at Risk:</td><td>".$Foods[$selectedFood]['sar_u']."</td></tr>";
        echo 
"</table>";
    }
?>
Each line in the array, which is defined in "include/calcdata.php", appears as follows:
PHP Code:
$Foods['Apples'] = array("unit" => "kg","kgCO2e_u" => 0,"LH2O_u" => 700,"haPasture_u" => 0,"haCrop_u" => 0,"ug_u" => 0,"sar_u" => 0); 
Finally, this is what the code to get the data for the tooltip looks like:
PHP Code:
<?php getToolTipData('Apples'); ?>
My tooltip ends up looking like this:

Apples - impact per :
Carbon: kg
Water: L
Pasture Land: ha
Cropland: ha
Air Pollution: micrograms
Species at risk:

There's nothing wrong with the code to show the tooltip itself, as it was working perfectly until I tried to put it into a function (although I can send this through too if it's required, of course). I also know that the function is receiving the string "Apples", since it shows up in the above example. The problem seems to be that the string isn't being correctly sent to/received by these statements:

PHP Code:
echo "<tr><td align=right>Carbon:</td><td>".$Foods[$selectedFood]['kgCO2e_u']."kg</td></tr>"
(and the like)

Does anyone have any idea what I'm doing wrong? It's probably something bleedingly obvious and embarrassingly simple, but for the life of me I can't seem to work it out... Any help would be greatly appreciated!

Thanks in advance!
Gareth
GJ384 is offline   Reply With Quote
Old 02-09-2010, 12:58 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The problem here is simple, but the best approach is a little debatable.
You're issue is called scope. With a function, you cannot see any variables not decalared within it except for global variables.
So you're options are:
  • Include the calcdata.php inside of the function itself. Use an include_once to prevent an error toss.
  • Use a 'global' keyword in you're function to import, or push the $Foods into $GLOBALS['Foods']. These are NOT my recommended approaches due to re-usability issues.
  • Pass the $Foods array into the function.

My personal recommendation is to pass the array into the function. This way you can call it whatever you want:
PHP Code:
    function getToolTipData($selectedFood, array $Foods) {
        echo 
"<b>$selectedFood</b> - impact per ".$Foods[$selectedFood]['unit'].":<br /><table>";
        echo 
"<tr><td align=right>Carbon:</td><td>".$Foods[$selectedFood]['kgCO2e_u']."kg</td></tr>";
        echo 
"<tr><td align=right>Water:</td><td>".$Foods[$selectedFood]['LH2O_u']."L</td></tr>";
        echo 
"<tr><td align=right>Pasture Land:</td><td>".$Foods[$selectedFood]['haPasture_u']."ha</td></tr>";
        echo 
"<tr><td align=right>Cropland:</td><td>".$Foods[$selectedFood]['haCrop_u']."ha</td></tr>";
        echo 
"<tr><td align=right>Air Pollution:</td><td>".$Foods[$selectedFood]['ug_u']."micrograms</td></tr>";
        echo 
"<tr><td align=right>Species at Risk:</td><td>".$Foods[$selectedFood]['sar_u']."</td></tr>";
        echo 
"</table>";
    } 


// Called with:
getToolTip('Apples'$Foods); 
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
GJ384 (02-09-2010)
Old 02-09-2010, 08:48 PM   PM User | #3
GJ384
New to the CF scene

 
Join Date: Feb 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
GJ384 is an unknown quantity at this point
Wow, works like a charm - thanks a million! I really appreciate your help.
GJ384 is offline   Reply With Quote
Reply

Bookmarks

Tags
array, function, include

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:30 PM.


Advertisement
Log in to turn off these ads.