Hello,
I would like to manipulate a csv and store each column into an array for some comparisons before printing it into my table.
My lackluster PHP knowlegde has got me as far as:
- upload the csv file
- print the csv file onto the screen (just a big dump)
How would I:
- store each column into an array
- print the columns I desire into the table instead of everything?
<?php
$fileTempName = $_FILES['locationFile']['tmp_name'];
$fileOrigName = $_FILES['locationFile']['name'];
$fileSize = $_FILES['locationFile']['size'];
$fileType = $_FILES['locationFile']['type'];
$targetFileName = $_SERVER['DOCUMENT_ROOT'] . "ha-cms/" . $fileOrigName;
if (!copy($fileTempName, $targetFileName))
{
echo "failed to copy file.";
}
echo "<table>\
\
";
$f = fopen($targetFileName, "r");
$rowCtr = 1;
while (($line = fgetcsv($f)) !== false)
{
echo "<tr>";
foreach ($line as $cell)
{
echo "<td>" . htmlspecialchars($cell) . " " . $rowCtr . "</td>";
$rowCtr++;
}
echo "<tr>\
";
}
fclose($f);
echo "\
</table>";
?>
Any help would be greatly appreciated.
This code assumes a CSV with three columns, and loads the lines into the columns in the $data array. Then you could use that array to do your calculations.
<?php
$fileTempName = $_FILES['locationFile']['tmp_name'];
$fileOrigName = $_FILES['locationFile']['name'];
$fileSize = $_FILES['locationFile']['size'];
$fileType = $_FILES['locationFile']['type'];
$targetFileName = $_SERVER['DOCUMENT_ROOT'] . "ha-cms/" . $fileOrigName;
if (!copy($fileTempName, $targetFileName))
{
echo "failed to copy file.";
}
$data = array(
'col0' => array();
'col1' => array();
'col2' => array();
);
$f = fopen($targetFileName, "r");
$rowCtr = 1;
while (($line = fgetcsv($f)) !== false)
{
$data['col0'][] = $line[0];
$data['col1'][] = $line[1];
$data['col2'][] = $line[2];
}
fclose($f);
var_dump($data);
?>
Whoa thank you so much you have assisted me greatly, and I have learnt something new.
Many thanks!
Forgive me if I asked one more question, what would be the tidiest way to obtain the second array within my first?
I.E. How could I replace the <td>1/</td> with the contents of $data[‘status’] = $line[5];
I have seen some examples with nested loops, and I have other examples using a different function unfamiliar to me called “each”, what would be the recommended way?
while (($line = fgetcsv($f)) !== false)
{
$data['country'][] = $line[9];
$data['status'][] = $line[5];
//$data['col2'][] = $line[2];
}
echo "<table>";
foreach ($data['country'] as $printCols)
{
$printCols = ucfirst($printCols);
if ($printCols == 'Country')
{
echo "<thead>";
echo "<tr>";
echo "<th>Row Number</th>";
echo "<th>Country</th>";
echo "<th>Status</th>";
echo "</tr>";
echo "</thead>";
}
else
{
switch ($printCols)
{
default:
echo "<tr>";
echo "<td>$rowCtr</td>";
echo "<td>$printCols</td>";
echo "<td>1</td>";
echo "</tr>";
$rowCtr = $rowCtr + 1;
break;
case 'Country':
continue;
case '':
$emptyCtr = $emptyCtr + 1;
echo "<tr>";
echo "<td>$rowCtr</td>";
echo "<td>" . "BLANK" . "</td>";
echo "<td>1</td>";
echo "</tr>";
$rowCtr = $rowCtr + 1;
break;
}
}
}