【问题标题】:How to display only the selected option for several chosen checkboxes of my displayed csv file column headers如何仅显示我显示的 csv 文件列标题的几个选定复选框的选定选项
【发布时间】:2020-01-31 07:40:42
【问题描述】:

  
  <table border="0" cellspacing="1" cellpadding="1" class="sortable" >

   

  <?php
      #read CSV file
      $uploadfile = $_SESSION['uploadCsv'];
      
      if (($handle = fopen($uploadfile, "r")) !== FALSE) {
        $mycsv = array();
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
        $mycsv[] = $data;
        fclose($handle);


      #Find the length of the transposed row

      $row_length = count($mycsv);
      
      echo "<form method='post' action=''><table><tr>";
      echo "<th><input type='checkbox' onClick='toggle(this)' /> Select/Deselect All<br/>
      </th>";
      echo "<th>Column</th>";
      echo "<th>Filter</th>";


      echo "</tr>";

      #Loop through each row (or each line in the csv) and output all the columns for that row
      foreach($mycsv[0] as $col_num => $col)
      {
          

        echo "<tr>";
          
    
    
          for($x=0; $x<1; $x++)
          {
              
          

          echo "<td><input type='checkbox' name='lang[]'  value='".$mycsv[$x][$col_num]."' ></td>";
          echo "<td align='center'>"  .$mycsv[$x][$col_num]."</td>";
          echo "<td>
            <select name='method[]' >
             <option value=''>Choose</option>
              <option  value='sum'>Sum</option>
              <option  value='mean'>Mean</option>
              <option  value='standard deviation'>Standard deviation</option>
              <option  value='mode'>Mode</option>
            </select>
              </td>";
          echo "</tr>";
          }
          
      }
          echo "<tr>";
          echo "<td><input type='checkbox' name='lang[]' ></td>";
          echo "<td align='center'><input type='text' name='own_column' placeholder='Type your new column here'><br></td>";
          echo "<td><input type='text' name='own_column' placeholder='Own formula'><br></td>";
          echo "</tr>";

          //echo "<tr>";
          //echo "<td colspan='3' align='center'><input type='submit' name='Order'/></td>";
          //echo "</tr>";
          echo "<tr>";
          echo "<td colspan='3'><div class='wrapper'><br/><br/><input type='button' class='button' value='Input Button'>
          </div></td>";
          echo "</tr>
          <input type='submit' value='submit' name='submit'<table></form>";
            }

  ?>
  </tbody>


 <div style="border:solid 2px red;width:50%;">
 <?php

$package = $_POST['method'];
if(isset($_POST['submit'])){

   if(!empty($_POST['lang'])) {

       foreach($_POST['lang'] as $value){
          echo "<tr><td>";
           echo " value : ".$value.'<br/>';
           echo "</td></tr>";

          if(!empty($_POST['method'])) 
           {

           foreach($_POST['method'] as $method){
            if($method == 'sum')
            {
              echo "<tr><td>";
              echo "Sum of ".$value."";
              echo "</td></tr>";
            } else if ($method == 'mean')
            {
              echo "<tr><td>";
              echo "mean of ".$value."";
              echo "</td></tr>";
            }else if ($method == 'mode')
            {
              echo "<tr><td>";
              echo "mode";
              echo "</td></tr>";
            }
            {
              echo"no links";
            }
  
                //echo "method : ".$method.'<br/>';
          }
        
           }

          

       }

   }

  

}
?>

My form image 我正在使用具有复选框和选择选项的表单。使用 PHP 导入我的 csv 文件后,它会使用列标题正确显示数据。但是,在如何仅显示带有所选选项的 CHECKED 框时遇到了真正的麻烦。

View image form 如何仅显示选定复选框的选定选项。请帮忙。

【问题讨论】:

  • 展示您的代码而不是图片
  • @AksenP 你好,我已经添加了代码,谢谢。

标签: php csv select checkbox option


【解决方案1】:

尝试用下面的代码替换你完整的 foraeach foreach($_POST['lang'] as $value){...} 部分

foreach ($_POST['lang'] as $key => $value) {

        $method = $package[$key];
        echo "<tr><td>";
        if ($method == 'sum') {
           echo "Sum of " . $value;
        }
        else if ($method == 'mean') {
           echo "mean of " . $value;
        }
        else if ($method == 'mode') {
           echo "mode";
        }
        else {
           echo"no links";
        }
        echo "</td></tr>";
}

希望这能解决这个问题

【讨论】:

  • 非常感谢,它确实解决了问题。 :D
  • 你好,它正在工作,但我遇到了问题。它仅在选中第一行的复选框时才有效。这意味着如果我从第二行开始检查,它显示我没有链接。也是连续的。这意味着如果我选中了复选框 1,2 和 4。这里也没有显示链接。如果我检查 1,2,3,4 它工作正常。请帮忙。谢谢
猜你喜欢
  • 2018-04-23
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
相关资源
最近更新 更多