How to import just two cells from CSV file to PHP - php

I have a CSV file with lots of data in it, from which I create many different tables using
PHP, specifying certain rows and columns.
However, I have a requirement for only showing two cells, A7 and A8. The code I currently use requires fields for table head and table data, which on this occasion I don't need.
What is the most effect way of doing this?

<?php
function get_cell($cell, $row)
{
$file = 'stuff.csv';
$row = $row -1;
// Reset the $row if it's negative.
if($row < 0)
{
$row = 0;
}
$handle = fopen ($file, "r");
$counter = 0;
while (($csvline = fgetcsv($handle, 450,";")) != false)
{
if($counter == $row)
{
$new_array = array();
$begin = 'a';
foreach($csvline as $cellnr)
{
$new_array[$begin] = $cellnr;
$begin++;
}
echo $new_array[strtolower($cell)];
}
$counter++;
}
}
get_cell('a', 1);
echo '<br>';
get_cell('b', 1);
?>

Related

Gravity forms: exporting two forms of entries data to one CSV file

I'm wondering if I can export two deferent forms entries based on the submitter id but with a specific concept,
The concept is:
If the entry title from the first form == some entry field of the other form, let's show these two entries in the exported data in one row, and so on, something like array_map in php.
I hope that you got the idea, any help to do that?
Thanks a lot,
Magdi
Use These Codes:
(first code returns data from csv file into an array)
(second code gets data from an array and save into a csv file)
function csvExporter($path) {
$csv = array();
$row = 1;
if (($handle = fopen($path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$csv[str_pad($row,4,0,STR_PAD_LEFT)] = array();
for ($c = 0; $c < $num; $c++) {
$csv[str_pad($row,4,0,STR_PAD_LEFT)][$c] = $data[$c];
}
$row++;
}
fclose($handle);
}
return $csv;
}
function csvImporter($path,$array) {
$csvFile = fopen($path,'w+');
foreach ($array as $rows) {
$eachRow = '[';
foreach ($rows as $columns) {
$eachRow .= '\'' . trim($columns,'\'') .'\',';
}
$eachRow .= ']' . PHP_EOL;
}
fclose($csvFile);
}

Modify PHP script to read from CSV file

I have script which was reading two variable from CSV and those variable was at column 0 and column 5 and it was fine:
$file = fopen($qtyfile,"r");
output("reading file $qtyfile");
$i=0;
$imported = 0;
$failed = 0;
while(! feof($file))
{
$i++;
$line = (fgetcsv($file));
if($i==1) continue;
$cols = explode(';',$line[0]);
$pcode = $cols[0];
$stock = $cols[5];
The CSV file has been orginzed in different way, and the variable now at column 16 & 19 i tried to modify the code to the following but it's not working :
$file = fopen($qtyfile,"r");
output("reading file $qtyfile");
$i=0;
$imported = 0;
$failed = 0;
while(! feof($file))
{
$i++;
$line = (fgetcsv($file));
if($i==1) continue;
$cols = explode(';',$line[0]);
$pcode = $cols[16];
$stock = $cols[19];
can you please help to make the script ready from new column.
any help will be appreciated.
Link to full script and the csv:
Here's an awesome little function which will parse any csv file into an associative array with column titles from row 1 as array keys.
function csvArray($file) {
$csv = array_map('str_getcsv', file($file));
array_walk($csv, function(&$a) use ($csv) {
$a = array_combine($csv[0], $a);
});
array_shift($csv);
return $csv;
}
Usage:
$output = csvArray($file);
foreach ($output as $o) {
echo $o['pcode'];
// whatever
}
I have to deal with quite a lot of csv files and I use this all the time. Once the data is in an array it's much easier to deal with, and it doesn't matter if columns get moved around as long as the name stays the same it won't break your code.

How to specify which csv file using multiple get_cell

I have a page with multiple get_cell functions within it that all successfully return a single cell value from a CSV file.
However, the first CSV file specified in the first instance is the one the rest pull information from. I have at least two different CSV files I need to reference on the same page.
How do I specify the CSV file, as well as the specific cell?
<?php
if (!function_exists('get_cell')) {
function get_cell($cell, $row)
{
$file = 'CSV/filename.csv';
$row = $row - 1;
// Reset the $row if it's negative.
if ($row < 0) {
$row = 0;
}
$handle = fopen($file, "r");
$counter = 0;
while (($csvline = fgetcsv($handle, 450, ",")) != false) {
if ($counter == $row) {
$new_array = array();
$begin = 'a';
foreach ($csvline as $cellnr) {
$new_array[$begin] = $cellnr;
$begin++;
}
echo $new_array[strtolower($cell)];
}
$counter++;
}
}
}
get_cell('A', 1);
?>
You can simply pass in the file name as one of the arguments, with CSV/filename.csv as a default if you'd like:
function get_cell($cell, $row, $file="CSV/filename.csv")
{
...
}
get_cell('A', 1, "CSV/filename2.csv");

How to use get_cell() more than once using php and the same csv file?

Using the code below, I can successfully pull a single piece of information from a CSV file, detailing a specific row and column i.e C,2.
If I repeat this function on the same page, I get the error message:
"Fatal error: Cannot redeclare get_cell() (previously declared in myphpfile.php:50) in myphpfile.php on line 187"
Is it possible to pick more than one cell from a CSV file using php?
Here's the code:
<?php
function get_cell($cell, $row)
{
$file = 'CSV/mycsvfile.csv';
$row = $row -1;
// Reset the $row if it's negative.
if($row < 0)
{
$row = 0;
}
$handle = fopen ($file, "r");
$counter = 0;
while (($csvline = fgetcsv($handle, 450,",")) != false)
{
if($counter == $row)
{
$new_array = array();
$begin = 'a';
foreach($csvline as $cellnr)
{
$new_array[$begin] = $cellnr;
$begin++;
}
echo $new_array[strtolower($cell)];
}
$counter++;
}
}
get_cell('C', 2);
?>

PHP and csv report calculations

I have a csv file that I would like to generate a summary report from. The csv looks like this :
The csv has in each row an activity and the coresponding time when it starts.
The summary I'm trying to generate has to look like this :
Basically I need to show each activity and the times when it starts and it ends
I did as following in PHP, I'm almost done but the result I get is not really what I want :
$csvFileName = "The csv path";
$report = array();
$file = fopen($csvFileName, "r");
while (($data = fgetcsv($file, 8000, "\n")) !== FALSE) {
$num = count($data);
for ($c = 0; $c < $num; $c++) {
$t = explode(',', $data[$c]);
$time = $t[0];
$activity = $t[1];
$report[] = array($activity, $time);
}
}
fclose($file);
//I'm reading the whole file content and copying it into an array.
$summaryReport = array();
$j = 1;
for($i=0; $i<sizeof($report); $i++){
if($report[$i][0] !== $report[$j][0]){
array_push($summaryReport,array($report[$i][0],$report[$i][1],$report[$j][1]));
}
$j++;
}
echo json_encode($summaryReport);
The output json looks like this :
[["Start","10:42","10:59"],["Driving route","11:10","11:50"],["Lunch-Rest Break","11:50","11:57"],["Driving route","11:57","12:03"],["Break","12:11","12:41"],["Driving route","13:05","14:09"],["Waiting","14:14","14:28"]]
What I'm looking for as result is something like that:
[["Start","10:42","10:59"],["Driving route","10:59","11:50"],["Lunch-Rest Break","11:50","11:57"],["Driving route","11:57","12:03"],["Break","12:03","12:41"],["Driving route","12:41","14:09"],["Waiting","14:09","14:28"],["End","14:28"]]
my coding logic is not really working well, does anyone see how can I do a simple loop to do what I'm looking for?
Thank you in advance.
The result can be achieved much easier. Look at my code, I got rid of all your inner loops, fixed syntax errors and there is no need to store the whole csv file in memory:
PHP code
<?php
$csvFileName = "./test.csv";
$file = fopen($csvFileName, "r");
$summaryReport = array();
$i = 0;
$previous_name = null;
while ($data = fgetcsv($file, 8000)) {
if ($previous_name !== $data[1])
{
$summaryReport[$i] = array($data[1], $data[0]);
if ($i > 0)
{
$summaryReport[$i-1][2] = $data[0];
}
$previous_name = $data[1];
++$i;
}
}
fclose($file);
echo json_encode($summaryReport);
Test csv file
10:41,Start
10:59,Driving
11:29,Driving
11:11,End
Output
[["Start","10:41","10:59"],["Driving","10:59","11:11"],["End","11:11"]]

Categories