...

View Full Version : Trouble with a function which references an included array



GJ384
02-09-2010, 12:48 PM
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:


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


$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 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:



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

Fou-Lu
02-09-2010, 12:58 PM
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:


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

GJ384
02-09-2010, 08:48 PM
Wow, works like a charm - thanks a million! I really appreciate your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum