Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jul 2011
    Location
    USA
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    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?

    http://bybyron.uni.cc/server_data/se..._July_2011.dat

    Thanks!

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Posts
    271
    Thanks
    3
    Thanked 40 Times in 40 Posts
    There might be other ways but this works
    PHP Code:
    <?PHP
    $parts
    =array();
    $file_handle fopen("http://bybyron.uni.cc/server_data/server_data_July_2011.dat""r");
    $ax=1;
    while (!
    feof($file_handle) ) {
    $parts[$ax] = fgets($file_handle);
    $ax=$ax+1;
    }
    fclose($file_handle);
    if(
    $ax>100) {
    for ( 
    $counter $ax-100$counter <= $ax$counter += 1) {
    echo 
    $parts[$counter];
    }
    } else {
    for ( 
    $counter 1$counter <= $ax$counter += 1) {
    echo 
    $parts[$counter];
    }
    }
    ?>
    If you can't stand behind your troops, feel free to stand in front of them
    Semper Fidelis

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    This will display the last $numRecords records of the file.

    PHP Code:
    <?php

    $lines 
    file('test.dat');
    $numRecords 5//number of records from eof to read
    if (count($lines) >= $numRecords) {
        for (
    $i count($lines) - $numRecords$i count($lines); $i++) {
            echo 
    $lines[$i] . '<br />';
        }
    } else {
        for (
    $i 0$i count($lines); $i++) {
            echo 
    $lines[$i] . '<br />';
        }
    }
    ?>
    Last edited by webdev1958; 08-28-2011 at 04:15 AM.

  • #4
    New Coder
    Join Date
    Jul 2011
    Location
    USA
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks guys! Both solutions work great!

  • #5
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    The main difference between the 2 solutions is that the file() function does all the opening, reading and closing of the data file.

  • #6
    New Coder
    Join Date
    Jul 2011
    Location
    USA
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote 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
    Regular Coder
    Join Date
    Jul 2010
    Posts
    271
    Thanks
    3
    Thanked 40 Times in 40 Posts
    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
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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).
    Code:
    #
    N  @;?
    N33@LM
    Nף@[[
    N\@oi
    N  @|w
    NR@
    N)\@
    N@˽
    N̄@
    N)\@
    N33@,NffV@_.NRn@[<NQ@fNR@tN@̞N@۬NQ@N@N\@;NH@LNzT@{9NHz@cN
    C@qNH:@N\B@܍NH@
    N33C@%Nfff@+Nk@LN{@`NG@N@,N\@<Nף@KN  <A`Nq=&AjNH*A{Nz,A	Nq=AN  @%Np@3N@AN@ONz@]N@kNp@zN@NQ@+N( A;N
    ALNQ@\Nz@kN@{N{@N{ANR@N@N\A"NcA0NaA>N   ALNA[N(AiN@+wN)\@;N  @LN5@[NG@pN@|Nq=@NH@Nl@N{@N@N  ANzAN8A-N@<N(AJN@+XN@<fN{>AKtN=
    KAZNMAkN
    #A|NQ(ANffBAN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d,N33@r,N\
    À,N@܎,N@,N(@,NG#B
    ,NQB,Nף$A,,N	A<,N33AL,N
    A\,NAl
    -NA-NQA)-N)\@7-N@E-NAS-NAa-N{Ao-N@}-N̤@-NG@-Nq=@-N@,-NA<-N
    AL-NR@b-N   A|-NHA
    .N,A{.N=
    @.NHJA-.NA<.NAAM.NA\.NAm.NQA|.N
    @.N@.N
    s@/NH@/N@#/NQ@1/N
    A?/N(@M/N=
    A\/Nq=:Aj/Nff"A.x/Nu@</NGq@L/Nף@\/NQ@l/Nq=@|/N=
    @/NR@/NH@/N¥@/N{@0Nף@0N)\o@ 0N@.0N@=0N@K0Npm@,Y0N@<g0N\FAMu0NA\0NAl0N{@|0N0A*0N(A1Nz@,1Nq@,:1N@<H1N{~@KV1N<@\d1NG1@lr1Np-@{1NR.@1Nff?1NR@1N{@1N  @1NH@1NX@1NHz@
    1NH@
    2NQ@,2N)\@<)2N\r@M72Nף@_E2N@lS2NE@|a2N
    3@*2N9@2N@̧2Nq=@ݵ2NG@2NzD@2NE@2NX@2NA,2Nz@<
    3N{.@L3NG@d&3Nz4@p43Nף?|B3N33@P3NQ@l3Nff@z3N@̈3N  `@ޖ3N=
    G@3Np@3N
    ׃@3N(|@3NzT@-3N{^@<3NzD@L3N*@]4NQ8@l4N=
    G@|#4N=
    '@14NR??4NR@M4N%@[4N=
    @i4NG?w4N?4N@4N{@4NY@74N\B@,4N)\_@<4NGa@L4NRN@b4Np@l4N
    c@{5Np@5NH@ 5NG@.5N@<5N)\_@J5Nף@X5N{^@f5N;@t5N@5N{N@5NRN@,5N{~@[5NH@]5Np@o5N{~@|5N=
    3A
    And read with:
    PHP Code:
    <?php

    $sPath 
    './newseek.dat';

    $iToRecord 100;
    $iRecordSize 8;
    date_default_timezone_set('UTC');

    print 
    '<ol>';

    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);
        }
    }
    print 
    '</ol>';
    ?>
    Results in:
    Code:
    <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>
    </ol>
    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.

  • #9
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    $file './newseek.dat';

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •