Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //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
- //"Predmet.txt" to whatever your file is. In either case, you can put the whole path as a string value and it should work.
- $parsed_file = dirname(__FILE__)."/Predmet.txt";
- echo "Opening ".$parsed_file."\n";
- $parsed = fopen($parsed_file, 'r');
- $line_no = 0;
- $no_collumns = 0;
- echo "Parsing ".$parsed_file."\n";
- while (!feof($parsed))
- {
- $parsed_line = fgets ($parsed);
- if ($parsed_line[0] == '#' ) continue;
- $parsed_words = explode (' ', $parsed_line);
- $parsed_first_word = $parsed_words[0];
- if ($parsed_words[0] === "Database:")
- {
- if (!mysql_connect($parsed_words[1], $parsed_words[2], $parsed_words[3]))
- {
- echo "Could not connect - check your Database: line. ".mysql_error()."\n";
- break;
- }
- if (!mysql_select_db($parsed_words[4]))
- {
- echo "Could not connect - check your Database: line. ".mysql_error()."\n";
- break;
- }
- $no_collumns = $parsed_words[5];
- }
- else if ($parsed_words[0] === "Collumns:")
- {
- $parsed_collumns = $parsed_words;
- $i = 1;
- while ($i<=$no_collumns) {echo "Collumn ".$i." ".$parsed_collumns[$i]."\n"; $i++;}
- $parsed_first_collumn = $parsed_collumns[1];
- }
- else
- {
- echo "Updating table. \n";
- $i = 2;
- while ($i<=$no_collumns)
- {
- $j = $i-1;
- if (!
- mysql_query
- ("UPDATE ResultsPredmet SET ${parsed_collumns[$i]} = '${parsed_words[$j]}' WHERE ${parsed_first_collumn} LIKE '${parsed_first_word}'")
- )
- {echo mysql_error()."\n"; break;}
- // echo "\nUPDATE ResultsPredmet SET ${parsed_collumns[$i]} = '${parsed_words[$j]}' WHERE ${parsed_first_collumn} LIKE \"${parsed_first_word}\"";
- $i++;
- }
- $line_no++;
- }
- }
- echo "Done. \n";
- fclose ($parsed);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement