Web Analytics Made Easy -
StatCounter Read Last 100 Entries from a Data File - CodingForum


No announcement yet.

Read Last 100 Entries from a Data File

  • Filter
  • Time
  • Show
Clear All
new posts

  • Read Last 100 Entries from a Data File

    Could somebody please show me how to always read only the last 100 entries from a text data file like this?


  • #2
    There might be other ways but this works
    PHP Code:
    $file_handle fopen("http://bybyron.uni.cc/server_data/server_data_July_2011.dat""r");
    while (!
    feof($file_handle) ) {
    $parts[$ax] = fgets($file_handle);
    $ax>100) {
    for ( 
    $counter $ax-100$counter <= $ax$counter += 1) {
    } else {
    for ( 
    $counter 1$counter <= $ax$counter += 1) {
    If you can't stand behind your troops, feel free to stand in front of them
    Semper Fidelis


    • #3
      This will display the last $numRecords records of the file.

      PHP Code:

      $numRecords 5//number of records from eof to read
      if (count($lines) >= $numRecords) {
          for (
      $i count($lines) - $numRecords$i count($lines); $i++) {
      $lines[$i] . '<br />';
      } else {
          for (
      $i 0$i count($lines); $i++) {
      $lines[$i] . '<br />';
      Last edited by webdev1958; Aug 27, 2011, 11:15 PM.


      • #4
        Thanks guys! Both solutions work great!


        • #5
          The main difference between the 2 solutions is that the file() function does all the opening, reading and closing of the data file.


          • #6
            Originally posted by webdev1958 View Post
            The main difference between the 2 solutions is that the file() function does all the opening, reading and closing of the data file.
            Ok, thank you!


            • #7
              I agree, Webdev1958 showed me something new to save me time.
              If you can't stand behind your troops, feel free to stand in front of them
              Semper Fidelis


              • #8
                Unfortunately as your filesize increases, the probability of exhausting your memory will also increase. There is no real solution to this, but IMO to combat it you would need to use an fopen technique with a line skip and count back in a 100 entry buffer. What a pain.
                If you can actually create this data file though, you could create a random access file instead and use seek. Since you only have the date and a server load, you can do that with a single integer and a single float and parse out the data to display as you see fit. The pro to this is that you now know each record is 64 bits in size, and you can use fseek in combination with 100x the 8bytes, and count backwards.

                So, if I converted these into an strtotime and added a float for the load, I'd end up with a dat file like this (no header, so you may want to consider adding that in).
                N  ˆ@;?
                N  ¸@|w
                N33£@,NffV@_.NR¸n@[<NىQ€@‍fNR¸–@£tN…ë‘@ج‍Nأُ?@غ¬NىQ¨@×N…“@هNحج\@;NHل¢@LNلzT@{9NHلz@?cN
                ¸N33C@%ئNfff@+شN…k@LًN{ژ@ض`N®G?@§Nأُ?@,µN\?؛@<أN×£ط@KرN  <A`كNq=&AjيNHل*A{ûNلz,A?	Nq=AœN  ب@¬%N¤pإ@¼3N?آص@جAN¸¥@غONلz„@ى]N?™±@ükN¤pإ@zN?آ?@ˆNىQب@+–Nِ( A;¤N
                ×AL²NىQà@\?Nلz¼@kخNحجش@{ـN{ض@‹êN{A›ّNR¸¶@«N®·@¼N\?Aج"N®cAـ0N¸aAë>N   AûLN…A[Nِ(AiN¸إ@+wN)\—@;…N  €@L“N¸5@[،N®G‰@p¯Nأُè@|½Nq=¢@?ثNHل²@?ظNحجl@¬çN{¾@¼ُN…ë¹@ثN  AغNلzAىNأُ8Aْ-Nأُ?@<Nِ(AJN®ك@+XN?™é@<fN{>AKtN=
                ×#A|‍NىQ(A‹¬NffBAœ؛N?آُ@«بNأًُ@»ضN®AخنNىQAغٍNq=JAë NىQXAüN)\ANHلٍ@+NأُA+9N®Gù@;GNأُ(AKUN  üA\cN  œAkqN…ë³A{N=
                ×@¬©N®Gل?¼·N®ç?ثإNىQX@غسN×£P@ëلNلzT@üïNffژ@‏Nq=ANq=²@,N33ƒ@=(Nلzج@L6N…£@[DNffv@lRN  ط@‚`N…£@?nNحجج@œ|N  ?@¬?N=
                '@¼?N?آE@ج¦N33#@ـ´NأُX@ىآNأُH@üذN\?2@كNأُ(@يN…ë1@,ûN®@<	N
                ׋@LNR¸‍@\%N®Gل@l3Nِ(L@|AN  ¨@›ON3sWC?Nfff?!خN…ëر?.ـN)\¯??êNحجج?LّNq=ê?\NR¸@kNq=ê?|"N®Gء??0Nلz$@œ>N…ë±?«LN×£ذ?»ZN  ??جhN¸¥?ـvN¸إ?ى„NىQ¸?ü’N=
                @يeNىQ¸?‎sN  à?‚N…ëر??N
                ×م?,‍N\?آ?;¬NىQط?L؛N×£ذ?]بN…ëر?kضNq=ت?|نNffئ?ŒٍNffو?› N?آُ?¬Nff@»NR¸@ج*NR¸~@ـ8N  @@ىFN¸5@üTN¸@cNq=Z@qN…ë‰@-NR¸@<?N33#@M›NلzD@\©N…‹@l·N=
                @}إN)\@ŒسNِ(ـ?œلN…ëٌ?¬ïN®Ga@¼‎N®G¹@جN33³@غNHل?@ى'N)\§@û5NىQ?@DNHلê@RNلzü@-`NR¸NA<nNq=A`?N{¦@n?Nلz´@|¦N?™™@Œ´N…ƒ@œآNR¸n@«ذN…ëA@½قN?آE@جىN…ƒ@ـْNffF@ىN…{@üNHلZ@%Nأُ8@3Nِ(¤@,AN?آ*@<ON®Ga@]kNR¸n@myNِ(œ@?•N)\؟@œ£N{^@¬±NىQˆ@¼؟Nأُ€@جحNلzœ@غغNحج”@ىéN…ë?@‏÷N¸…@Nq=?@N¤p•@,"N×£p@<0NلzŒ@M>N®w@]LNq=Z@lZN¸…@|hN×£P@ŒvNq=‚@œ„N…+@*’N?آE@¼*N…‹@ح®N¤p…@ـ¼N…ëq@ëتN  ?@üطN  ?@
                '@,نN…ë!@=ٍN¸µ@K N=
                ·@]NR¸AmN®#A|*N?آ½@Œ8Nحجü@œFN  *@¬TNلz„@»bNلz”@جpN…k@ف~NR¸‍@ىŒN¸?@ü?NffV@©N?آµ@·N…[@/إNِ(\@<سN…ë©@LلN=
                ×C@?Nأُ?@?N{~@,¦NR¸¶@<´Nq=آ@LآNR¸¶@]ذN®·@sقN×£*@ىN)\?@?ْN¸u@? N{>@¬ N?آ?@¼$ N¤p}@ث2 N{~@ـ@ N{>@يN N{ژ@
                k N\?b@y N=
                '@,‡ N)\?@<• NىQ@L£ N®@]± Nلzô?l؟ N?™)@|ح N…ëA@Œغ N=
                w@?é N?آ%@¬÷ Nِ(l@¼!NR¸~@ج!N33“@ـ!!N  ?@ى/!Nأُˆ@ü=!N®G1@L!Nq=Z@Z!N  ذ@,h!NىQ*@<v!NR¸^@N„!NR¸.@]’!NلzŒ@l*!NHلْ?|®!N…ë?Œ¼!Nلzش?œت!Nffو?«ط!NHل
                @¼و!N®@جô!Nأُè?ـ"N\?@ë"N®@û"NR¸ق?-"N®G،@;"NىQ@,I"N33S@<W"N×£@@Le"N33C@_s"N=
                g@m?"NىQ8@}?"N  ¬B‹?"N¤p•@¬¹"N{@¼ا"N?™9@جص"N=
                #N×£€@#N?™‘@+*#N{A<8#Nff‏@LF#NR¸NA\T#N¸QAlb#N33A|p#NR¸6AŒ~#N®çA¦Œ#Nِ(¤@°?#NىQ8@؟¨#N\?B@خ¶#NحجL@àؤ#N…أ@èز#NHل؛@‎#N\?ع@-$N)\؟@<$N…م@M'$Nq=آ@]5$N\??@mC$Nلzن@|Q$N¤p‎@?_$N?آه@*m$NحجA±{$N)\÷@¼‰$N  Aج—$NR¸Aف¥$N?™	Aى³$N®د@ûء$NR¸A
                ذ$N×£0Aق$Nحجج@+ى$Nِ(œ@<ْ$N×£?@L%N¤p½@^%N®ا@m$%N)\§@|2%N33‹@?@%Nحجü@œN%N\?A¬\%N…A¼j%Nq=ê@جx%N…ëٌ@ف†%NىQAي”%N¸‎@ü¢%N®·@±%Nحجü@؟%N®?@+ح%N®ï@=غ%N\?؛@Lé%N\??@]÷%Nلz¬@l&N\?¢@|&N\?ْ@Œ!&N=
                Aœ/&N?™¹@¬=&NىQط@¼K&N{AدY&N…ëAفg&N?آه@ىu&N?آي@‎ƒ&N×£A’&N33س@*&N¸A,®&NR¸ض@B¼&N¤pARت&N…ث@cط&N=
                ï@¬'N¤p	A»,'N…ëظ@ح:'N?™ر@فH'N®؟@ىV'Nلzن@ûd'N=
                ·@s'NHلâ@?'N=
                ¯@5)NىQˆ@C)N?™y@,Q)N?™?@<_)N=
                ·@,2*N  ب@<@*Nq=ê@KN*Nلzü@\\*N  ب@lj*N  ّ@|x*NR¸¦@Œ†*N®G¹@›”*N=
                W@«¢*NHل¢@¼°*N  ?@ج¾*NىQH@فج*N)\?@ëع*N=
                W@ûè*N®G@÷*N  à@+N)\_@++NR¸N@<!+Nِ(,@K/+N?آE@^=+N?™jBlK+N\?²@{Y+N¸…@¬ƒ+NHلع@»‘+N{ِ@ث?+NأُAـ*+Nِ(ؤ@ى»+N…'Aüة+N33Aط+Nلzـ@و+N33ث@-ô+N{ِ@<,N{ِ@\,Nأُ*@l,,Nحجن@|:,N…ëAAŒH,Nحج?A›V,NR¸ئ@±d,N33غ@¾r,N\?
                ¹,NىQBا,N×£$A,ص,N?آ	A<م,N33ALٌ,N…ë
                -NحجA-NىQAŒ)-N)\÷@£7-N?™¹@¬E-N®AآS-N¸Aجa-N{Aكo-Nأُ°@ى}-Nحج¤@‎‹-N®Gة@?-Nq=¢@¨-N?™‰@,¶-N…A<ؤ-N
                ×ALز-NR¸î@bà-N   A|ü-NHلAŒ
                .Nأُ,A{.N=
                A\/Nq=:Aj/Nff"A.x/N¸u@<†/N®Gq@L”/N×£°@\¢/NىQà@l°/Nq=¢@|¾/N=
                ا@Œج/NR¸‍@œع/NHل?@¬è/N?آ¥@¾ِ/N{¶@ج0N×£€@ف0N)\o@ى 0N…ë‰@ü.0Nأُ?@=0N?آف@K0N¤pm@,Y0N?آي@<g0N\?FAMu0NأُA\ƒ0N…ëAl‘0N{ِ@|?0Nحج0AŒ*0Nِ(جA1Nلz@,1N…ëq@,:1N?™‰@<H1N{~@KV1Nحج<@\d1N®G1@lr1N¤p-@{€1NR¸.@Œژ1Nffو?œœ1NR¸@¬?1N{‍@½¸1N  ¸@ـش1NHلْ@يâ1NأُX@üً1NHلz@
                ×3@*‹2N?™9@½™2Nأُ@ج§2Nq=²@فµ2N®G?@ىأ2NلzD@‎ر2N¸E@à2NأُX@î2NحجA,ü2Nلzن@<
                3N{.@L3N®G@d&3Nلz4@p43N×£ً?|B3N33@ŒP3NىQ@¬l3Nff†@¼z3N?™ر@جˆ3N  `@ق–3N=
                ׃@ء3Nِ(|@د3NلzT@-ف3N{^@<ë3NلzD@Lù3N¸*@]4NىQ8@l4N=
                @حi4N®Gل?ـw4N®ç?ى…4N?آ@ü“4N{@¢4N?™Y@7°4N\?B@,¾4N)\_@<ج4N®Ga@Lع4NR¸N@bè4N¤p@lِ4N
                ×c@{5N¤p…@Œ5NHل’@œ 5N®G‰@¬.5N…ƒ@¼<5N)\_@جJ5N×£?@ـX5N{^@ëf5N…;@üt5N…ë@ƒ5N{N@‘5NR¸N@,?5N{~@[»5NHل¢@]ة5N¤p?@o×5N{~@|ه5N=
                And read with:
                PHP Code:


                $iToRecord 100;
                $iRecordSize 8;


                if (
                $fh fopen($sPath'r'))
                fseek($fh, -($iToRecord $iRecordSize), SEEK_END);
                    while (
                $sRecord fread($fh$iRecordSize))
                $aUnpacked unpack('idate/fload'$sRecord); 
                $iTime $aUnpacked['date'];
                $fLoad $aUnpacked['load'];
                printf('<li class="%s">%s | Server Load:*%0.2f</li>' PHP_EOLdate('D'$iTime), date('D jS M, h:i:s a'$iTime), $fLoad);
                Results in:
                <ol><li class="Wed">Wed 27th Jul, 08:30:04 am | Server Load:*4.96</li>
                <li class="Wed">Wed 27th Jul, 09:30:04 am | Server Load:*5.34</li>
                <li class="Wed">Wed 27th Jul, 10:30:04 am | Server Load:*5.18</li>
                <li class="Wed">Wed 27th Jul, 11:30:06 am | Server Load:*5.69</li>
                <li class="Wed">Wed 27th Jul, 12:30:04 pm | Server Load:*4.02</li>
                <li class="Wed">Wed 27th Jul, 01:30:05 pm | Server Load:*3.74</li>
                <li class="Wed">Wed 27th Jul, 02:30:04 pm | Server Load:*4.31</li>
                <li class="Wed">Wed 27th Jul, 03:30:04 pm | Server Load:*4.53</li>
                <li class="Wed">Wed 27th Jul, 04:30:04 pm | Server Load:*6.93</li>
                <li class="Wed">Wed 27th Jul, 05:30:04 pm | Server Load:*3.71</li>
                <li class="Wed">Wed 27th Jul, 06:30:04 pm | Server Load:*7.43</li>
                <li class="Wed">Wed 27th Jul, 07:30:04 pm | Server Load:*12.41</li>
                <li class="Wed">Wed 27th Jul, 08:30:05 pm | Server Load:*8.31</li>
                <li class="Wed">Wed 27th Jul, 09:30:04 pm | Server Load:*9.87</li>
                <li class="Wed">Wed 27th Jul, 10:30:04 pm | Server Load:*7.69</li>
                <li class="Wed">Wed 27th Jul, 11:30:04 pm | Server Load:*11.05</li>
                <li class="Thu">Thu 28th Jul, 12:30:04 am | Server Load:*25.52</li>
                <li class="Thu">Thu 28th Jul, 08:30:04 am | Server Load:*2.07</li>
                <li class="Thu">Thu 28th Jul, 09:30:05 am | Server Load:*3.78</li>
                <li class="Thu">Thu 28th Jul, 10:30:04 am | Server Load:*4.30</li>
                <li class="Thu">Thu 28th Jul, 11:30:04 am | Server Load:*3.97</li>
                <li class="Thu">Thu 28th Jul, 12:30:03 pm | Server Load:*2.95</li>
                <li class="Thu">Thu 28th Jul, 01:30:04 pm | Server Load:*2.77</li>
                <li class="Thu">Thu 28th Jul, 02:30:04 pm | Server Load:*2.71</li>
                <li class="Thu">Thu 28th Jul, 03:30:03 pm | Server Load:*2.73</li>
                <li class="Thu">Thu 28th Jul, 04:30:04 pm | Server Load:*1.80</li>
                <li class="Thu">Thu 28th Jul, 05:30:04 pm | Server Load:*2.23</li>
                <li class="Thu">Thu 28th Jul, 06:30:04 pm | Server Load:*4.94</li>
                <li class="Thu">Thu 28th Jul, 07:30:05 pm | Server Load:*5.75</li>
                <li class="Thu">Thu 28th Jul, 09:30:04 pm | Server Load:*7.84</li>
                <li class="Thu">Thu 28th Jul, 10:30:05 pm | Server Load:*3.39</li>
                <li class="Thu">Thu 28th Jul, 11:30:04 pm | Server Load:*3.92</li>
                <li class="Fri">Fri 29th Jul, 12:30:05 am | Server Load:*4.59</li>
                <li class="Fri">Fri 29th Jul, 01:30:05 am | Server Load:*2.38</li>
                <li class="Fri">Fri 29th Jul, 02:30:04 am | Server Load:*2.49</li>
                <li class="Fri">Fri 29th Jul, 03:30:04 am | Server Load:*3.79</li>
                <li class="Fri">Fri 29th Jul, 04:30:05 am | Server Load:*4.52</li>
                <li class="Fri">Fri 29th Jul, 05:30:07 am | Server Load:*4.49</li>
                <li class="Fri">Fri 29th Jul, 06:30:04 am | Server Load:*3.08</li>
                <li class="Fri">Fri 29th Jul, 07:30:04 am | Server Load:*2.81</li>
                <li class="Fri">Fri 29th Jul, 10:30:05 am | Server Load:*2.90</li>
                <li class="Fri">Fri 29th Jul, 11:30:05 am | Server Load:*2.39</li>
                <li class="Fri">Fri 29th Jul, 12:30:04 pm | Server Load:*5.57</li>
                <li class="Fri">Fri 29th Jul, 01:30:05 pm | Server Load:*4.04</li>
                <li class="Fri">Fri 29th Jul, 02:30:04 pm | Server Load:*3.07</li>
                <li class="Fri">Fri 29th Jul, 03:30:05 pm | Server Load:*3.08</li>
                <li class="Fri">Fri 29th Jul, 04:30:12 pm | Server Load:*3.39</li>
                <li class="Fri">Fri 29th Jul, 05:30:07 pm | Server Load:*8.80</li>
                <li class="Fri">Fri 29th Jul, 06:30:04 pm | Server Load:*7.14</li>
                <li class="Fri">Fri 29th Jul, 07:30:04 pm | Server Load:*2.72</li>
                <li class="Fri">Fri 29th Jul, 08:30:04 pm | Server Load:*2.02</li>
                <li class="Fri">Fri 29th Jul, 09:30:12 pm | Server Load:*2.82</li>
                <li class="Fri">Fri 29th Jul, 10:30:08 pm | Server Load:*1.88</li>
                <li class="Fri">Fri 29th Jul, 11:30:04 pm | Server Load:*2.05</li>
                <li class="Sat">Sat 30th Jul, 12:30:04 am | Server Load:*2.38</li>
                <li class="Sat">Sat 30th Jul, 02:30:04 am | Server Load:*4.20</li>
                <li class="Sat">Sat 30th Jul, 03:30:04 am | Server Load:*6.55</li>
                <li class="Sat">Sat 30th Jul, 04:30:04 am | Server Load:*3.50</li>
                <li class="Sat">Sat 30th Jul, 05:30:06 am | Server Load:*3.11</li>
                <li class="Sat">Sat 30th Jul, 06:30:04 am | Server Load:*5.17</li>
                <li class="Sat">Sat 30th Jul, 07:30:04 am | Server Load:*4.12</li>
                <li class="Sat">Sat 30th Jul, 08:30:04 am | Server Load:*3.94</li>
                <li class="Sat">Sat 30th Jul, 09:30:05 am | Server Load:*3.32</li>
                <li class="Sat">Sat 30th Jul, 10:30:05 am | Server Load:*3.47</li>
                <li class="Sat">Sat 30th Jul, 11:30:04 am | Server Load:*3.07</li>
                <li class="Sat">Sat 30th Jul, 12:30:04 pm | Server Load:*5.41</li>
                <li class="Sat">Sat 30th Jul, 01:30:05 pm | Server Load:*2.88</li>
                <li class="Sat">Sat 30th Jul, 02:30:04 pm | Server Load:*3.11</li>
                <li class="Sat">Sat 30th Jul, 03:30:04 pm | Server Load:*2.61</li>
                <li class="Sat">Sat 30th Jul, 04:30:07 pm | Server Load:*1.99</li>
                <li class="Sat">Sat 30th Jul, 05:30:06 pm | Server Load:*2.48</li>
                <li class="Sat">Sat 30th Jul, 06:30:04 pm | Server Load:*2.59</li>
                <li class="Sat">Sat 30th Jul, 07:30:05 pm | Server Load:*2.11</li>
                <li class="Sat">Sat 30th Jul, 08:30:05 pm | Server Load:*1.76</li>
                <li class="Sat">Sat 30th Jul, 09:30:04 pm | Server Load:*1.81</li>
                <li class="Sat">Sat 30th Jul, 10:30:04 pm | Server Load:*2.34</li>
                <li class="Sat">Sat 30th Jul, 11:30:04 pm | Server Load:*2.22</li>
                <li class="Sun">Sun 31st Jul, 12:30:04 am | Server Load:*3.40</li>
                <li class="Sun">Sun 31st Jul, 01:30:31 am | Server Load:*3.04</li>
                <li class="Sun">Sun 31st Jul, 02:30:04 am | Server Load:*3.49</li>
                <li class="Sun">Sun 31st Jul, 03:30:04 am | Server Load:*3.52</li>
                <li class="Sun">Sun 31st Jul, 04:30:04 am | Server Load:*3.23</li>
                <li class="Sun">Sun 31st Jul, 05:30:10 am | Server Load:*2.46</li>
                <li class="Sun">Sun 31st Jul, 06:30:04 am | Server Load:*3.56</li>
                <li class="Sun">Sun 31st Jul, 07:30:03 am | Server Load:*4.17</li>
                <li class="Sun">Sun 31st Jul, 08:30:04 am | Server Load:*4.59</li>
                <li class="Sun">Sun 31st Jul, 09:30:04 am | Server Load:*4.29</li>
                <li class="Sun">Sun 31st Jul, 10:30:04 am | Server Load:*4.11</li>
                <li class="Sun">Sun 31st Jul, 11:30:04 am | Server Load:*3.49</li>
                <li class="Sun">Sun 31st Jul, 12:30:04 pm | Server Load:*4.77</li>
                <li class="Sun">Sun 31st Jul, 01:30:04 pm | Server Load:*3.47</li>
                <li class="Sun">Sun 31st Jul, 02:30:03 pm | Server Load:*2.93</li>
                <li class="Sun">Sun 31st Jul, 03:30:04 pm | Server Load:*2.28</li>
                <li class="Sun">Sun 31st Jul, 04:30:04 pm | Server Load:*3.22</li>
                <li class="Sun">Sun 31st Jul, 05:30:05 pm | Server Load:*3.23</li>
                <li class="Sun">Sun 31st Jul, 06:30:04 pm | Server Load:*3.97</li>
                <li class="Sun">Sun 31st Jul, 08:30:19 pm | Server Load:*5.09</li>
                <li class="Sun">Sun 31st Jul, 09:30:05 pm | Server Load:*4.92</li>
                <li class="Sun">Sun 31st Jul, 10:30:07 pm | Server Load:*3.97</li>
                <li class="Sun">Sun 31st Jul, 11:30:04 pm | Server Load:*11.19</li>
                Nice. I don't know whether it was the c&p or if it was the forums here, but there is no linefeeds between these.

                One other benefit is that the packed file is 6kb versus the original @ 49kb.

                I love random access files.
                PHP Code:
                header('HTTP/1.1 420 Enhance Your Calm'); 
                Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


                • #9
                  PHP Code:
                  $file './newseek.dat';

                  $lines file$fileFILE_IGNORE_NEW_LINES );
                  $lines array_slice$lines, -100 );