Example #1
0
/**
 * 1b. Load CSV file into the DB.
 * @param string $filename The CSV filename location to load.
 * @return bool
 * @throws Exception
 */
function loadCSVTransactions($filename)
{
    $db = new Database();
    if (($fhandler = fopen($filename, "r")) === false) {
        throw new Exception("Unable to open the CSV file");
    }
    while (($row = fgetcsv($fhandler)) !== false) {
        $price = trim($row[1]);
        if (is_numeric($price) && $price >= 0) {
            $category_name = trim(strtolower($row[0]));
            $datetime = trim(strtolower($row[2]));
            $db->addTransaction($category_name, $price, $datetime);
        }
    }
    return true;
}