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
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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.