Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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); 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    GJ384 (02-09-2010)

  • #3
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Wow, works like a charm - thanks a million! I really appreciate your help.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •