function addFile($fileContents, &$dataArray, &$addDateTime) { $validHeaders = array(); $csvArray = csvToArray($fileContents); if (validDateTime($csvArray)) { if ($addDateTime) { //If we need to add date time, leave it in the array -- we don't need it anymore after this point $addDateTime = false; } else { // Else remove the date/time column $dateTimes = getDateTime($dataArray); reorderArray($dateTimes, $csvArray); //reoder the array before combining so that the data will match previous data removeDateTime($csvArray); } combineArrays($dataArray, $csvArray); return true; } else { return false; } }
foreach ($locations as $location) { if (!is_numeric($location)) { array_push($locArray, $location); } else { break; } } $locString = implode($locArray); unset($contentsArray[$index]['location']); $contentsArray[$index]['location'] = $locString; } return $contentsArray; } // This function formats the area by exploding on spaces for each area and then resetting the value of each area only to the first value in the areas array. function formatArea($contentsArray) { foreach ($contentsArray as $index => $value) { $areas = explode(' ', $contentsArray[$index]['area']); unset($contentsArray[$index]['area']); $contentsArray[$index]['area'] = $areas[0]; } return $contentsArray; } // Each function is called and saved as $contentsArray to cut down on variables. // Each time the updated version of the array is saved over the previous array of the same name. $contentsArray = readTheFile('public/data/parks.csv'); $contentsArray = cutOne($contentsArray); $contentsArray = reorderArray($contentsArray); $contentsArray = formatLocation($contentsArray); $contentsArray = formatArea($contentsArray); // print_r($contentsArray);
$contentsArray[$index]['method'] = $valueArray[5]; $contentsArray[$index]['categories'] = substr($valueArray[6], 0, -1); } return $contentsArray; } // Each function is called and saved as $contentsArray to cut down on variables. // Each time the updated version of the array is saved over the previous array of the same name. $contentsArray = readTheFile('data_ads1.txt'); $contentsArray = cutOne($contentsArray); $contentsArray = reorderArray($contentsArray); $contentsArray2 = readTheFile('reagan_spatulas.txt'); $contentsArray2 = cutOne($contentsArray2); $contentsArray2 = reorderArray($contentsArray2); $contentsArray3 = readTheFile('database_ads.txt'); $contentsArray3 = cutOne($contentsArray3); $contentsArray3 = reorderArray($contentsArray3); $masterArray = array_merge($contentsArray, $contentsArray2, $contentsArray3); // print_r($contentsArray); $query = "INSERT INTO ads (user_id, method, image_url, title, price, location, description, categories) \n VALUES (:user_id, \n :method,\n :image_url,\n :title, \n :price, \n :location, \n :description,\n :categories)"; $stmt = $dbc->prepare($query); foreach ($masterArray as $ad) { $stmt->bindValue(':user_id', rand(1, 3), PDO::PARAM_INT); $stmt->bindValue(':method', $ad['method'], PDO::PARAM_STR); $stmt->bindValue(':image_url', $ad['image_url'], PDO::PARAM_STR); $stmt->bindValue(':title', $ad['title'], PDO::PARAM_STR); $stmt->bindValue(':price', $ad['price'], PDO::PARAM_STR); $stmt->bindValue(':location', $ad['location'], PDO::PARAM_STR); $stmt->bindValue(':description', $ad['description'], PDO::PARAM_STR); $stmt->bindValue(':categories', $ad['categories'], PDO::PARAM_STR); $stmt->execute(); echo "Inserted ID: " . $dbc->lastInsertId() . PHP_EOL;