Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. <?php
  2.  
  3. // IMPORTANT!
  4. // Always Verify Table Columns in Text Asset Before Running File!
  5. // Also verify your file paths to ensure these functions call successfully.
  6.  
  7. function listToArray($explodedList) {
  8. function nullChecker($explodedPokemon) {
  9. if ($explodedPokemon[2] !== "") {
  10. return $explodedPokemon[2];
  11. } else {
  12. return "N/A";
  13. }
  14. }
  15. $pokemonArray = [];
  16. foreach ($explodedList as $pokemon) {
  17. $explodedPokemon = explode(',', $pokemon);
  18. $singlePokemon = [];
  19. $singlePokemon[] = [
  20. "Pokemon_Name" => $explodedPokemon[0],
  21. "Pokedex_Number" => $explodedPokemon[1],
  22. "Mega_evolution" => nullChecker($explodedPokemon),
  23. "Type" => $explodedPokemon[3],
  24. "Total" => $explodedPokemon[4],
  25. "Hp" => $explodedPokemon[5],
  26. "Attack" => $explodedPokemon[6],
  27. "Defense" => $explodedPokemon[7],
  28. "Spatk" => $explodedPokemon[8],
  29. "Spdef" => $explodedPokemon[9],
  30. "Speed" => $explodedPokemon[10],
  31. ];
  32. foreach ($singlePokemon as $stat) {
  33. $pokemonArray[] = $stat;
  34. }
  35. }
  36. return $pokemonArray;
  37. }
  38.  
  39. function createTable($columns) {
  40. $columnCommand = "CREATE TABLE IF NOT EXISTS pokemon (";
  41. foreach ($columns as $column) {
  42. if ($column == $columns[0]) {
  43. $columnCommand .= $column . " VARCHAR(100) NOT NULL";
  44. }
  45. else {
  46. $columnCommand .= ",\n " . $column . " VARCHAR(100) NOT NULL";
  47. }
  48. }
  49. $columnCommand .= "); \n";
  50. return $columnCommand;
  51. }
  52.  
  53. function populateTable($pokemonArray, $columns) {
  54. for ($i = 0; $i < count($pokemonArray); $i++) {
  55. ${tableCommand . $i} = createCommand($pokemonArray[$i], $columns);
  56. file_put_contents("../create_pokemon_db.sql", ${tableCommand . $i}, FILE_APPEND);
  57. }
  58. }
  59.  
  60. function createCommand($pokemon, $columns) {
  61. $tableCommand = "REPLACE INTO pokemon (";
  62. foreach ($columns as $column) {
  63. if ($column == $columns[0]) {
  64. $tableCommand .= $column;
  65. } else {
  66. $tableCommand .= ", " . $column;
  67. }
  68. }
  69. $tableCommand .= ") \n VALUES (";
  70. foreach ($pokemon as $stat) {
  71. if ($stat === $pokemon['Pokemon_Name']) {
  72. $tableCommand .= "'" . $stat . "'";
  73. } else {
  74. $tableCommand .= ", '" . $stat . "'";
  75. }
  76. }
  77. $tableCommand .= "); \n";
  78. return $tableCommand;
  79. }
  80.  
  81. function rewriteTextFile($pokemonArray) {
  82. foreach ($pokemonArray as $pokemon) {
  83. $newText[] = implode(",", $pokemon);
  84. }
  85. $document = implode("\n", $newText);
  86. file_put_contents("pokemon_info.txt", $document);
  87. }
  88.  
  89. function loadAssets(){
  90. $document = trim(file_get_contents("pokemon_info.txt"));
  91. $assets['list'] = explode("\n", $document);
  92. $assets['columns'] = explode(',', $assets['list'][0]);
  93. $assets['columnsQuery'] = createTable($assets['columns']);
  94. array_shift($assets['list']);
  95. return $assets;
  96. }
  97.  
  98. extract(loadAssets());
  99. $pokemonArray = listToArray($list);
  100. file_put_contents("../create_pokemon_db.sql", $columnsQuery);
  101. populateTable($pokemonArray, $columns);
  102.  
  103. // Note:
  104. // The function below is for saving any changes you have made to the database contents by modifying the listToArray function
  105. // This will permenantly alter your pokemon_info.txt
  106. // This will delete the column names from your pokemon_info.txt
  107. // >>You MUST manually recreate the list of columns before calling this function a second time<<
  108. //
  109. // rewriteTextFile($pokemonArray);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement