简体   繁体   English

下拉菜单 PHP

[英]Drop-Down Menu PHP

I have this html code for a menu with sub-menu:我有一个带有子菜单的菜单的 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>

Then I have this table called "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 

I want to create a code that shows me the results only associated with that sub-menu.我想创建一个代码,向我显示仅与该子菜单相关联的结果。 For example, if I press Plumbers in the sub-menu, I want it to only show me the plumbers from my table.例如,如果我在子菜单中按管道工,我希望它只显示我桌子上的管道工。 The PHP code I use is:我使用的PHP代码是:

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

But I can only get it to show me all the services.但我只能让它向我展示所有服务。 I'm new to PHP.我是 PHP 新手。

Thanks for your help.谢谢你的帮助。

You can simply create a link in your menu:您可以简单地在菜单中创建一个链接:

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

Then, for your information.php.然后,为您的info.php。 (I've used mysql_real_escape_string() function to prevent SQL injections ) (我使用mysql_real_escape_string()函数来防止SQL 注入

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 */

Note:笔记:

  • You must have a structured database, a schema that you have, etc. in order to achieve what you want.你必须有一个结构化的数据库、一个你拥有的模式等,才能实现你想要的。 You can update your post in order for people here in SO to help you.您可以更新您的帖子,以便 SO 中的人为您提供帮助。
  • As far as I can see with your given code, user will click a link that they prefer, and they will be redirected to a page that contains information, in a form of list or some sort.就我所提供的代码所见,用户将单击他们喜欢的链接,然后他们将被重定向到包含信息的页面,以列表或某种形式。
  • If you don't have yet a code that would connect to your database, I assume that you want to use PHP and mysql because of the tag you used in your post.如果您还没有连接到数据库的代码,我假设您想使用PHPmysql,因为您在帖子中使用了标签。 Unfortunately, mysql is already deprecated and we recommend that you use mysqli prepared statement .不幸的是,mysql 已被弃用,我们建议您使用mysqli 准备好的语句

To start, you have this menu, with links that would redirect them to another page.首先,您有这个菜单,带有将它们重定向到另一个页面的链接。 Unfortunately, again, you only gave us one table that contains the information you want to show on the next page.不幸的是,您只给了我们一张表格,其中包含您要在下一页显示的信息。

First, let us create a table, which we will call main_services and it will have two column: service_id (auto-increment and your primary key) and service .首先,让我们创建一个表,我们将其命名为main_services ,它将有两列: service_id (自动增量和您的主键)和service

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

Then, let us alter the table structure of your services table.然后,让我们更改您的services表的表结构。 Your services table will still have four columns: id , service_id , name and description .您的 services 表仍然有四列: idservice_idnamedescription We will index the main_services.service_id to your services.serviceid我们会将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 

After we have established your tables, we will create the links on your menu.在我们建立您的表格后,我们将在您的菜单上创建链接。

<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>

Then let us create your information.php file:然后让我们创建您的 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 */

?>

Fist add jquery files (learn link)拳头添加jquery文件(学习链接)

      $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>

I think you can solve this (jQuery menu).. go through this url and learn https://jqueryui.com/menu/我想你可以解决这个问题(jQuery 菜单)。通过这个网址学习https://jqueryui.com/menu/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM