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