如何在php和mysql

时间:2018-03-15 08:16:56

标签: php

<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav navbar-right">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class='dropdown'>
<a href="category.php" class='dropdown-toggle' data-toggle='dropdown'> 
<span class='label label-pill count'></span>
<i class="glyphicon glyphicon-briefcase"></i>&nbsp;Category</a>
<ul class='dropdown-menu' id="category">
<?php echo display_category($link);?>
</ul>
</li>
<li>
<a href="contact.php">
<i class="glyphicon glyphicon-user">
</i>&nbsp;Contact Us</a>
</li></ul></div> 
<?php
function display_category($link)  
 { 
$output = '';
$query = "SELECT * FROM category"; 
$res = mysqli_query($link, $query);
while($row = mysqli_fetch_array($res))
{ 
$output .= '<option value="'.$row["c_id"].'">'.$row["c_name"].'</option>';  
  }      
return $output;
}
?>

我在php中创建了一个菜单,其中类别是其中一个选项,但我希望当我点击类别相关的产品时,应该从数据库中打开。如果有人有想法请分享。

1 个答案:

答案 0 :(得分:0)

你可以试试这个。不需要做ajax。

common.scss

在你的css中,默认情况下隐藏内部ul并且在外部ul上悬停&gt; li>只是展示内心的ul。通过这种方式,您将能够显示该类别的相关产品。

修改

在html中使用

<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav navbar-right">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class='dropdown'>
<a href="category.php" class='dropdown-toggle' data-toggle='dropdown'> 
<span class='label label-pill count'></span>
<i class="glyphicon glyphicon-briefcase"></i>&nbsp;Category</a>
<ul class='dropdown-menu' id="category">
<?php
$query = "SELECT * FROM category"; 
$res = mysqli_query($link, $query);
while($row = mysqli_fetch_array($res))
{ 
?>
<li><a href="#"><?php echo $row["c_name"] ?></a>
<ul>
<?php
$query1 = "SELECT * FROM product where category_id =".$row['cat_id']; 
$res1 = mysqli_query($link, $query1);
while($row1 = mysqli_fetch_array($res1))
{ 
?>
<li><a href="#"><?php echo $row1['product_name']; ?></a></li>
<?php
}
?>
</ul>
</li>';  
<?php 
} 
?>     
</ul>
</li>
<li>
<a href="contact.php">
<i class="glyphicon glyphicon-user">
</i>&nbsp;Contact Us</a>
</li></ul></div> 

在Css中

<ul id="nav">
  <?php
    $query = "SELECT * FROM category"; 
    $res = mysqli_query($link, $query);
    while($row = mysqli_fetch_array($res))
    { 
    ?>
    <li><a href="#"><?php echo $row["c_name"] ?></a>
    <ul>
    <?php
    $query1 = "SELECT * FROM product where category_id =".$row['cat_id']; 
    $res1 = mysqli_query($link, $query1);
    while($row1 = mysqli_fetch_array($res1))
    { 
    ?>
    <li><a href="#"><?php echo $row1['product_name']; ?></a></li>
    <?php
    }
    ?>
    </ul>
    </li>';  
    <?php 
    } 
    ?>     
    <li>
    <a href="contact.php">
    <i class="glyphicon glyphicon-user">
    </i>&nbsp;Contact Us</a>
    </li>
</ul>

这是一个你可以参考的JSfiddle。

https://jsfiddle.net/SmitRaval/5t0v5nq5/

希望这有帮助。