0

When I try to call a function that I've created, it's not outputting anything. If I insert the function code directly it works fine, but when calling the function nothing shows on screen.

<?php
    function MRYMENUCALL() {
        echo '<div class="menu_column Left">';
        foreach ($Menuitems as $Menuitem) {
            if ($Menuitem['Menu'] == 1) {
                if ($Menuitem['Column'] == 0) {
                    echo '<div class="menu_item" id="'
                        . $Menuitem['id']
                        . '" onmouseover="menu_expand(this.id);"><div class="menu_name Left">'
                        . $Menuitem['Name']
                        . '</div><div class="menu_price Right">'
                        . $MenuItem['Price']
                        . '</div><div class="menu_details Left" id="'
                        . $Menuitem['id']
                        . '"><div class="menu_description Left">'
                        . $Menuitem['Description']
                        . '</div>';
                    if ($Menuitem['Picture'] !=  NULL ){
                        echo '<div class="menu_picture"><img class="menu_image Right" src="/IMG/Menu/'
                            . $Menuitem['Picture']
                            . '"></div>';
                    }
                    echo '</div></div>';
                }
            }
        }
        echo '</div>';
    }

    //Left Menu
    MRYMENUCALL();
?>
3
  • Have you checked your PHP logs to see if there is anything wrong? Commented Feb 11, 2014 at 23:07
  • 2
    My guess is $menuitems isn't in scope of the function. Try putting "global $menuitems;" as the first line of your function. Commented Feb 11, 2014 at 23:08
  • @Ben That was my problem, thanks alot! Commented Feb 11, 2014 at 23:18

2 Answers 2

1

$Menuitems most likely is outside the scope of this function. If you went to your log files, you'll most likely see something telling you so. You'll either need to define it inside that function before using it, or pass it in as an argument to that function.

Also, find your php.ini file and change display_errors to 1. Also, set html_errors to 1 while you're in there and restart apache. It'll start outputting errors on the page instead of quietly to the log.

Sign up to request clarification or add additional context in comments.

Comments

0

When trying to run your code you will get(demo):

<div class="menu_column Left">
Warning: Invalid argument supplied for foreach() on line 4
</div>

So, you should pass $Menuitems variable as a paremeter of MRYMENUCALL() function. Try this code:

    <?php
// you define $Menuitems somewhere
// eg. $Menuitems = array(array('Menu' => 1, 'Column' => 0, 'id' => 1, 'Name' => Asd, 'Price' => 2, 'Picture' => asd));
        function MRYMENUCALL($Menuitems) {
            echo '<div class="menu_column Left">';
            foreach ($Menuitems as $Menuitem) {
                if ($Menuitem['Menu'] == 1) {
                    if ($Menuitem['Column'] == 0) {
                        echo '<div class="menu_item" id="'
                            . $Menuitem['id']
                            . '" onmouseover="menu_expand(this.id);"><div class="menu_name Left">'
                            . $Menuitem['Name']
                            . '</div><div class="menu_price Right">'
                            . $MenuItem['Price']
                            . '</div><div class="menu_details Left" id="'
                            . $Menuitem['id']
                            . '"><div class="menu_description Left">'
                            . $Menuitem['Description']
                            . '</div>';
                        if ($Menuitem['Picture'] !=  NULL ){
                            echo '<div class="menu_picture"><img class="menu_image Right" src="/IMG/Menu/'
                                . $Menuitem['Picture']
                                . '"></div>';
                        }
                        echo '</div></div>';
                    }
                }
            }
            echo '</div>';
        }

        //Left Menu
        MRYMENUCALL($Menuitems);
    ?>

Working demo: link on Codepad.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.