Jump to content

Giving each row of text a variable


ClownZee

Recommended Posts

Ok well im working on a gaming clan web site and i need to pull a text file off the gaming server for the ranks.  well i have that done and now im stuck because i dont know how to give each row of text a variable so i can import it into a database.  Can anyone help me find out how to give each row of text a variable..  any help will be great.  im kinda new to PHP so plz help :)

 

thx ahead of time

Link to comment
https://forums.phpfreaks.com/topic/82869-giving-each-row-of-text-a-variable/
Share on other sites

<?php
$i = 1;


$myfile = '/data/ranks.txt';
$command = "tac $myfile > /data/ranks.txt";
passthru($command);
$ic = 0;
$ic_max = 10;  // stops after this number of rows
$handle = fopen("http://e3.swirve.com/data/ranks.txt", "r");
while (!feof($handle) && ++$ic<=$ic_max && $i<=10) {
   $buffer = fgets($handle, 4096);

      if (!feof($handle))
         {echo "$i."  ;
             ++$i;
          echo $buffer."<br>";
         }
}

fclose($handle);

?>

 

and that shows up as

 

1. 109 Mz (#109) 13222 6330814 DepaRted C 0

2. 692 IRONtoTHEtop (#692) 11157 6297646 IRON C 0

3. 785 1st MKR Vol Cavalry (#785) 16045 6208217 MKR C 0

4. 798 Hunters Destroyer (#798) 13398 6010372 OoAoDToO T 0

5. 236 Neos Kraken (#236) 11367 5838920 XNEOFEDX C 0

6. 333 Gafooleyland (#333) 11015 5637699 ELF C 0

7. 234 War (#234) 11319 5586690 DepaRted C 0

8. 1290 Manchester United (#1290) 7387 5566092 H 0

9. 78 my own empire (#78) 19782 5545837 RMortis C 0

10. 1287 Seashelf (#1287) 10901 5475305 ELF C 0

 

now i need to be able to split that up row by row to put into a database...

e.g

$filename = "file.txt";
$lines = file($filename);

foreach($lines as $line_num => $line)
{
mysql_query("insert into `cats` (category) values ('".mysql_real_escape_string($line)."')") or die(mysql_error());
}

That will get every line and insert it into the category field in the cats table.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.