mardi 4 février 2020

How can I display the sum of each csv headers correctly when selected, it is adding up in the loop

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

   <?php
      #read CSV file
      
      
    if (($handle = fopen("sample1.csv", "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='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>";

                    $post_data  = $_POST["lang"];
                    //$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;
                    $sum = array();
             
                    foreach ($mycsv[0] as $key => $value)
                    {
                        foreach ($post_data as $search_term) 
                        {

                            if (strcasecmp(trim($value), trim($search_term)) == 0) 
                            {

                                for ($row=0; $row < $row_length; $row++)
                                {
                                    
                                    array_push($sum,$mycsv[$row][$key]);

                                    if ($row == 0)
                                    {

                                        echo "<tr>";
                                        echo "<th colspan=100%>" . $mycsv[0][$key] . "</th>";
                                        echo "</tr>";
                                    }
                                    else if ($row == 1)

                                    {


                                       //echo "<td>" . $mycsv[$row][$key] . "</td>";
                                        //echo "array sum =".array_sum($sum)."";

                                    }  

                                           
                                //echo " array sum =".array_sum($sum)."] ";
                                
                                }

                                        $method = $_POST["method"];

                                        $method = $method[$key];
                                                                echo "<tr><td>";
                                                                if ($method == 'sum') {

                                                                                echo "Sum of " . $value;
                                                                                echo "array sum =".array_sum($sum)."";

                                                                }
                                                                else if ($method == 'mean') {
                                                                   echo "mean of " . $value;
                                                                }
                                                                else if ($method == 'average') {
                                                                   echo "average of " . $value;
                                                                }
                                                                else if ($method == 'highest') {
                                                                   echo "highest value of " . $value;
                                                                }else if ($method == 'lowest') {
                                                                   echo "lowest value of " . $value;
                                                                }else{
                                                                        echo " Choose a method ";
                                                                echo "</td></tr>";  
                                                                        }


                                                    
                                  }

                        }
                    }
        }

  ?>
</table>

view image

As you can view in the image,After checking and choosing filter 'sum', the total sum of column year is 5 and sum of goods is 20, the sum of goods should have been 15, but it adds up with column year. How can I display them correctly.

After sum, I need to display the mean. average, highest and lowest of each. Am not able to display there answer seperately. Can someone solve this for me.Thank you.

View CSV File image




Aucun commentaire:

Enregistrer un commentaire