samedi 1 février 2020

How to display an adaptive result of a CSV file using checkboxes and select option

As you can view in the image here View Image , I am using a form which have check-boxes and select option. Upon import of my CSV files using PHP, it displays the data correctly with the column headers. It also displays the CHECKED-boxes with its selected option only.

my csv file:

year, Exports of goods(F.O.B), Domestic exports, Re-exports.

1, 5, 10,20

1, 5, 10, 20

1, 5, 10, 20

1, 5, 10, 20

However am having real problem in the displaying the selected option only once. Also if I do not choose the first column of my CSV file 'year' , it displays me 'no links'. Meaning it only works when column 1 is selected.

I just need a result which is adaptive. That is for example, if I checked on row 2 and 4 for EX. 'Export of goods' and 're-exports', I need it to display the checked data with its selected option. I can't figure out on how to make it adaptive.View Image

Can someone help me please. Thank you.

<body>
<?php
   $uploadfile = $_SESSION['uploadCsv'];
    $file = fopen($uploadfile,"r");
    $sum = array();
    while(!feof($file)) {
      $csv = fgetcsv($file,1024);
      array_push($sum,$csv[1]);
    }
    echo array_sum($sum)."\n sum of column 2";
?>
<?php
   $uploadfile = $_SESSION['uploadCsv'];
    $file = fopen($uploadfile,"r");
    $sum = array();
    while(!feof($file)) {
      $csv = fgetcsv($file,1024);
      array_push($sum,$csv[2]);
    }
    echo array_sum($sum)."\n sum of column 3";
?>

  
  <table border="0"  >

   

  <?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);

        $row_length = count($mycsv);


      #Find sum of csv
      foreach($mycsv[1] as $col_num => $col)
      {
          

        echo "<tr>";
          
    
    
          for($x=0; $x<$row_length; $x++)
          {
              array_push($sum, $mycsv[$row][$key]);
          }
      
        }     

      #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='average'>Average</option>
              <option  value='highest'>Highest Value</option>
              <option  value='lowest'>Lowest Value</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>
          <tr><td><input type='submit' value='submit' name='submit'</td></tr><table></form>";
            }

  ?>
  </tbody>


 <div>
 <?php
if (($handle = fopen("sample1.csv", "r")) !== FALSE) {
                   $mycsv = array();
                   while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
                       $mycsv[] = $data;
                   fclose($handle);
                 
                   $post_data  = $_POST["checklist"];
                   //$SelectedOption = $_POST['demo'];
                   for($i=0; $i < count($post_data); $i++)
                   {
                      echo "Selected : " . $post_data[$i]."</br>" ;
                   }
                     
                   $row_length = count($mycsv);
                   $col_length = count($post_data);
                   $flag = True;
                  }

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

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

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

           

              foreach($_POST['method'] as $method)
                {

                  
                    $method = $package[$key];
                    echo "<tr><td>";
                    if ($method == 'sum') 
                    {   
                      $file = fopen($uploadfile,"r");
                      $sum = array();
                      while(!feof($file)) {
                      $csv = fgetcsv($file,1024);
                      array_push($sum,$csv[$key]);           
                      }
                      echo "Sum of " . $value. " = ".array_sum($sum);
                    }
                    else if ($method == 'mean') 
                    {
                      echo "mean of " . $value;
                    }
                    else if ($method == 'average') 
                    {
                      echo "Averge of " . $value;
                    }
                    else if ($method == 'highest') 
                    {
                      echo "highest value of " . $value;
                    }else if ($method == 'lowest'){
                      echo "highest value of " . $value;
                    } else {
                    echo "no method chosen.";
                    }
                }   
                
        }
    } 

}


?>

View Image form here




Aucun commentaire:

Enregistrer un commentaire