2016-01-23 164 views
0

我想根據下面的數據庫顯示下拉列表。 (bom table)根據ID綁定下拉列表PHP

例如,當我從下拉列表中選擇'Table'時,它會顯示bom_description,bom_quantity和UOM。

bom_id |bom_description |product_id |finish_product |bom_quantity |UOM 
1  Table Tops  1   Table    1  /PC 
2  Table Legs  2   Chair    4  /PC 
3  Chair Seat  3        1  /PC 
4  Chair Back  4        1  /PC 
5  Chair Legs  5        4  /PC 

選擇產品:表(下拉列表)

(HTML表會顯示以下)

bom_description |bom_quantity |UOM 
Table Tops  1    /PC 
Table Legs  4    /PC 

當我點擊椅子,它會顯示這個

選擇產品:椅子(下拉式清單)

bom_description |bom_quantity |UOM 
Chair Seat  1    /PC 
Chair Back  1    /PC 
Chair Legs  4    /PC 

現在,這是我的代碼,但不顯示用於顯示內容的html表格。

<script src="script/jquery-1.11.1.min.js"></script> 
    <script> 
     $(document).ready(function() { 
      $(".itemTypes").change(function() { 
       if (this.value == 0) { 
        $("tr").show(); 
       } 
       else { 
        $("tr").hide(); 
        $(".header").show(); 
        $("." + this.value).show(); 
       } 
      }); 
     }); 
    </script> 

<?php 
    session_start(); 

if (!isset($_SESSION['username'])) { 
    header('location: homepage.php'); 
} 

//connect to database 
include ("dbFunctions.php"); 

$queryBOM = "SELECT * FROM bom"; 

$resultBOM = mysqli_query($link, $queryBOM) or die(mysqli_error($link)); 
mysqli_close($link); 
?> 

Select Product: 
         <select class="itemTypes"> 
          <option value="0"> 
           all types 
          </option> 
          <?php 
          while ($row1 = mysqli_fetch_array($resultBOM)) { 
           ?> 
           <option value="<?php echo $row1['finish_product']; ?>"> 
            <?php echo $row1['finish_product']; ?> 
           </option> 
          <?php } ?></select> 
         <p> 


          <table border="1"> 
           <tr> 
            <th>BOM Description</th> 
            <th>Quantity</th> 
            <th>UOM</th> 
            <th colspan="2">Action</th> 

            <?php 
            while ($row = mysqli_fetch_assoc($resultBOM)) { 
             $bom_description = $row['bom_description']; 
             $bom_quantity = $row['bom_quantity']; 
             $UOM = $row['UOM']; 
             ?> 

             <tr> 
              <td><center><?php echo $bom_description; ?></center></td> 
              <td><center><?php echo $bom_quantity; ?></center></td> 
              <td><center><?php echo $UOM; ?></center></td> 
              <td><center> 
                <form method="post" action="editBOM.php"> 
                 <input type="hidden" name="bomID" value="<?php echo $bom_id; ?>" /> 
                 <input type="image" src="images/editicon.png" name="edit" value="edit" style="width:30px;height:30px;"/> 
               </center></form> 
              </td> 
              <td><center> 
                <form method="post" action="dodeleteBOM.php"> 
                 <input type="hidden" name="bomID" value="<?php echo $bom_id; ?>" /> 
                 <input type="image" src="images/deleteicon.png" name="delete" value="delete" style="width:25px;height:25px;"/> 
               </center></form> 
               </center></td> 
              <?php 
             } 
             ?> 
            </tr> 

如何解決這個問題?

+0

你的第一張表看起來不對。不應該兩個第一行具有「finished_product」值作爲「表」和最後三個「主席」? – Heah

+0

@愛德華請投票答覆也 –

回答

0
You can do it in following way:- 

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script> 
    <script> 
     $(document).ready(function() { 
      $(".itemTypes").change(function() { 
       var vall= $(this).val(); 
       if (vall== 0) { 
        $("tr").show(); 
       } 
       else { 
        $('form#abc').submit(); 
       } 
      }); 
     }); 
    </script> 

<?php 
    session_start(); 

if (!isset($_SESSION['username'])) { 
    header('location: homepage.php'); 
} 

//connect to database 
include ("dbFunctions.php"); 
$post_data=''; 
if(isset($_POST['value'])){ 
    $post_data = $_POST['value']; 
    $queryBOM1 = "SELECT * FROM bom WHERE bom_description LIKE '%".$_POST['value']."%'"; 
}else{ 
$queryBOM1 = "SELECT * FROM bom"; 
} 
$queryBOM2= "SELECT * FROM bom"; 

$resultBOM = mysqli_query($link, $queryBOM2) or die(mysqli_error($link)); 
$resultBOM1 = mysqli_query($link, $queryBOM1) or die(mysqli_error($link)); 
mysqli_close($link); 
?> 

Select Product: 
         <form action = "<?php echo $_SERVER['REQUEST_URI'];?>" method="POST" id="abc"> 
         <select class="itemTypes" name="value"> 
          <option value="0"> 
           all types 
          </option> 
          <?php 
          while ($row1 = mysqli_fetch_array($resultBOM)) { 
           ?> 
           <option value="<?php echo $row1['finish_product']; ?>" <?php if($row1['finish_product']==$post_data){echo "selected";}?>> 
            <?php echo $row1['finish_product']; ?> 
           </option> 
          <?php }?></select> 
</form> 
         <p> 
          <table border="1"> 
           <tr> 
            <th>BOM Description</th> 
            <th>Quantity</th> 
            <th>UOM</th> 
            <th colspan="2">Action</th> 

            <?php 
            while ($row = mysqli_fetch_assoc($resultBOM1)) { 
             $bom_description = $row['bom_description']; 
             $bom_quantity = $row['bom_quantity']; 
             $UOM = $row['UOM']; 
             ?> 

             <tr> 
              <td><center><?php echo $bom_description; ?></center></td> 
              <td><center><?php echo $bom_quantity; ?></center></td> 
              <td><center><?php echo $UOM; ?></center></td> 
              <td><center> 
                <form method="post" action="editBOM.php"> 
                 <input type="hidden" name="bomID" value="<?php echo $bom_id; ?>" /> 
                 <input type="image" src="images/editicon.png" name="edit" value="edit" style="width:30px;height:30px;"/> 
               </center></form> 
              </td> 
              <td><center> 
                <form method="post" action="dodeleteBOM.php"> 
                 <input type="hidden" name="bomID" value="<?php echo $bom_id; ?>" /> 
                 <input type="image" src="images/deleteicon.png" name="delete" value="delete" style="width:25px;height:25px;"/> 
               </center></form> 
               </center></td> 
              <?php 
             } 
             ?> 
            </tr>