【问题标题】:cant get menu to work无法让菜单工作
【发布时间】:2016-12-12 19:53:17
【问题描述】:

所以我创建了一个菜单,但我无法获取到工作页面的链接。 在 PHPmyadmin 中,我有一个名为 menus 的表。

在表格中,您可以找到 ID、Name、parentid(用于子菜单)和 href(这些字段仅包含页面名称,例如 index.php/news。 php等)

   <?php include 'assets/class/dbcon.php'; ?>

<?php
    function loop_array($array = array(), $parent_id = 0){
        $html = 'http://mywebsite.com/';
            global $connection; 
            $result = mysqli_query($connection,"SELECT href FROM menus");
            $href = Array();
            while ($row = mysqli_fetch_array($result)) {
            $href = $row['href'];

            }
        if(!empty($array[$parent_id])){

            echo '<ul class="cssmenu">';
            foreach($array[$parent_id] as $items){
            echo '<a href="'.$html.$href.'"><li style="z-index: 100;">';
            echo $items['name'];
            loop_array($array, $items['id']);
            echo '</li></a>';
            }
            echo '</ul>';

        }       
    }

    function display_menus() 
    {


    global $connection;
    $query = $connection->query("SELECT * FROM menus");
    $array = array();

    if(mysqli_num_rows($query)){

        while($rows = mysqli_fetch_array($query)){

            $array[$rows['parent_id']][] = $rows;

        }
        loop_array($array);
    }


    }

    mysqli_close($connection); 
?>

这个想法是当我点击一个 li 标签时, 那是加载http://mywebsite.com/variouspages.php,但它现在所做的是当我点击一个 li 标签时,它会将我发送回 mywebsite.com。如果我回显 href 列,它会显示所有 href 链接。所以它说类似 index.phpnews.phpfotos.php

希望有人可以帮助我解决这个问题。谢谢!非常喜欢。

【问题讨论】:

  • 这是一个混乱的 PHP 代码和 HTML。你想做什么?这个怎么不能再分开了?
  • .$html.$href. 这是什么意思?
  • 欢迎来到 StackOverflow!请查看How to Ask 了解有关提出好问题的更多信息。在这种情况下,如果您可以提供 minimal reproducible example 说明您遇到的问题,可能会对您和回答的人有所帮助。

标签: php mysql


【解决方案1】:

谷歌开发者:

   <header>
    <div style="width: 100%; background-color: #383838; border-bottom: 4px solid #cd0000;">
        <div class="menu">
            <ul class="cssmenu"><a href="http://cvdemarskoppen.nl/"><li style="z-index: 100;">Home</li></a><a href="http://cvdemarskoppen.nl/"><li style="z-index: 100;">Nieuws</li></a><a href="http://cvdemarskoppen.nl/"></a><li style="z-index: 100;"><a href="http://cvdemarskoppen.nl/">De Marskoppen</a><ul class="cssmenu"><a href="http://cvdemarskoppen.nl/"></a><a href="http://cvdemarskoppen.nl/"><li style="z-index: 100;">Foto's</li></a><a href="http://cvdemarskoppen.nl/"><li style="z-index: 100;">Agenda</li></a><a href="http://cvdemarskoppen.nl/"><li style="z-index: 100;">Het Bestuur</li></a><a href="http://cvdemarskoppen.nl/"><li style="z-index: 100;">Historie</li></a><a href="http://cvdemarskoppen.nl/"><li style="z-index: 100;">Commissies</li></a><a href="http://cvdemarskoppen.nl/"><li style="z-index: 100;">Lid worden</li></a></ul></li><a href="http://cvdemarskoppen.nl/"></a><li style="z-index: 100;"><a href="http://cvdemarskoppen.nl/">Garde Officieren</a><ul class="cssmenu"><a href="http://cvdemarskoppen.nl/"></a><a href="http://cvdemarskoppen.nl/"><li style="z-index: 100;">Foto's</li></a><a href="http://cvdemarskoppen.nl/"><li style="z-index: 100;">Agenda</li></a><a href="http://cvdemarskoppen.nl/"><li style="z-index: 100;">Leden</li></a><a href="http://cvdemarskoppen.nl/"><li style="z-index: 100;">Historie</li></a></ul></li><a href="http://cvdemarskoppen.nl/"><li style="z-index: 100;">Vorstenhuis</li></a><a href="http://cvdemarskoppen.nl/"><li style="z-index: 100;">Sponsors</li></a></ul>   </div>
    </div>
        <div class="header">
            <div style="display: none; background-color: #fe0000; width: 30%; float: left; height: 199px; position: absolute; margin: 60px 0 0 0; border-top: 4px solid #3f454b; border-bottom: 4px solid #3f454b;"></div>
            <div style="width: 100%; float: left;">
            <div style="display: none; background-image: url('css/images/header/logo.png'); width: 209px; height: 208px; position: absolute; z-index: 4; float: right; margin: -4 0 0 180;"></div>
            </div>
            <div id="cycler" style="overflow:hidden;">
                <img class="active" src="css/images/header/1.jpg" alt="Plaats hier een foto." style="width: 100%; height: 150%; margin-top: -10%; display: block; z-index: 3;">
                <img src="css/images/header/2.jpg" alt="Plaats hier een foto." style="width: 100%; height: 150%; margin-top: -10%; z-index: 1; display: block;" class="">   
                <img src="css/images/header/3.jpg" alt="Plaats hier een foto." style="width: 100%; height: 150%; margin-top: -10%; z-index: 1; display: block;" class="">           
            </div>
        </div>
        <div class="clear"></div>
        <script>
    function cycleImages(){
          var $active = $('#cycler .active');
          var $next = ($active.next().length > 0) ? $active.next() : $('#cycler img:first');
          $next.css('z-index',2);//move the next image up the pile
          $active.fadeOut(1500,function(){//fade out the top image
          $active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
              $next.css('z-index',3).addClass('active');//make the next image the top one
          });
        }

    $(document).ready(function(){
    // run every 7s
    setInterval('cycleImages()', 7000);
    })
        </script>

    </header>

我为菜单编写的 HTML。

<header>
<div style="width: 100%; background-color: #383838; border-bottom: 4px solid #cd0000;">
    <div class="menu">
        <?php display_menus(); ?>
    </div>
</div>

感谢您的帮助。请记住,这对我来说是反复试验。 php新手,所以我还在学习一切。

【讨论】:

    【解决方案2】:

    您的链接组装不正确,因为您没有循环遍历 href 数组的值以附加到 html 变量。

    您只是循环通过$array[$parent_id],然后尝试将$href 数组(不是它的值)附加到$html 变量之后。

    这实质上是给你http://mywebsite.com/array 或者只是在DOM 中的http://mywebsite.com/

    你需要为$html数组添加一个循环,如下:

    $html = 'http://mywebsite.com/';
    
    $href = array('a', 'b', 'c'); //this comes from your db as per your question
    
    $link = '';
    $link .= '<ul>';
    
    foreach($href as $val){
        $link .= '<a href="' . $html . $val . '"><li style="z-index: 100;"></li></a>';
    }
    
    $link .= '</ul>';
    echo $link;
    

    这将在 DOM 中为您提供以下内容:

    <ul>
     <a href="http://mywebsite.com/a"><li style="z-index: 100;"></li></a>
     <a href="http://mywebsite.com/b"><li style="z-index: 100;"></li></a>
     <a href="http://mywebsite.com/c"><li style="z-index: 100;"></li></a>
    </ul>
    

    【讨论】:

    • 我明白你的意思了。但我不能真正放置代码的位置。而 a b c 你的意思是 .php 文件?或列。这里是数据库的图像gyazo.com/dfd8b2cda8cbd1b8f02ba22c985a9056
    • 我在示例代码中使用了“a,b, & c”,但这些是来自您的数据库的 .php 文件,用于添加到$html。您需要重构代码以获得 href 值。有几种方法可以做到这一点,您甚至可以在第一个 foreach 循环中使用array_pop() 来每次获取href 的最后一个值,并将其附加到$html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 2017-11-11
    • 2017-05-21
    相关资源
    最近更新 更多