Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print first 3 columns of CSV file in table format in PHP upon file upload

I am new to PHP and I am stuck with this problem.

$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
if (((in_array($_FILES['uploadedfile']['type'],$mimes)))
    && ($_FILES["uploadedfile"]["size"] > 0))
{
    $filename = $_SESSION['username'].$_FILES["uploadedfile"]["name"];
    move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],"./uploads/".$filename);

    echo "Stored in: " . "./uploads/" .$filename;
    echo "<br />\n";
    echo 'Daylight hours will be calculated for this latitude : '.$_POST['latCSV'];
    echo "<br>";
    //print out uploaded file.csv
    echo "<table BORDER=1>\n\n";
    echo "  <tr><th>AvgCrown</th><th>MinCrown</th><th>MaxCrown</th><th>dateLogged</th><th>DaylightHours</th></tr>";
    echo "<tbody id='tblBdyLoggedData'>";
    $f = fopen("./uploads/" .$filename, "r");
    $i=0;   //just a counter
    while (($line = fgetcsv($f)) !== false) {
        echo "<tr>";
        foreach ($line as $cell) {
            // Save the value before you output
            $lastColValue = htmlspecialchars($cell);
            //unset($lastColValue[3]);
            echo "<td>" . $lastColValue . "</td>";

        }
        //checking if date is valid
        checkDateString($lastColValue);

        // If you want to store all log dates in an array:
        $logDates[] = $lastColValue;                            
        $firstlogDate = $logDates[$i];
        echo "<td>" . checkDateString($lastColValue) . "</td>";
        //call function using firstlogDate for every row
        echo "<td>" . calcDaylight($firstlogDate,$_POST["latCSV"]) . "            </td>";
        echo "</tr>\n";
        $i = $i+1;
    }
    fclose($f);
    echo "</tbody></table></body></html>";
}

I would like to get print only the first 3 columns of the CSV file upon file upload as the two other will be calculated using respective functions. Datelogged will be taken from the csv and reformated properly and daylight Hours is also calculated from datelogged.

like image 469
Kunal Avatar asked Jan 28 '26 08:01

Kunal


2 Answers

First, parse out the $file_contents using the PHP filesystem library:

$foo = split(',', $file_contents);

Reference: http://php.net/manual/en/book.filesystem.php

Now you can access the first three columns:

echo foo[0] . '/' . foo[1] . '/' . foo[2];
like image 188
Daniel Li Avatar answered Jan 29 '26 22:01

Daniel Li


May this code will help you

?php
echo "<html><body><table>\n\n";
$f = fopen("so-csv.csv", "r");
while (($line = fgetcsv($f)) !== false) {
        $count=0;
        echo "<tr>";
        foreach ($line as $cell ) 
        {
            if($count<3)
            {    
                echo "<td>" . htmlspecialchars($cell) . "</td>";
                $count++;
            }
        }
        echo "<tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
like image 21
Hardik Avatar answered Jan 29 '26 22:01

Hardik