PHP code example of pllano / csv
1. Go to this page and download the library: Download pllano/csv library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pllano / csv example snippets php
ilename = 'test.csv';
$csv = new Pllano\Csv\Reader($filename);
$records = $csv->Read();
$count = count($records);
if ($count >= 1) {
foreach ($records as $item) {
print_r($item);
print_r('<br>');
}
}
php
ilename = 'test.csv';
$csv = new Pllano\Csv\Reader($filename);
$csv->setItemStart(10);
$csv->setExecute(1);
$records = $csv->Read();
print_r($records);
/*
Array
(
[0] => Array
(
[name] => Lorem
[number] => 11
[price] => 22.00
)
)
*/
php
ilename = 'test2.csv';
$csv = new Pllano\Csv\Reader($filename);
$csv->setItemStart(10); // start item - default: 1
$csv->setExecute(50); // amount - default: 0
$records = $csv->Read();
$item_start = $csv->getItemStart(); // returns 0
$count = count($records);
if ($count >= 1) {
foreach ($records as $key => $item) {
$real_key = $key + $item_start;
print_r($real_key);
print_r(' - ');
print_r($item);
print_r('<br>');
}
}
php
function clean($value = '')
{
$value = trim($value);
$value = stripslashes($value);
$value = strip_tags($value);
$value = htmlspecialchars($value, ENT_QUOTES);
// $value = htmlentities($value);
return $value;
}
$filename = 'test.csv';
$start = 0;
$rows_total = 0;
if ($_GET["filename"]) {$filename = clean($_GET["filename"]);}
if ($_GET["start"]) {$start = clean($_GET['start']);}
// Include Composer autoloader if not already done.
29); // Monitoring the execution time of the script in seconds set_time_limit
// $csv->setAutoDetection(false); // Auto Detection Delimiter false|true - default: false
$stop = 500;
$records = $csv->Read();
$count = count($records);
if ($count >= 1) {
foreach ($records as $item) {
print_r($item);
print_r('<br>');
}
}
$rows_total = $csv->countItems(); // returns total items
// $csv->getHeaders(); // returns Array ( [0] => name [1] => number [2] => price )
// $csv->getItemStart(); // returns string
// $csv->getExecute(); // returns amount 10
$end = $csv->getItemEnd(); // returns 11
// $csv->getAutoDetection(); // returns false|true
// $csv->getCsvControl(); // returns Array ( [0] => ; [1] => " [2] => \ )
if ($filename && $end >= 0 && $rows_total >= 0) {
if ($end <= $rows_total && $end <= $stop) {
// start //site.com/test.php
print '<meta http-equiv="Refresh" content="0; url=/test.php?filename='.$filename.'&start='.$end.'">';
}
if ($end >= $stop || $end >= $rows_total) {
print_r('<br>');
print_r('Memory, MB: '.$csv->getMemory());
print_r('<br>');
print_r('Time, sec: '.$csv->getTime());
}
}