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();
?>
$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.
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.