建立动态超级菜单

时间:2018-06-25 07:26:53

标签: php mysql mysqli menu

Mega Menu

<div class="menu-wrapper" role="navigation">
  <ul class="nav" role="menubar">
    <li role="menuitem">
      <a href="#">Menu Item 1</a>
      <div class="mega-menu" aria-hidden="true" role="menu">
        <?php foreach($kategoriler as $kategori){ ?>
          <div class="nav-column">
            <h3><?php echo $kategori["kategori_adi"]; ?></h3>

            <?php
              $kategori_id = $kategori["id"];
              $urunler = $db->query("SELECT * FROM urun WHERE kategori_id=$kategori_id", PDO::FETCH_ASSOC);
            ?>
            <ul>
              <?php foreach($urunler as $urun){ ?>
                <li role="menuitem"><a href="<?php echo $urun["url"]; ?>"><?php echo $urun["brand"]; ?></a></li>
              <?php } ?>
            </ul>
          </div>
        <?php } ?>
      </div>
    </li>

  <li role="menuitem"><a href="#">Menu Item 2</a></li>
  <li role="menuitem"><a href="#">Menu Item 3</a></li>
  <li role="menuitem"><a href="#">Menu Item 4</a></li>
  <li role="menuitem"><a href="#">Menu Item 5</a></li>
  <li role="menuitem"><a href="#">Menu Item 6</a></li>

    <li role="menuitem" class="nav-search">
      <form action="#">
        <label for="search"></label>
        <input type="text" id="search" title="Search..." placeholder="Search...">
        <input type="submit" value="">
      </form>
    </li>
  </ul>
</div>
--
-- Database: `megamenu`
--

--
-- Table structure for table `kategori`
--

CREATE TABLE `kategori` (
  `id` int(11) NOT NULL,
  `kategori_adi` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `kategori`
--

INSERT INTO `kategori` (`id`, `kategori_adi`) VALUES
(1, 'category 1'),
(2, 'category 2'),
(3, 'category 3'),
(4, 'category 4');

--
-- Table structure for table `urun`
--

CREATE TABLE `urun` (
  `id` int(11) NOT NULL,
  `brand` varchar(255) DEFAULT NULL,
  `kategori_id` int(11) DEFAULT NULL,
  `url` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `urun`
--

INSERT INTO `urun` (`id`, `brand`, `kategori_id`, `url`) VALUES
(1, 'product1', 1, '#'),
(2, 'product2', 1, '#'),
(3, 'Klavye', 1, '#'),
(4, 'Sifonyer', 2, '#'),
(5, 'TV Sehpahasi', 2, '#'),
(6, 'Yemek Masasi', 2, '#'),
(7, 'Hortum', 3, '#'),
(8, 'Cardak', 3, '#'),
(9, 'Sandalye', 3, '#'),
(10, 'Masa', 3, '#'),
(11, 'Teyp', 4, 'http://www.kablosuzkedi.com');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `kategori`
--
ALTER TABLE `kategori`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `urun`
--
ALTER TABLE `urun`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `kategori`
--
ALTER TABLE `kategori`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT for table `urun`
--
ALTER TABLE `urun`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;COMMIT;

问题:此处仅可见一个子类别。我希望通过数据库动态显示菜单item1,菜单item 2,菜单item 3

1 个答案:

答案 0 :(得分:0)

我认为您在遍历$urunler为空时遇到错误。请改用以下几行:

<?php
$kategori_id = $kategori["id"];
$urunler = $db->query("SELECT * FROM urun WHERE kategori_id=$kategori_id", PDO::FETCH_ASSOC);

if (!empty($urunler):
?>
<ul>
    <?php 
    foreach($urunler as $urun): 
    ?>
    <li role="menuitem"><a href="<?php echo $urun["url"]; ?>"><?php echo $urun["brand"]; ?></a></li>
    <?php
    endforeach;
    ?>
</ul>
<?php
endif;
?>