MySQL DB驱动的菜单生成器功能 [英] MySQL DB driven menu generator function

查看:35
本文介绍了MySQL DB驱动的菜单生成器功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我编写了递归PHP函数,该函数基于像这样的父子结构生成网站导航

Recently I've written recursive PHP function which generates website navigation based on parent-child structure like this

<ul>
  <li>parent
     <li>child</li>
  </li>
</ul>

代码看起来像这样

function generateMenu($parent, $level, $db){
  $q = $db->query("select id, name FROM menu WHERE parent = '$parent'");

  if($level > 0 && $q->num_rows > 0) echo "\n<ul>\n";

   while($row=$q->fetch_object()){
      echo "<li>";
      echo '<a href="?page=' .$page. '">' . $row->name . '</a>';
      //display this level's children
      generateMenu($row->id, $level++, $menu, $db);
      echo "</li>\n\n";
    }

    if($level > 0 &&  $q->num_rows > 0) echo "</ul>\n";
}

上面的代码段仅从db表中回显< ul>< li> 结构(如上面的示例所示).

The piece of code above simply echoes <ul><li> structure (like given above example) from db table.

现在的问题是,如何在此网站上创建导航菜单?

请查看左侧边栏.

http://www.smithsdetection.com/continuous_vapour_sampling.php

现在我认为:

  1. 首先我们需要回应所有父母
  2. 函数必须获取当前页面ID作为输入值(例如,$ current)
  3. 函数必须回显直到当前页面级别

我不知道如何修改我的功能,以获取给定网站上的输出.请帮助.

I can't figure out how to modify my function, to get output like on given website. PLease help.

顺便说一句

我的数据库表是这样的

注意请不要发布有关sql注入孔的答案,我已经在意这些问题:使用in_array(如果列名数组中列出了变量)检查并通过real_escape.

NOTE Please don't post answers about sql injection holes, I've already taken care about them: checking with in_array (if variable listed in column names array) and passing through real_escape.

推荐答案

假定当前页面ID在var $ current 中,并且$ db是一个开放的MySQLi DB连接:

Assuming the current page id is in the var $current and that $db is an open MySQLi DB connection:

// first get your current page's path back to root:
// $stack will be a stack of menus to show
$stack = array();

// always expand current menu:
$stack[] = $current;

// now starting at $current, we go through the `menu` table adding each parent
// menu id to the $stack until we get to 0:
$i = $current;
while ( $i > 0 ) {
  // get parent of $i
  $query = sprintf('SELECT `parent` FROM `menu` WHERE id=%d LIMIT 1', $i);
  $result = $db->query($query);

  if (!$result) {
    // do error handling here
  }

  $row = $result->fetch_assoc();

  // save parent id into $i...
  $i = $row['parent'];

  // ...and push it onto $stack:
  $stack[] = $i;
}

/**
 * @param int $parent the parent ID of the menu to draw.
 * @param array $stack the stack of ids that need to be expanded
 * @param string $indent string for pretty-printing html
 * @param MySQLi $db Open db connection
 */
function generateMenu($parent, $stack, $indent, $db){

  // $next is the next menu id that needs expanding
  $next = array_pop($stack);

  $query = sprintf('SELECT `id`, `name` FROM `menu` WHERE `parent`=%d', $parent);

  $result = $db->query($query);

  if ( ! $result ) {
    // do error handling here
  }

  if ($result->num_rows > 0) {
    echo "\n$indent<ul>\n";

    while($row = $result->fetch_object()){
      echo "$indent  <li>\n";
      echo "$indent    <a href=\"?page={$row->id}\">{$row->name}</a>\n";

      //display this level's children, if it's the $next menu to need to be drawn:
      if ($row->id == $next)
        generateMenu($next, $stack, "$indent    ", $db);

      echo "$indent  </li>\n\n";
    }
    echo "$indent</ul>\n";
  }
  $result->free();
}

$first = array_pop($stack); // should always be 0
generateMenu($first, $stack, '', $db);

这篇关于MySQL DB驱动的菜单生成器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆