<!DOCTYPE html>
<html>
<head>
<title>Start1</title>
</head>
<body>
<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;
}
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>
</body>
</html>
View image Please view this image to have an idea. 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.
When I run the following codes above, it displays me the sum of each, however the sum is adding up with the other mean. Meaning it is not giving me the the correct sum.
if the sum is not working out, I dont know how i will make the mean average and others work out, please help.
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
Thank you.
Aucun commentaire:
Enregistrer un commentaire