ClownZee Posted December 23, 2007 Share Posted December 23, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/82869-giving-each-row-of-text-a-variable/ Share on other sites More sharing options...
papaface Posted December 23, 2007 Share Posted December 23, 2007 Show us the code you have. Including sample data from the text file. Quote Link to comment https://forums.phpfreaks.com/topic/82869-giving-each-row-of-text-a-variable/#findComment-421467 Share on other sites More sharing options...
ClownZee Posted December 24, 2007 Author Share Posted December 24, 2007 <?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... Quote Link to comment https://forums.phpfreaks.com/topic/82869-giving-each-row-of-text-a-variable/#findComment-422042 Share on other sites More sharing options...
papaface Posted December 24, 2007 Share Posted December 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/82869-giving-each-row-of-text-a-variable/#findComment-422045 Share on other sites More sharing options...
ClownZee Posted December 24, 2007 Author Share Posted December 24, 2007 like i said before im kinda new to this php stuff and just testing stuff out at the moment. where exactly do i add that into my code... sorry for sounding so new but i am lol. thx Quote Link to comment https://forums.phpfreaks.com/topic/82869-giving-each-row-of-text-a-variable/#findComment-422126 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.