I have a little problem ... my menu.php does not work and i dunno why
heres my index:

<html>
<?php

function writeOut($pageName)
{
    $buffer = fopen($pageName, r);
    $data   = fread($buffer, filesize($pageName));
    fclose($buffer);
    echo($data);
}

writeOut('head.html');
echo('<body><div id="top">');
writeOut('top.html');
echo('</div>');
/* 
in case of anomalies

ob_start();
include 'menu.php';
$result = ob_get_clean();
*/
include 'menu.php';
echo('');
// reserved for content
echo('');
writeOut('right.html');
echo('</body>');

?>
</html>

and my menu.php:

<?php

/* Example in menu_skuska.html */
$buffer = '';
function addMenuHeader($header_name,$id,$distance_from_top,$distance_from_left)
{
    $buffer = $buffer + '<div style="position:absolute; top:' + $distance_from_top + '; left:' + $distance_from_left + ';">';
    $buffer = $buffer + '<p onClick="toggleMenu(' + $id + ')" id="menu_header">' + $header_name + '</p>';
    $buffer = $buffer + '<div id="' + $id + '" style="visibility:hidden;">';
}

function addMenuItem($item_name,$link)
{
    $buffer = $buffer + '<a href="' + $link + '">' + $item_name + '</a><br>';
}

function finalizeMenu()
{
    $buffer = $buffer + '</div></div>';
}

function printMenu()
{
    echo($buffer);
}

addMenuHeader('Click me','menu1','100px','100px');
addMenuItem('Google','http://www.google.com');
addMenuItem('Facebook','http://www.facebook.com');
finalizeMenu();
printMenu();

?>

<script>

function toggleMenu(obj)
{
    if (obj.style.visibility == "visible")
    {
        obj.style.visibility = "hidden";
    }else   obj.style.visibility = "visible";
}

</script>

the menu.php is meant to generate somethink like this:

<div style="position:absolute; top:10%; left:10%; color:green;">
    <p onClick="ToggleMenu(toggled)" style="cursor: pointer;">skuska</p>
    <div id="toggled" style="visibility:hidden;">
        skuska2<br>
        skuska3
    </div>
</div>

<script>
    function ToggleMenu(obj)
    {
        if (obj.style.visibility == "visible")
        {
            obj.style.visibility = "hidden";
        }else   obj.style.visibility = "visible";
    }
</script>

so why does menu.php simply echo nothink??

Recommended Answers

Hello, there are many ways you can learn and write PHP, OOP is one of them and you are using one other, functional. Well in PHP functional variables inside functions have a scope only inside them, if you want to use them in a global manner you should declare that …

Jump to Post

Tomashqooo you didn’t read my answer or the page I sent you. Ok let’s make it more clear (but PHP doc is our friend). You wrote

   function printMenu()
  {
      echo($buffer);
  }

$buffer has a scope only within printMenu function. It has nothing to do with the $buffer = …

Jump to Post

All 5 Replies

is your index saved as index.html ? It won't work i guess! try saving it as index.php
Although I haven't gone into your coding to see if its correct.

index is .php of course ... everythink worked till i tryed to generate a menu :(

Hello, there are many ways you can learn and write PHP, OOP is one of them and you are using one other, functional. Well in PHP functional variables inside functions have a scope only inside them, if you want to use them in a global manner you should declare that before using. Take a look at
http://php.net/manual/en/language.variables.scope.php
it a explains it a lot .

If you have a look at my code ... the variable is declared before its used ... didnt help, althrough i tryed globals, but no output at all ... pls help me some1

Tomashqooo you didn’t read my answer or the page I sent you. Ok let’s make it more clear (but PHP doc is our friend). You wrote

   function printMenu()
  {
      echo($buffer);
  }

$buffer has a scope only within printMenu function. It has nothing to do with the $buffer = ''; you declared outside it. If you want to use the global scope $buffer variable from within a function you must declare it first e.g.

  function printMenu()
  {
       global $buffer;
       echo($buffer);
  }

This goes also for all the other functions that you are using a global variable inside a function without declaring it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.