下拉菜单 PHP

Drop-Down Menu PHP

我有这个 html 代码用于带有子菜单的菜单:

<li class="has-submenu">
                                <a href="#">Services</a>
                                <div class="mainmenu-submenu">
                                    <div class="mainmenu-submenu-inner"> 
                                        <div>
                                            <h4>Home</h4>
                                            <ul>
                                                <li><a href="#">Plumbers</a></li>
                                                <li><a href="#">Painters</a></li>
                                            </ul>
                                            <h4>People</h4>
                                            <ul>
                                                <li><a href="#">Babysitter</a></li>
                                                <li><a href="#">Trainer</a></li>

                                            </ul>
                                            <h4>School</h4>
                                            <ul>
                                                <li><a href="#">Teacher</a></li>

                                            </ul>
                                        </div>
                                        <div>
                                            <h4>Machine</h4>
                                            <ul>
                                                <li><a href="#">Mecanic</a></li>
                                            </ul>
                                            </div>
                                </div>
                            </div>
                        </li>

然后我把这个 table 叫做 "services":

id  |    name   | service   | description    

  1       Mario    Plumber         aaa        
  2       Luigi    Plumber         aaa      
  3       Link     Painter         aaa      
  4       Zelda    Babysitter      aaa      
  5       Sonic    Trainer         aaa      
  6       Marth    Teacher         aaa      
  7        Ike     Trainer         aaa      
  8     Little Mac Mecanic         aaa 

我想创建一个代码来显示仅与该子菜单关联的结果。例如,如果我在子菜单中按管道工,我希望它只显示我的 table 中的管道工。我使用的 PHP 代码是:

$query = "SELECT * FROM services WHERE service LIKE service";
                    $result = mysql_query($query) or die(mysql_error());
                    $casaArray = array();

但我只能让它显示所有服务。我是 PHP.

的新手

感谢您的帮助。

拳头添加jquery个文件(学习link)

      $con = new mysqli("YourHost", "Username", "Password", "Database"); /* REPLACE NECESSARY DATA */

      if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
      }
      if($stmt = $con->prepare("SELECT service_id, service FROM main_service")){
        $stmt->execute();
        $stmt->bind_result($serviceid,$service);
        while($stmt->fetch()){
      ?>

  <li class="ui-state-disabled">
      <a href="information.php?id=<?php echo $serviceid; ?>"><?php echo $service; ?></a>
      <---------------here set sub menu sql and take the result if submenu not empty---->
      <ul>
      <li class="ui-state-disabled">Ada</li>
      <li>Saarland</li>
      <li>Salzburg an der schönen Donau</li>
    </ul>
    <---------------here close the submenu loop---->
  </li>
  <---------------here close the menu loop---->
</ul>

我认为你可以解决这个问题(jQuery 菜单)..通过这个 url 并学习 https://jqueryui.com/menu/

您只需在菜单中创建 link:

<a href="information.php?action=Plumber">Plumbers</a>

那么,为了你的information.php。 (我用过mysql_real_escape_string() function to prevent SQL injections

if(!empty($_GET["action"])){

  $action = mysql_real_escape_string($_GET["action"]);

  $query = "SELECT * FROM services WHERE service='$action'";
  $result = mysql_query($query);
  while($row = mysql_fetch_array($result)){
    echo $row["name"]." - ".$row["description"]."<br>";
  } /* END OF WHILE LOOP */

} /* END OF IF NOT EMPTY ACTION */

注:

  • 您必须拥有结构化数据库、架构等才能实现您想要的。您可以更新您的 post 以便 SO 中的人可以帮助您。
  • 据我所见,使用您给定的代码,用户将单击他们喜欢的 link,他们将被重定向到包含信息的页面,以列表或某种形式。
  • 如果您还没有连接到数据库的代码,我假设您想要使用 PHP and mysql because of the tag you used in your post. Unfortunately, mysql is already deprecated and we recommend that you use mysqli prepared statement

首先,您有这个菜单,其中 links 会将他们重定向到另一个页面。再次遗憾,您只给了我们一个 table,其中包含您希望在下一页显示的信息。

首先,让我们创建一个 table,我们称之为 main_services,它将有两列:service_id(自动递增和您的主键)和 service.

service_id  |  service
    1       |  Plumber
    2       |  Painter
    3       |  Babysitter
    4       |  Trainer
    5       |  Teacher
    6       |  Mechanic
and so on...

然后,让我们改变您的 services table 的 table 结构。您的服务 table 仍将有四列:idservice_idnamedescription。我们会将 main_services.service_id 索引到您的 services.serviceid

id  |   service_id |    name    |  description    

  1         1        Mario           aaa        
  2         1        Luigi           aaa      
  3         2        Link            aaa      
  4         3        Zelda           aaa      
  5         4        Sonic           aaa      
  6         5        Marth           aaa      
  7         4        Ike             aaa      
  8         6        Little Mac      aaa 

在我们建立您的 table 之后,我们将在您的菜单上创建 link。

<div>
  <h4>Options</h4>
  <ul>
    <?php

      $con = new mysqli("YourHost", "Username", "Password", "Database"); /* REPLACE NECESSARY DATA */

      if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
      }

      if($stmt = $con->prepare("SELECT service_id, service FROM main_service")){
        $stmt->execute();
        $stmt->bind_result($serviceid,$service);
        while($stmt->fetch()){
          ?>
            <li>
              <a href="information.php?id=<?php echo $serviceid; ?>"><?php echo $service; ?></a>
            </li>
          <?php
        }
        $stmt->close();

      }
    ?>
  </ul>
</div>

然后让我们创建您的 information.php 文件:

<?php

  /* LET US FIRST ESTABLISH YOUR CONNECTION TO YOUR DATABASE */
  $con = new mysqli("YourHost", "Username", "Password", "Database"); /* REPLACE NECESSARY DATA */

  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
  }

  if(!empty($_GET["id"])){

    if($stmt = $con->prepare("SELECT name, description FROM services WHERE service_id = ?")){
      ?>
        <table>
          <tr>
            <th>Name</th>
            <th>Description</th>
          </tr>
      <?php

      $stmt->bind_param("i",$_GET["id"]);
      $stmt->execute();
      $stmt->bind_result($name,$description);
      while($stmt->fetch()){
        ?>
          <tr>
            <td><?php echo $name; ?></td>
            <td><?php echo $description; ?></td>
          </tr>
        <?php
      } /* END OF WHILE LOOP */

      ?>
        </table>
      <?php
      $stmt->close();
    } /* END OF PREPARED STATEMENT */

  } /* END OF IF NOT EMPTY ID */

?>