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??

Dani AI

Generated

Short diagnosis and what's actually stopping output: was right that scope is a problem, but there are two other mistakes that together leave menu.php empty. PHP concatenation uses . (or .=') — not+— so expressions like$buffer = $buffer + '...'do not append strings. Also, functions in PHP have local scope by default, so the$bufferyou set outside those functions is not the same$bufferinside them unless you declareglobalor return the string from the function. Finally, the JavaScript toggle is written to expect an element but the generated onclicks pass an identifier incorrectly; either pass the element itself or pass an id string and resolve it withdocument.getElementById()`.

A simple, minimal pattern that avoids globals and fixes concatenation (produce the HTML from PHP and toggle by id in JS):

<?php
function buildMenu() {
    $out = '';
    $out .= '<div style="position:absolute; top:100px; left:100px;">';
    $out .= '<p onclick="toggleMenu(\'menu1\')" class="menu-header" style="cursor:pointer;">Click me</p>';
    $out .= '<div id="menu1" style="visibility:hidden;">';
    $out .= '<a href="http://www.google.com" rel="nofollow">Google</a><br>';
    $out .= '</div></div>';
    return $out;
}
echo buildMenu();
function toggleMenu(id) {
    var el = document.getElementById(id);
    if (!el) return;
    el.style.visibility = (el.style.visibility === 'visible') ? 'hidden' : 'visible';
}

Quick troubleshooting checklist and best practices: enable errors early (ini_set('display_errors',1); error_reporting(E_ALL);) so PHP warnings show; replace the fopen/fread/filesize pattern with file_get_contents() or readfile() for static HTML includes; when adding menu items pass a plain URL (not an already-wrapped <a> tag) so the function builds valid anchors; avoid duplicate id attributes for headers (use a class instead); view the page source in the browser to confirm whether PHP emitted any HTML at all. Tying back to the thread: pointed to scope — fix that plus switch + to . (or use return values) and the menu should appear.

Recommended Answers

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.