使用复选框和联接表php mysqli过滤列

时间:2018-10-25 04:20:55

标签: php mysqli

我想显示检查数据中的列。例如,我检查了Lump和Web Break,所以只有Lump和Web Break列。我在搜索这个,但找不到。

这是我的复选框代码

</div>         

                <label>Select Defect</label>
                <table>
                <tr>
                <td width='300px'><p><input type="checkbox" name="chk[]" value=".1"> Big Hole</p>
                    <p><input type="checkbox" name="chk[]" value=".2"> Big Pin Hole</p>
                    <p><input type="checkbox" name="chk[]" value=".3"> Detection Off</p>
                    <p><input type="checkbox" name="chk[]" value=".4"> Extreme Hole</p>
                    <p><input type="checkbox" name="chk[]" value=".5"> Pin Hole</p>
                </td> 
                <td width='300px'><p><input type="checkbox" name="chk[]" value=".6"> Pin Light Spot</p>
                    <p><input type="checkbox" name="chk[]" value=".7"> Small Hole</p>
                    <p><input type="checkbox" name="chk[]" value=".8"> Uninspected</p>
                    <p><input type="checkbox" name="chk[]" value=".9"> Very Small Dark Spot</p>
                    <p><input type="checkbox" name="chk[]" value=".10"> Very Small Light Spot</p>
                </td>      
                <td width='300px'><p><input type="checkbox" name="chk[]" value=".11"> Web Break</p>
                    <p><input type="checkbox" name="chk[]" value=".12"> Filter Small</p>
                    <p><input type="checkbox" name="chk[]" value=".13"> Edge Filter Small</p>
                    <p><input type="checkbox" name="chk[]" value=".14"> Filter Light Spot</p>
                    <p><input type="checkbox" name="chk[]" value=".15"> Mini Dark Spot</p>
                </td>  
                <td width='300px'><p><input type="checkbox" name="chk[]" value=".16"> Small Dark Spot</p>
                    <p><input type="checkbox" name="chk[]" value=".17"> Lump</p>
                    <p><input type="checkbox" name="chk[]" value=".18"> Intensity Filter</p>
                    <p><input type="checkbox" name="chk[]" value=".19"> Big Dark Spot</p>
                    <p>&nbsp;</p>
                </td>                
              </table>
          </div>

这是数据表

    <thead>
      <th class="text-center" >Date Process</th>
      <th class="text-center" >Big Hole</th>
      <th class="text-center">Big Pin Hole</th>
      <th class="text-center" >Detection Off</th>
      <th class="text-center" >Extreme Hole</th>
      <th class="text-center" >Pin Hole</th>
      <th class="text-center" >Pin Light Spot</th>
      <th class="text-center" >Small Hole</th>
      <th class="text-center" >Uninspected</th>
      <th class="text-center" >Very Small Dark Spot</th>
      <th class="text-center" >Very Small Light Spot</th>
      <th class="text-center" >Web Break</th>
      <th class="text-center" >Filter Small</th>
      <th class="text-center" >Edge Filter Small</th>
      <th class="text-center" >Filter Light Spot</th>
      <th class="text-center" >Mini Dark Spot</th>
      <th class="text-center" >Small Dark Spot</th>
      <th class="text-center" >Lump</th>
      <th class="text-center" >Intensity Filter</th>
      <th class="text-center" >Big Dark Spot</th>
    </thead>

我从数据库中获取数据并使用连接

  <?php 


  $no = 0;
  $modal=mysqli_query($mysqli,"
  SELECT t_name_file.PROCESS_TIME,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 1 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `1`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 2 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `2`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 3 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `3`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 4 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `4`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 5 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `5`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 6 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `6`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 7 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `7`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 8 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `8`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 9 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `9`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 10 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `10`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 11 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `11`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 12 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `12`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 13 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `13`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 14 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `14`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 15 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `15`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 16 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `16`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 17 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `17`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 18 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `18`,
  COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 19 THEN t_defect_class.DEFECT_CLASS_NAME END) AS `19`
  FROM t_transaction
  INNER JOIN t_name_file ON t_transaction.NAMEFILE_ID=t_name_file.NAMEFILE_ID
  INNER JOIN t_defect_class ON t_transaction.DEFECT_CLASS_ID=t_defect_class.DEFECT_CLASS_ID
  GROUP BY t_transaction.DEFECT_CLASS_ID, t_name_file.PROCESS_TIME ORDER BY t_name_file.PROCESS_TIME");
  while($defect=mysqli_fetch_array($modal)){
  $no++;

?>
  <tr>
      <td class="text-center"><?php echo $defect['PROCESS_TIME']; ?></td>
      <td class="text-center"><?php echo  $defect['1']; ?></td>
      <td class="text-center"><?php echo  $defect['2']; ?></td>
      <td class="text-center"><?php echo  $defect['3']; ?></td>
      <td class="text-center"><?php echo  $defect['4']; ?></td>
      <td class="text-center"><?php echo  $defect['5']; ?></td>
      <td class="text-center"><?php echo  $defect['6']; ?></td>
      <td class="text-center"><?php echo  $defect['7']; ?></td>
      <td class="text-center"><?php echo  $defect['8']; ?></td>
      <td class="text-center"><?php echo  $defect['9']; ?></td>
      <td class="text-center"><?php echo  $defect['10']; ?></td>
      <td class="text-center"><?php echo  $defect['11']; ?></td>
      <td class="text-center"><?php echo  $defect['12']; ?></td>
      <td class="text-center"><?php echo  $defect['13']; ?></td>
      <td class="text-center"><?php echo  $defect['14']; ?></td>
      <td class="text-center"><?php echo  $defect['15']; ?></td>
      <td class="text-center"><?php echo  $defect['16']; ?></td>
      <td class="text-center"><?php echo  $defect['17']; ?></td>
      <td class="text-center"><?php echo  $defect['18']; ?></td>
      <td class="text-center"><?php echo  $defect['19']; ?></td>

      </td>
  </tr>    

<?php } ?>

</div>

当我尝试过滤列但不适用于字段时,它起作用。

2 个答案:

答案 0 :(得分:0)

您必须解析$_POST数组中的值,并仅使用用户选择的列来构建查询。

请记住要检查是否输入了一些值,否则,您将不会选择任何会产生错误的内容。另外,我不确定这些复选框是否具有值,因此我将它们设置为整数而不是小数来匹配您提供的SQL。

确认用户已选中至少一个框的示例-     

if(isset($_POST['chk']){
  // do the example stuff below and build your query
}else{
  // use your existing query string as-is
}

?>

然后根据选中的复选框来构建查询。使用注释中所示的数组示例值

<?php
// this would be $_POST['chk'] if the user checked
// those 3 particular checkboxes
$arr=array(7,8,9);

$queryString="SELECT t_name_file.PROCESS_TIME,";

foreach($arr as $conditionValue){
    $queryString.="
COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN ".$conditionValue." THEN t_defect_class.DEFECT_CLASS_NAME END) AS '".$conditionValue."' ,";
}
$queryString=trim($queryString,",");
$queryString.="FROM t_transaction
INNER JOIN t_name_file ON t_transaction.NAMEFILE_ID=t_name_file.NAMEFILE_ID
INNER JOIN t_defect_class ON t_transaction.DEFECT_CLASS_ID=t_defect_class.DEFECT_CLASS_ID
GROUP BY t_transaction.DEFECT_CLASS_ID, t_name_file.PROCESS_TIME ORDER BY t_name_file.PROCESS_TIME";


print("\n\n".$queryString."\n\n");

?>

生成

SELECT t_name_file.PROCESS_TIME,
COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 7 THEN t_defect_class.DEFECT_CLASS_NAME END) AS '7' ,
COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 8 THEN t_defect_class.DEFECT_CLASS_NAME END) AS '8' ,
COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN 9 THEN t_defect_class.DEFECT_CLASS_NAME END) AS '9' FROM t_transaction
INNER JOIN t_name_file ON t_transaction.NAMEFILE_ID=t_name_file.NAMEFILE_ID
INNER JOIN t_defect_class ON t_transaction.DEFECT_CLASS_ID=t_defect_class.DEFECT_CLASS_ID
GROUP BY t_transaction.DEFECT_CLASS_ID, t_name_file.PROCESS_TIME ORDER BY t_name_file.PROCESS_TIME

最后,在确保所有查询都正确之后,我将在查询字符串中提取硬返回值...

答案 1 :(得分:0)

谢谢,现在可以使用了。

这是我的代码

    if (isset($_POST['chk'])){
  $defect_query = "";
  foreach ($_POST['chk'] as $id => $data) {
    $defect_query = $defect_query.", COUNT(CASE t_transaction.DEFECT_CLASS_ID WHEN ".$data." THEN t_defect_class.DEFECT_CLASS_NAME END) AS `".$data."`";
  }

  $machine      = $_POST['machine'];
  $start_date   = $_POST['start_date'];
  $end_date     = $_POST['end_date'];
  $grade        = $_POST['grade'];
  $chk          = $_POST['chk'];
?>

    <thead>
      <th class="text-center" >Date Process</th>
      <?php
        foreach ($_POST['chk'] as $id => $data) {
          $get_defect_name = mysqli_query($mysqli, "SELECT * FROM `t_defect_class` WHERE `DEFECT_CLASS_ID` = $data");
          $defect_name_table = mysqli_fetch_assoc($get_defect_name);
        ?>
        <th class="text-center"><?php echo $defect_name_table['DEFECT_CLASS_NAME']; ?></th>
        <?php } ?>
    </thead>
<?php



  $modal=mysqli_query($mysqli,"SELECT t_name_file.PROCESS_TIME".$defect_query." FROM t_transaction INNER JOIN t_name_file ON t_transaction.NAMEFILE_ID=t_name_file.NAMEFILE_ID INNER JOIN t_defect_class ON t_transaction.DEFECT_CLASS_ID=t_defect_class.DEFECT_CLASS_ID GROUP BY t_transaction.DEFECT_CLASS_ID, t_name_file.PROCESS_TIME ORDER BY t_name_file.PROCESS_TIME");
  $no = 0;
  while($defect=mysqli_fetch_array($modal)){
  $no++;

?>
  <tr>
      <td class="text-center"><?php echo $defect['PROCESS_TIME']; ?></td>
      <?php foreach ($_POST['chk'] as $id => $data) { ?>
        <td class="text-center"><?php echo  $defect[$data]; ?></td>
      <?php } ?>
  </tr>


<?php 
} 
} ?>