数据放入下拉列表

时间:2017-03-26 17:04:55

标签: php html css mysql

我想从MySQL获取数据并将其插入我的Navbar中。我的问题是,有些数据必须放在下拉列表中,但它会创建一个新的主要列表项。

以下是屏幕截图现在的样子,但我希望在下拉菜单中插入Panel列。

enter image description here

    <?php 
     $query  =   $connect->prepare("SELECT * FROM `ucp_navbar` WHERE `Kategorie` = 0");
     $query->execute();
    ?>

<div class='collapse navbar-collapse' id='bs-example-navbar-collapse-1'>
<ul class='nav navbar-nav'>
    <?php while ($row = $query->fetch()) { echo $row["Panel"]; if($row["Panel"] == 0) { ?>
        <li <?php if($page_a == $row["Active"]) echo "class='active nav-active'"; ?>>
            <a href='<?php echo htmlentities($row["Link"]); ?>'><i class='fa fa-<?php echo htmlentities($row["Icon"]); ?>' aria-hidden='true'></i> <?php echo htmlentities($row["Name"]); ?></a>
        </li><?php } ?>

        <?php if($row["Panel"] != 0 && $row["Panel"] == 1) { ?>
        <li class='dropdown'>
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><i class='fa fa-user' aria-hidden='true'></i> Account<span class="caret"></span></a>
            <ul class="dropdown-menu">
                <li>
                    <a href='<?php echo htmlentities($row["Link"]); ?>'><i class='fa fa-<?php echo htmlentities($row["Icon"]); ?>' aria-hidden='true'></i> <?php echo htmlentities($row["Name"]); ?></a>
                </li>
            </ul>        
        </li>
    <?php } } ?>
</ul>

我的数据库结构:

Database_structure

1 个答案:

答案 0 :(得分:0)

我认为您遇到的问题是您将子菜单视为主导航栏项目。下面是一个可能的解决方案,我已经做了一些重型格式重构,试图将逻辑部分放到顶部,以便解决方案更清晰。我倾向于同意早期的评论者(@Scott)的观点,即创建表的父子关系将是长期更易于维护的实践。

<?php 
// Make query
$query = $connect->prepare("SELECT * FROM `ucp_navbar` WHERE `Kategorie` = 0");
$query->execute();

// Setup containers for list items
$nav = array();
$dropdown = array();

// Iterate query
while ($row = $query->fetch()) {
  // Assign built list item to either navbar or dropdown arrays
  switch ($row['Panel']) {
    case 0:
      $nav[] = buildListItem(
        htmlentities($row['Link']),
        htmlentities($row['Icon']),
        echo htmlentities($row["Name"]),
        true
      );
      break;
    case 1:
      $dropdown[] = buildListItem(
        htmlentities($row['Link']),
        htmlentities($row['Icon']),
        echo htmlentities($row["Name"])
      ); 
  }  
}

// Add dropdowns to main nav and convert array of nav items to string.
$nav[] = buildDropdown($dropdowns);
$navbar = implode("", $nav);

/**
 * Builds an individual list item.
 *
 * @param string $link URL to use for href
 * @param string $icon font-awesome icon to use
 * @param string $name display name for navbar
 * @param bool   $active adds nav-active class when true
 *
 * @return string formatted single list item
 */
function buildListItem($link, $icon, $name, $active = false)
{
  $class = ($active) ? ' class="active nav-active"': ""
  return "<li{$class}><a href=\"{$link}\"><i class=\"fa fa-{$icon}\" aria-hidden=\"true\"></i>{$name}</a></li>";
}

/**
 * Construct dropdown list item.
 *
 * @param array $dropdowns group of dropdown list items 
 *
 * @return string formatted dropdown list for navbar
 */
function buildDropdown($dropdowns)
{
  $list = implode("", $dropdowns);
  return <<<EOT 
  <li class='dropdown'>
      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
        <i class='fa fa-user' aria-hidden='true'></i> 
        Account<span class="caret"></span>
      </a>
      <ul class="dropdown-menu">
          $list
      </ul>        
  </li>
EOT;
}
?>

<div class='collapse navbar-collapse' id='bs-example-navbar-collapse-1'>
<ul class='nav navbar-nav'>
   <?php echo $navbar; ?>
</ul>