• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

Hilfe bei PHP-Menü

Status
Für weitere Antworten geschlossen.

tommylik

Neues Mitglied
Hallo an alle,

Habe da ein PHP-Menu im Internet gefunden zu freien Verwendung.

Hier der Link: Julius Plenz - Webauthoring - Einfaches, Flexibles Menü mit PHP

Funktioniert ganz gut.

PHP:
<?php
/* $Id: generate_menu.php,v 1.9 2004/11/02 18:22:56 plenz Exp $
 * (c) 2004  Julius Plenz <[email protected]>  http://www.plenz.com/3
 *
 * This is free software. You can redistribute and modify it
 * under the terms of the GNU General Public License (GPL).
 * See http://www.gnu.org/copyleft/gpl.html for more info.
 */

class menu {
    function get_menu_from_file () {
        // Where is the menu-file located? Change this!
        $menu = menu::get_entries ($_SERVER["DOCUMENT_ROOT"] . './test/entries.txt');
        return $menu;
    }

    function get_real_filename ($file) {
        $file = str_replace (substr ($_SERVER["DOCUMENT_ROOT"], 0, -1), '', $file);
        return $file;
    }

    function generate_menu ($file = false) {
        $file = menu::get_real_filename ($file?$file:$_SERVER['SCRIPT_NAME']);
        $menu = menu::get_menu_from_file ();

        echo "<ul id=\"nav\">\n";
        menu::submenu ($file, $menu);
        echo "</ul>\n";
    }

    function submenu ($file, $menu) {
        foreach ($menu as $entry) {
            if (isset ($entry[2])) {
                if ($entry[1] == $file) {
                    menu::submenu_active   ($entry[0], $entry[2]);
                } else {
                    menu::submenu_inactive ($entry[0], $entry[1], $file, $entry[2]);
                }
            } else {
                if ($entry[1] == $file) {
                    menu::print_active ($entry[0]);
                } else {
                    menu::print_item   ($entry[0], $entry[1]);
                }
            }
        }
    }

    function submenu_active ($text, $menu) {
        printf ("<li><strong>%s</strong>\n", $text);
        echo "<ul>\n";
            menu::submenu (false, $menu);
        echo "</ul>\n";
        echo "</li>\n";
    }

    function submenu_inactive ($text, $url, $file, $menu) {
        printf ("<li><a href=\"%s\">%s</a>", $url, $text);
        if (menu::file_in_array ($file, $menu)) {
            echo "\n<ul>\n";
                menu::submenu ($file, $menu);
            echo "</ul>\n";
        }
        echo "</li>\n";
    }

    function print_item ($text, $url) {
        printf ("<li><a href=\"%s\">%s</a></li>\n", $url, $text);
    }

    function print_active ($text, $url = false) {
        printf ("<li><strong>%s</strong></li>\n", $text);
    }

    function file_in_array ($file, $menu) {
        foreach ($menu as $entry) {
            if (isset ($entry[2]) && is_array ($entry[2])) {
                if ($entry[1] == $file) {
                    return true;
                }
                if (menu::file_in_array ($file, $entry[2])) {
                    return true;
                }
            } else {
                if ($entry[1] == $file) {
                    return true;
                }
            }
        }
        return false;
    }

    function thelocation ($file, $menu = false) {
        $return = "";
        $file = menu::get_real_filename ($file);
        if (!$menu)
            $menu = menu::get_menu_from_file ();

        foreach ($menu as $entry) {
            if (isset ($entry[2]) && menu::file_in_array ($file, $entry[2])) {
                if ($entry[1] == $file) {
                    $return .= sprintf ("<strong>%s</strong>", $entry[0]);
                    return $return;
                } else {
                    $return .= sprintf ("<a href=\"%s\">%s</a> : ", $entry[1], $entry[0]);
                    $return .= menu::thelocation ($file, $entry[2]);
                }
            } else {
                if ($entry[1] == $file) {
                    $return .= sprintf ("<strong>%s</strong>", $entry[0]);
                    return $return;
                }
            }
        }
        return $return;
    }

    function location ($file, $menu = false) {
        echo menu::thelocation ($file, $menu);
    }

    function plainlocation ($file, $menu = false) {
        echo preg_replace ("/<[^>]+>/", "", menu::thelocation ($file, $menu));
    }

    function sitemap ($menu = false) {
        if (!$menu) {
            $menu = menu::get_menu_from_file ();
            echo "<ul id=\"sitemap\">\n";
            $menu_missing = true;
        }

        foreach ($menu as $entry) {
            if (isset ($entry[2])) {
                printf ("<li><a href=\"%s\">%s</a>\n<ul>\n", $entry[1], $entry[0]);
                menu::sitemap ($entry[2]);
                echo "</ul>\n</li>\n";
            } else {
               menu::print_item ($entry[0], $entry[1]);
            }
        }

        if (isset ($menu_missing))
            echo "</ul>\n";
    }

    function get_entries ($file = false) {
        if (!$file) {
            die ('No file!');
        }

        if (function_exists ("file_get_contents")) {
            $txt = file_get_contents ($file);
        } else {
            $txt = join ("", file ($file)) . "\n";
        }
        $txt = str_replace ("\r", "", $txt);     // strip windows-linebreaks
        $txt = str_replace ("\n\n", "\n", $txt); // squeeze empty lines
        $menu = menu::get_from_txt ($txt);
        return $menu;
    }

    function get_from_txt ($txt) {
        $lines = explode ("\n", $txt);
        unset ($lines[count ($lines)-1]); // remove empty line at the end
        $lnumbers = count ($lines);
        $menu = Array ();
        $sublines = "";
        $anumber = -1;

        for ($i=0; $i<$lnumbers; $i++) {
            $l = $lines[$i];
            $l = substr ($l, 1); // remove "-" at first position

            if (substr ($l, 0, 1) != "-") {
                if (strlen ($sublines) > 1) {
                    $menu[$anumber][2] = menu::get_from_txt ($sublines);
                    $sublines = "";
                }

                preg_match ("/^(.*)\s+(\S+)$/U", $l, $matches) || die ("No match!");
                list (, $text, $url) = $matches;
                $menu[++$anumber] = Array ($text, $url);
                continue;
            }

            $sublines .= $l . "\n";
        }

        if (strlen ($sublines) > 1) {
            $menu[$anumber][2] = menu::get_from_txt ($sublines);
            $sublines = "";
        }

        return $menu;
    }
}


/* Example-menu:
 * =============
 *
 * $ ls
 * entries.txt generate_menu.php [...]
 *
 * $ cat entries.txt
 * -Getting started /
 * --First Step /fs.html
 * --Second Step /ss.html
 * -FAQ /faq/
 * --FAQ: How to ... /faq/howto.html
 * --FAQ: Why is ... /faq/whyis.html
 * --Advanced /faq/advanced/
 * ---Foo /faq/advanced/foo.html
 * ---Bar /faq/advanced/bar.html
 * --... /im_tired...
 * -What's this about? /about.html
 * -Contact /contact.html
 * [...]
 *
 * $ grep menu:: some_file.php
 *      menu::generate_menu ($PHP_SELF);
 * OR:
 *      menu::generate_menu (__FILE__);
 *
 * If you call the menu::generate_menu without parameters, it'll
 * automatically use $_SERVER['SCRIPT_NAME'].
 *
 * Note: The Document-Root (/var/www/user/, /www/web01/, etc) will be
 * automatically stripped from the filename so you can use __FILE__.
 */

// vim:set ft=php sw=4 et: ?>
Zusätzlich benötigt man noch eine Textdatei für die Menüpunkte.

Die sieht bei mir folgendermaßen aus:

-Startseite /test/index.php
-Unternehmen /test/unternehmen.php
--Standort /test/standort.php
--Personal /test/personal.php
--Jobs /test/jobs.php
--History /test/history.php
-Solartechnik /test/solartechnik.php
--Photovoltaik /test/photovoltaik.php
---Produkte /test/pv_produkte.php
---Info /test/pv_info.php
---Planung /test/pv_planung.php
--Solarthermie /test/solarthermie.php
---Produkte /test/st_produkte.php
---Info /test/st_info.php
---Planung /test/st_planung.php
-Industrietechnik /test/industrietechnik.php
--Engineering /test/engineering.php
--Roboter /test/roboter.php
--SPS /test/sps.php
--Planung /test/i_planung.php
-Haustechnik /test/haustechnik.php
--Heizung /test/heizung.php
--Regenwasser /test/regenwasser.php
--Strom /test/strom.php
--Netzwerk /test/netzwerk.php
-News /test/news.php
-Downloads /test/downloads.php
-AGB /test/agb.php
-Impressum /test/impressum.php

Ich benötige nun eure Hilfe die PHP-Datei so abzuändern das ich mit CSS auch auf die einzelnen Items von dem PHP-Menü zugreifen kann.

Zur Zeit kann man ja nur auf das element UL mit der ID "nav" zugreifen.

Ein bischen php verständinis habe ich mir ja schon angeeignet aber diesen Code abzuändern das schaffe ich nicht.

Dies ist zur Zeit mein Idee was CSS betrifft:

Code:
ul#nav { background-color: orange; text-indent: 0.5em; width: 237px; margin: 0; padding: 0 0 0 0; border-bottom: 1px solid black; }
ul#nav li { width: 100%; margin: 0 0 -1px; padding: 0; border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; list-style-type: none; }
ul#nav li a { background-color: #ffc; width: 100%; margin: 0 0 0 0; padding: 1px 0; display: block; }
ul#nav li a:hover { color: red; background-color: teal; }
Damit versuch ich mich zur Zeit. Ich würde gerne noch 2-3 Klassen mit einbringen z.B. für die Untermenüs. Ich weiß aber nicht wo ich die in der PHP-datei einfügen muß. Oder ist dies so wie ich jetzt denke nicht möglich. Es wäre schön wenn mir einer von Euch dabei helfen könnte.

Ich sage schon mal im vorraus vielen Dank.

Mfg Tommylik
 
Werbung:
Status
Für weitere Antworten geschlossen.
Zurück
Oben