Advertisement
Guest User

Untitled

a guest
Jun 19th, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.13 KB | None | 0 0
  1. <?php
  2.    //If you want to change the file you're parsing, change the $parsed_file value. If it's in the same directory, just change the
  3.    //"Predmet.txt" to whatever your file is. In either case, you can put the whole path as a string value and it should work.
  4.    $parsed_file = dirname(__FILE__)."/Predmet.txt";
  5.  
  6.    echo "Opening ".$parsed_file."\n";  
  7.    $parsed = fopen($parsed_file, 'r');
  8.    $line_no = 0;
  9.    $no_collumns = 0;
  10.    echo "Parsing ".$parsed_file."\n";
  11.    while (!feof($parsed))
  12.    {
  13.      $parsed_line = fgets ($parsed);
  14.      if ($parsed_line[0] == '#' ) continue;
  15.      $parsed_words = explode (' ', $parsed_line);
  16.      $parsed_first_word = $parsed_words[0];
  17.      if ($parsed_words[0] === "Database:")
  18.      {
  19.         if (!mysql_connect($parsed_words[1], $parsed_words[2], $parsed_words[3]))
  20.         {
  21.            echo "Could not connect - check your Database: line. ".mysql_error()."\n";
  22.            break;
  23.         }
  24.         if (!mysql_select_db($parsed_words[4]))
  25.         {
  26.            echo "Could not connect - check your Database: line. ".mysql_error()."\n";
  27.            break;
  28.         }
  29.         $no_collumns = $parsed_words[5];
  30.      }
  31.      else if ($parsed_words[0] === "Collumns:")
  32.         {
  33.            $parsed_collumns = $parsed_words;
  34.            $i = 1;
  35.            while ($i<=$no_collumns) {echo "Collumn ".$i." ".$parsed_collumns[$i]."\n"; $i++;}
  36.            $parsed_first_collumn = $parsed_collumns[1];
  37.         }
  38.     else
  39.     {        
  40.         echo "Updating table. \n";
  41.         $i = 2;
  42.         while ($i<=$no_collumns)
  43.            {
  44.              $j = $i-1;
  45.              if (!
  46.                   mysql_query
  47.                   ("UPDATE ResultsPredmet SET ${parsed_collumns[$i]} = '${parsed_words[$j]}' WHERE ${parsed_first_collumn} LIKE '${parsed_first_word}'")
  48.                 )
  49.              {echo mysql_error()."\n"; break;}
  50.           //   echo "\nUPDATE ResultsPredmet SET ${parsed_collumns[$i]} = '${parsed_words[$j]}' WHERE ${parsed_first_collumn} LIKE \"${parsed_first_word}\"";
  51.              $i++;
  52.              }
  53.      $line_no++;
  54.     }
  55.    }
  56.    echo "Done. \n";
  57.    fclose ($parsed);
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement