简体   繁体   English

带有php,js和mysql的导航菜单

[英]Navigation menu with php, js and mysql

Note: original & new inside the code. 注意: 原始的代码。

My issue here is that the original "parent" is not very SEO friendly and does not act according to plan, it also does not get fetched up in sitemaps. 我的问题是, 原始的 “父母”对SEO不太友好,不会按计划行事,也不会在站点地图中获取。

However the new is SEO friendly, will get picked up in sitemaps and will be according to plan in the end. 但是, 新版本对 SEO友好,将在站点地图中显示,并最终按计划进行。

I need it to be a link so that you can see both the navigation menu to the left with the name of each menu / sub-menu and in the center of the page have a picture with the name for each sub-menu depending on which parent you click in the navigation menu on the left. 我需要它作为一个链接,以便您可以看到左侧的导航菜单以及每个菜单/子菜单的名称,并且在页面的中央有一张图片,每个子菜单的名称取决于其中的哪个父级,请在左侧的导航菜单中单击。

With the new line added i get a page refresh due to the link, so the sub-menus only flash open for a second before the refresh. 添加行后,由于链接的缘故,我得到了页面刷新,因此子菜单仅刷新一秒钟才刷新。

What i need help solving is: 我需要帮助解决的是:

-Making the sub-menu stay open during page refresh so you can use both the navigation from the sidebar navigation and the center navigation. -在页面刷新期间使子菜单保持打开状态,以便您可以同时使用侧边栏导航和中心导航。

Thanks in beforehand! 预先感谢!
//Jim //吉姆

This is my current menu: 这是我当前的菜单:

<div id="container">
    <dl>
    <?php 
        $select_category = mysql_query("SELECT * FROM menu WHERE hidden = 0 ORDER BY menu ASC");
            while($ln = mysql_fetch_array($select_category)){
                $idcat = $ln['nmr'];
                $catname = str_replace(' ', '-', $ln['menu']);
    ?>

            *Original*: <dt><a href="#/<?php echo $catname; ?>/" style="color:#000;" ><strong><?php echo $ln['menu']; ?></strong></a></dt>

        *New*: <dt><a href="http://www.mysite.com/cats/<?php echo $msub['nmr'];?>/<?php echo $mname; ?>/" style="color:#000;" ><strong><?php echo $mn['menu']; ?></strong></a></dt>

        <?php 
            $select_sub = mysql_query("SELECT * FROM submenu WHERE nmrmenu = '$idcat' AND hidden = 0");
            if(mysql_num_rows($select_sub) == 0){
            }else {
        ?>
        <dd>
        <ul>
        <?php
            while($lsub = mysql_fetch_array($select_sub)){
                $subname = str_replace(' ', '-', $lsub['submenu']);
                $pil = '&raquo;';
                $brnr = $lsub['nmr'];
        ?> 
        <li> <a href="http://www.mysite.com/cat/<?php echo $lsub['nmr'];?>/<?php echo $subname; ?>/" style="color:#333;"> <?php echo $pil; ?>&nbsp;<?php echo $lsub['submenu']; ?></a></li>
        <?php } ?>
        </ul>
        </dd>
       <?php } ?>
      </dl>
     <?php } ?>
   </div>

CSS: CSS:

#container{ margin:auto; margin-left:10px; }
dl, dt, dd, ul, li, a{ margin:0; padding:0; }
a{ text-decoration: none; color:#0F0; }
li{ padding-left:.6em; list-style-type:none; color:#FF0; }
dl{ width:100px; }
dt{  }

JS: JS:

$(function()
{
    $("dd:not(first)").hide();
    $("dt a").click(function()
     {
         $("dd").slideUp("normal");
         $(this).parent("dt").next("dd").slideDown("normal");
     });
});

Here's a jsFiddle of my code. 这是我的代码的jsFiddle

First you'll want to clean up your html and css. 首先,您需要清理HTML和CSS。 Then you're going to want to check which page the user is viewing and if it's a category, show that specific sub navigation. 然后,您将要检查用户正在查看的页面,如果是类别,则显示该特定子导航。

Here's your cleaned up php/html. 这是您清理过的php / html。 I think you've messed up your variable names, so if this doesn't work, that's probably why. 我认为您已经弄乱了变量名,所以如果这不起作用,那可能就是原因。

Example, you have *New*: <dt><a href="http://www.mysite.com/cats/<?php echo $msub['nmr'];?> but $msub['nmr']; is not set in the code above where it is used. 例如,您有*New*: <dt><a href="http://www.mysite.com/cats/<?php echo $msub['nmr'];?>$msub['nmr'];在上面使用它的代码中未设置$msub['nmr'];

<ul id="nav-container">
<?php 
$select_category = mysql_query("SELECT * FROM menu WHERE hidden = 0 ORDER BY menu ASC");
while ($ln = mysql_fetch_array($select_category)) {
  $idcat = $ln['nmr'];
  $catname = str_replace(' ', '-', $ln['menu']);

  echo '<li>'
  echo '<a href="http://www.mysite.com/cats/'.$msub['nmr'].'/'.$mname.'/" ><strong>'.$mn['menu'].'</strong></a>';
  echo '<ul';

  // This is where we check which page you're on, and show the sub navigation if it corresponds to that page.
  if ($GET['cat'] == $msub['nmr'])
    echo ' style="display:block;">';
  else
    echo '>';


  while ($lsub = mysql_fetch_array($select_sub)) {
    $subname = str_replace(' ', '-', $lsub['submenu']);
    $pil = '&raquo;';
    $brnr = $lsub['nmr'];

    echo '<li><a href="http://www.mysite.com/cat/'.$lsub['nmr'].'/'.$subname.'/">'.$pil.'&nbsp;'.$lsub['submenu'].'</a></li>';
  }

  echo '</ul>';
  echo '</li>';
}

?>

</ul>

Here's the CSS to go with it. 这是附带的CSS。

a {
    text-decoration: none;
    color: #333;
}
li {
    padding-left: .6em;
    list-style-type:none;
    color:#FF0;
}

#nav-container li ul {
    display: none; /* Hide all sub navigations to start with */
}

Because of the way I used css, you no longer need to use JavaScript :) 由于我使用CSS的方式,您不再需要使用JavaScript :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM