Jūs esateŽurnalai / Ernestas Kardzys's blog / PHP. Working with CSV files
PHP. Working with CSV files
I wanted to create an index for my blog, so I needed to download stock prices. I download them from "Nasdaq OMX Vilnius" web site.
Maybe this might be useful for more people, so, here it is :)
Good friend of mine Paulius helped me with this CSV-based stock index counter :) If you need awesome web site for a very attractive price - don't hesitate to contact him :)
Ok, so my index counter :) I'm not very good at PHP, so the code is not perfect. But it works :)
<?php define(FILE, "file.csv"); /* Gets the content of the CSV file */ function downloadCSV($file) { $ch = curl_init(); $fp = fopen (dirname(__FILE__) . FILE, 'w+'); curl_setopt($ch, CURLOPT_URL, $file); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); return $fp; } /* Closes the CSV file */ function closeCSV($file) { return fclose($file); } /* Gets data from the CSV data file */ function getData($file, $number) { return $file[$number]; } /* Analyzes CSV file for market data */ function analyzeCSVFile($handle, $line_length, $delimiter) { return fgetcsv($handle, $line_length, $delimiter); } /* Gets the path to the CSV file */ function getCSVPath() { return "http://www.php.rules/folder/awesome_file_with_stock_prices.csv"; } /* Calculates the capilization of the stock */ function getCurrentCapitalization($name, $price) { $capitalization = 0; switch ($name) { case "STOCK_PRICE1": $capitalization = str_replace(",", ".", $price) * NUMBER_OF_STOCK_PRICE1_SHARES; break; case "STOCK_PRICE2": $capitalization = str_replace(",", ".", $price) * NUMBER_OF_STOCK_PRICE2_SHARES; break; default: break; } return $capitalization; } /* Calculates the index of the nowaday */ function calculateIndex($current_capitalization) { $starting_capitalization_value = STARTING_CAPITALIZATION_OF_STOCKS; /* Starting capitalization value */ return ($current_capitalization / $starting_capitalization_value) * 100; } downloadCSV(getCSVPath()) or die(); $handle = fopen(FILE, 'r') or die('Error opening for reading!'); $total_capitalization = 0; while(($data = analyzeCSVFile($handle, 1000, ";")) != FALSE) { $total_capitalization += getCurrentCapitalization(getData($data, 0), getData($data, 9)); } fclose($handle); printf("<b>Index: %.2f</b><br><br>\n", calculateIndex($total_capitalization)); ?>My friend Paulius also said, that making functions getCSVPath() is not very good, because it has only "return" which probably will slow the hole process, so keep in mind that :)


Yeah :P
> My friend Paulius also said, that making functions getCSVPath() is not very good, because it has only “return” which probably will slow the hole process, so keep in mind that :)
yeah, and in such frequently used code it's very very important, right? :))
C# and OOP influence: every action in separate method / function ;)
/* Closes the CSV file */
function closeCSV($file) {
return fclose($file);
}
/* Gets data from the CSV data file */
function getData($file, $number) {
return $file[$number];
}
/* Analyzes CSV file for market data */
function analyzeCSVFile($handle, $line_length, $delimiter) {
return fgetcsv($handle, $line_length, $delimiter);
}
/* Gets the path to the CSV file */
function getCSVPath() {
return “http://www.php.rules/folder/awesome_file_with_stock_prices.csv”;
}
Why just don't use:
fclose($file) instead of closeCSV($file)
$file[$number] => getData($file, $number
fgetcsv($handle, $line_length, $delimiter) => analyzeCSVFile($handle, $line_length, $delimiter)
$getCSVPath => getCSVPath() [$getCSVPath = “http://www.php.rules/folder/awesome_file_with_stock_prices.csv”;]
Less function calls, less code. :)
Thanks ;)
And one more, don't use @ symbol behind function, because it makes your code very slow. So you mast use something else like if() :) Or if you are lazy disable all errors in your site. :)
Skelbti naują komentarą