构建动态巨型菜单

cngwdvgl  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(460)

<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 在中通过数据库动态可见。

k10s72fa

k10s72fa1#

我认为您在遍历时遇到了一个错误 $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;
?>

相关问题