【问题标题】:How to show the subcategories which belong to a category?如何显示属于某个类别的子类别?
【发布时间】:2014-10-08 09:16:27
【问题描述】:

此代码适用于类别和子类别。在类别中,我在下拉列表中显示所有值,但对于子类别,我想显示属于某个类别的确切子类别。请帮助我

<div class="form-group">
    <label for="exampleInputEmail1">Category Name</label>
    <select name="type" id="type">
        <option>Select Category</option>  
        <?php 
            $res= mysql_query("select * from ".CATEGORY."  order by id asc");
            while($data=mysql_fetch_array($res))
            {
        ?>
        <option value="<?php echo $data['id'];?>"><?php echo $data['cat_name'];?></option>
        <?php } ?>
    </select>
</div>
<div class="form-group">
    <label for="exampleInputEmail1">Subcategory Name</label>
    <select name="type1" id="type1">
        <option>Select Category</option>
        <?php $res= mysql_query("select * from ".SUBCATEGORY." order by id asc"); 
        while($data=mysql_fetch_array($res))
        {
        ?>
        <option value="<?php echo $data['id'];?>"><?php echo $data['sub_name'];?></option>
        <?php } ?>
    </select>
</div>                               

【问题讨论】:

  • 使用 Ajax 获取子类别

标签: php


【解决方案1】:

在其中使用 jquery 选择类别时,它将获取值并从 ajax 值发布到您的 ajax.php 文件

<div class="form-group">
    <label for="exampleInputEmail1">Category Name</label>
    <select name="type" id="type">
        <option>Select Category</option>  
        <?php 
            $res= mysql_query("select * from ".CATEGORY."  order by id asc");
            while($data=mysql_fetch_array($res))
            {
        ?>
        <option value="<?php echo $data['id'];?>"><?php echo $data['cat_name'];?></option>
        <?php } ?>
    </select>
</div>
<div class="form-group">
    <label for="exampleInputEmail1">Subcategory Name</label>
    <select name="type1" id="type1">


    </select>
</div> 


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){

    $('#type').on("change",function () {
        var categoryId = $(this).find('option:selected').val();
        $.ajax({
            url: "ajax.php",
            type: "POST",
            data: "categoryId="+categoryId,
            success: function (response) {
                console.log(response);
                $("#type1").html(response);
            },
        });
    }); 

});

</script>

在同一目录下创建一个 php 文件名 ajax.php

并把这段代码

<?php 
$categoryId = $_POST['categoryId'];
echo "<option>Select Category</option>";
$res= mysql_query("select * from ".SUBCATEGORY." WHERE category_id = $categoryId order by id asc"); 
        while($data=mysql_fetch_array($res))
        {
        echo "<option value='".$data['id']."'>."$data['sub_name']."</option>";
        }
?>

这会起作用

【讨论】:

  • 非常感谢。它确实有效,对我有很大帮助
【解决方案2】:

您的查询错误,请从表名中删除 quete

$res= mysql_query("select * from ".CATEGORY."  order by id asc");
 $res= mysql_query("select * from ".SUBCATEGORY." order by id asc");

喜欢这个

$res= mysql_query("select * from CATEGORY  order by id asc");
 $res= mysql_query("select * from SUBCATEGORY order by id asc");

在我看来,您可能必须在第二个查询中添加 where 子句。

【讨论】:

    【解决方案3】:

    您需要在CategoryClick上执行AJAX请求或使用属于类别的子类别数组生成java代码并在js中在它们之间切换。 https://stackoverflow.com/a/11238038/3711660 - 关于 AJAX 请求

    【讨论】:

      【解决方案4】:

      试试这个可能对你有帮助...

      <?php
      if($_GET['id'])
      {
          $id=$_GET['id'];
      }
      ?>                          
      <script>
      function newDoc(str) 
      {
          window.location.assign("your_page_name.php?id="+str)
      }
      </script>
      
      <div class="form-group">
        <label for="exampleInputEmail1">Category Name</label>
        <select name="type" id="type" onchange="newDoc(this.value)">
             <option>Select Category</option>  
      
              <?php 
              $res= mysql_query("select * from ".CATEGORY."  order by id asc");
      
              while($data=mysql_fetch_array($res))
              {
              ?>
                  <option value="<?php echo $data['id'];?>"><?php echo $data['cat_name'];?></option>
              <?php 
              }
              ?>
      
          </select>
      </div>
      <div class="form-group">
          <label for="exampleInputEmail1">Subcategory Name</label>
          <select name="type1" id="type1">
              <option>Select Category</option>
               <?php 
               $res= mysql_query("select * from ".SUBCATEGORY." where SUB_ID='$id' order by id asc"); 
               while($data=mysql_fetch_array($res))
               {
               ?>
               <option value="<?php echo $data['id'];?>"><?php echo $data['sub_name'];?></option>
               <?php 
               } 
               ?>
          </select>
      </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-04
        • 1970-01-01
        • 1970-01-01
        • 2012-10-14
        • 1970-01-01
        • 1970-01-01
        • 2019-03-31
        相关资源
        最近更新 更多