exze Posted February 5, 2012 Share Posted February 5, 2012 Hello, I am trying to read data from a text file but also adding up the 4th column for example 'name' 'more' '1.00' 'evenmore' so were the 1.00 is I want to add it up is there anyway ? Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/ Share on other sites More sharing options...
SergeiSS Posted February 5, 2012 Share Posted February 5, 2012 It's possible, of course The algorithm is depends on if you have just one string or more strings. Better say, if this string is the last in the file or not the last. The reason is that if this info is the last in the file, you may add new info very easy. If it's not the last info you have to rewrite your file. Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/#findComment-1314782 Share on other sites More sharing options...
exze Posted February 5, 2012 Author Share Posted February 5, 2012 I need it to run through the whole file adding up every 4th section. Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/#findComment-1314785 Share on other sites More sharing options...
SergeiSS Posted February 5, 2012 Share Posted February 5, 2012 I need it to run through the whole file adding up every 4th section. Well... I explain an algorithm as I see it. You need functions fopen(), fgets() (fread), fwrite() and some others. Read your file line by line, add any info and write it in another file. Then remove old file and finally give your new file the name of an old file. In the result your will have that you need. Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/#findComment-1314803 Share on other sites More sharing options...
exze Posted February 5, 2012 Author Share Posted February 5, 2012 Is there anychance you could help or write it? I have no idea what to do :/ Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/#findComment-1314818 Share on other sites More sharing options...
litebearer Posted February 5, 2012 Share Posted February 5, 2012 Rough idea... <?PHP /* set the file name here */ $file = "mydata.txt"; /* put the file into an array */ $lines = file($file); /* count the number of elements in the array */ /* each line from the file is an element of this first array */ $count = count($lines); /* loop thru each line/element of the first array */ for($i=1;$i<$count;$i++) { /* create a temp array of the current line */ /* presumes spaces are being used as delimiters */ $temp = explode(" ", $lines[$i]); /* start adding the desired element of the temp array to your total */ /* note that $x is the number of the element you want added; elements start at 0 (zero) */ $total = $total + $temp[$x]; } echo $total; ?> Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/#findComment-1314820 Share on other sites More sharing options...
exze Posted February 5, 2012 Author Share Posted February 5, 2012 Ok thanks for far this is the format 'date' 'name' 'moreinformation' '1.00' 'ip' 'date' 'name' 'moreinformation' '1.00' 'ip' 'date' 'name' 'moreinformation' '1.00' 'ip' 'date' 'name' 'moreinformation' '1.00' 'ip' first row is the 4th column but then it's an extra 5 columns to the next one :s Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/#findComment-1314822 Share on other sites More sharing options...
litebearer Posted February 5, 2012 Share Posted February 5, 2012 please show an exact replication of your file (4 or 5 lines worth). Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/#findComment-1314825 Share on other sites More sharing options...
exze Posted February 5, 2012 Author Share Posted February 5, 2012 That is it above. Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/#findComment-1314830 Share on other sites More sharing options...
litebearer Posted February 5, 2012 Share Posted February 5, 2012 so every line is LITERALLY EXACTLY the same? date is NOT a real date, 1.00 is always 1.00? I mean an actual copy of the first 4 or 5 lines. Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/#findComment-1314831 Share on other sites More sharing options...
exze Posted February 5, 2012 Author Share Posted February 5, 2012 'January 16, 2012, 7:48 pm' 'Matt' '[email protected]' '1.00' '66.211.170.66' 'January 16, 2012, 7:50 pm' 'Josh' '[email protected]' '0.99' '66.211.170.66' 'January 16, 2012, 8:31 pm' 'Michael' '[email protected]' '1.01' '66.211.170.66' 'January 19, 2012, 7:48 pm' 'I Needa Doc' '[email protected]' '1.00' '66.211.170.66' Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/#findComment-1314838 Share on other sites More sharing options...
exze Posted February 5, 2012 Author Share Posted February 5, 2012 Ah sorry my mistake. Still looking for some help Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/#findComment-1314854 Share on other sites More sharing options...
litebearer Posted February 5, 2012 Share Posted February 5, 2012 Perhaps not the most elegant or efficient way, but it works. Simply replace ezra1.txt with your actual file name. <?PHP /* set the file name here */ $file = "ezra1.txt"; /* put the file into an array */ $lines = file($file); /* count the number of elements in the array. Each line from the file is an element of this first array */ $count = count($lines); /* loop thru each line/element of the first array */ for($i=1;$i<$count;$i++) { /* create a temp array of the current line. Presumes spaces are being used as delimiters */ $temp = explode("'", $lines[$i]); /* start adding the desired element of the temp array to your total */ /* note that $x is the number of the element you want added; elements start at 0 (zero) */ $total = $total + $temp[7]; } echo $total; ?> Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/#findComment-1314855 Share on other sites More sharing options...
exze Posted February 5, 2012 Author Share Posted February 5, 2012 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/#findComment-1314857 Share on other sites More sharing options...
SergeiSS Posted February 6, 2012 Share Posted February 6, 2012 It's not a good idea to use file() in this case. I'd prefer to use fopen()-fgets()/fread()-fwrite()-fclose(). If the size of the file is big you may have a problem with memory. Quote Link to comment https://forums.phpfreaks.com/topic/256469-reading-from-a-text-file/#findComment-1314995 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.