Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // IMPORTANT!
- // Always Verify Table Columns in Text Asset Before Running File!
- // Also verify your file paths to ensure these functions call successfully.
- function listToArray($explodedList) {
- function nullChecker($explodedPokemon) {
- if ($explodedPokemon[2] !== "") {
- return $explodedPokemon[2];
- } else {
- return "N/A";
- }
- }
- $pokemonArray = [];
- foreach ($explodedList as $pokemon) {
- $explodedPokemon = explode(',', $pokemon);
- $singlePokemon = [];
- $singlePokemon[] = [
- "Pokemon_Name" => $explodedPokemon[0],
- "Pokedex_Number" => $explodedPokemon[1],
- "Mega_evolution" => nullChecker($explodedPokemon),
- "Type" => $explodedPokemon[3],
- "Total" => $explodedPokemon[4],
- "Hp" => $explodedPokemon[5],
- "Attack" => $explodedPokemon[6],
- "Defense" => $explodedPokemon[7],
- "Spatk" => $explodedPokemon[8],
- "Spdef" => $explodedPokemon[9],
- "Speed" => $explodedPokemon[10],
- ];
- foreach ($singlePokemon as $stat) {
- $pokemonArray[] = $stat;
- }
- }
- return $pokemonArray;
- }
- function createTable($columns) {
- $columnCommand = "CREATE TABLE IF NOT EXISTS pokemon (";
- foreach ($columns as $column) {
- if ($column == $columns[0]) {
- $columnCommand .= $column . " VARCHAR(100) NOT NULL";
- }
- else {
- $columnCommand .= ",\n " . $column . " VARCHAR(100) NOT NULL";
- }
- }
- $columnCommand .= "); \n";
- return $columnCommand;
- }
- function populateTable($pokemonArray, $columns) {
- for ($i = 0; $i < count($pokemonArray); $i++) {
- ${tableCommand . $i} = createCommand($pokemonArray[$i], $columns);
- file_put_contents("../create_pokemon_db.sql", ${tableCommand . $i}, FILE_APPEND);
- }
- }
- function createCommand($pokemon, $columns) {
- $tableCommand = "REPLACE INTO pokemon (";
- foreach ($columns as $column) {
- if ($column == $columns[0]) {
- $tableCommand .= $column;
- } else {
- $tableCommand .= ", " . $column;
- }
- }
- $tableCommand .= ") \n VALUES (";
- foreach ($pokemon as $stat) {
- if ($stat === $pokemon['Pokemon_Name']) {
- $tableCommand .= "'" . $stat . "'";
- } else {
- $tableCommand .= ", '" . $stat . "'";
- }
- }
- $tableCommand .= "); \n";
- return $tableCommand;
- }
- function rewriteTextFile($pokemonArray) {
- foreach ($pokemonArray as $pokemon) {
- $newText[] = implode(",", $pokemon);
- }
- $document = implode("\n", $newText);
- file_put_contents("pokemon_info.txt", $document);
- }
- function loadAssets(){
- $document = trim(file_get_contents("pokemon_info.txt"));
- $assets['list'] = explode("\n", $document);
- $assets['columns'] = explode(',', $assets['list'][0]);
- $assets['columnsQuery'] = createTable($assets['columns']);
- array_shift($assets['list']);
- return $assets;
- }
- extract(loadAssets());
- $pokemonArray = listToArray($list);
- file_put_contents("../create_pokemon_db.sql", $columnsQuery);
- populateTable($pokemonArray, $columns);
- // Note:
- // The function below is for saving any changes you have made to the database contents by modifying the listToArray function
- // This will permenantly alter your pokemon_info.txt
- // This will delete the column names from your pokemon_info.txt
- // >>You MUST manually recreate the list of columns before calling this function a second time<<
- //
- // rewriteTextFile($pokemonArray);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement