find top five highest value using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • taurus89
    New Member
    • Feb 2010
    • 6

    find top five highest value using php

    hello!!!
    i have five txt file in one folder(cat2t.tx t,cat9t.txt,cat 6t.txt,cat43t.t xt,cat19t.txt)
    and i need to extract data from this file without insert manually...
    so how to find top five highest value in column total and display together with column number in this five txt file..
    i'm stuck because it must find randomly...and all five txt file is different value...
    i don't know how to coding it???? please show me...
    please help me!!! i'm a newbie.

    Number Execs. Total %/Part %/Exe
    ---------------------------------------------------------------
    10 563 4 0.71 0.71
    900 559 11 1.95 1.97
    901 548 1 0.18 0.18
    1000 398 1 0.18 0.25
    1001 397 3 0.53 0.76
    1003 394 5 0.89 1.27
    1006 389 3 0.53 0.77
    1008 386 3 0.53 0.78
    1009 383 3 0.53 0.78
    2003 340 1 0.18 0.29
    2004 339 23 4.09 6.78
    2011 316 1 0.18 0.32
    2503 315 1 0.18 0.32
    3000 314 1 0.18 0.32
    3024 313 23 4.09 7.35
    3025 290 5 0.89 1.72
    3026 285 14 2.49 4.91
    3033 271 6 1.07 2.21
    3500 265 2 0.36 0.75
    3512 263 1 0.18 0.38
    5010 262 3 0.53 1.15
    5019 259 1 0.18 0.39
    5025 258 1 0.18 0.39
    6010 257 4 0.71 1.56
    7001 253 1 0.18 0.40
    7012 252 12 2.13 4.76
    8000 547 97 17.23 17.73
    8001 450 2 0.36 0.44
    8100 448 51 9.06 11.38
    8102 51 50 8.88 98.04
    8207 380 1 0.18 0.26
    8211 379 39 6.93 10.29
    8212 379 39 6.93 10.29
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    You would probably need to to read line by line of your *.txt files using:
    string fgets ( resource $handle [, int $length ] ).
    Each line you would than explode around empty string using:
    array explode ( string $delimiter , string $string [, int $limit ] )

    This would give you an array whose elements you can compare to get the highest value in each position.

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      Or if you have a mysql database, you can do this in two queries; one insert, one select.



      Dan

      Comment

      • taurus89
        New Member
        • Feb 2010
        • 6

        #4
        thank u for reply my message...
        i'm sorry...
        but i can't understand how to do it...
        maybe u can show me to coding it...

        Comment

        • taurus89
          New Member
          • Feb 2010
          • 6

          #5
          thank u for reply...
          but i don't understand...

          Comment

          • dlite922
            Recognized Expert Top Contributor
            • Dec 2007
            • 1586

            #6
            I can't code it for you, but I can guide you. That way you understand what you code.

            First Step:

            Learn to read a file as shown here: http://www.php.net/manual/en/function.file.php

            Second Step:

            Read the file and just print it to a screen for now and see if it works.


            Third Step:

            If you just need to sort by the first number, the Number Execs column you posted above, just use sort http://us.php.net/sort

            if You are sorting by another number, then extract your column by some delimiter, in your case a space is a good delimiter it looks like.

            How do you do it?! easy, php has a function for that: http://php.net/explode

            So for each line of the file, (which now you have in an array by using the file() function), you iterate and explode each item in that array.

            If you're taking the third number to sort, you take the third item from the result of the explode and store it in another array.

            Take that final array and use sort() or rsort() function then take the first five items.

            If you're still lost, attempt the code, tell us where you're lost and we'll show you the next step.

            Happy Coding!



            Dan

            PS: Unlike other forums, we teach PHP, not code it for you ;)

            Comment

            Working...