#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    6
    Rep Power
    0
    Hi

    I need to read a .txt file and sort the value, highest firs, lowest last.

    The .txt file contains the following info on a different row:
    altavista:joe:5
    yahoo:eric:2
    disney:dis:4

    the firs thing i did was reading the first line and split the code by ":" so that
    echo $referrer[0] //would give altavista
    echo $referrer[1] //would give joe
    echo $referrer[2] //would give 5

    I want to sort these lines on the numbers(highest first, lowest last) and print this in a table. How can i do this?
    so what i would like in a table is this

    <table>
    <tr><td>
    altavista</td><td>5</td></tr>
    <tr><td>
    disney</td><td>4</td></tr>
    <tr><td>
    yahoo</td><td>3</td></tr>
    </table>


    Is this possible anyway? i don;t have access to mysql or another database app

    Tnx

    ------------------
    Quote the Dokus:"NeverMore!"
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Annapolis, Maryland US
    Posts
    113
    Rep Power
    16
    There's gotta be a better way to do it than this, but play around with the following code - it will give you a problem if the end number is greater than 9, due to the way array sort functions sort ascii representations of number values (1, 11, 12, 2, 3, 4, 5, 6 etc) ...but it may help you a bit.

    <?
    function mirror($incoming)
    {
    $length=strlen($incoming);
    for($i=$length; $i>=0; $i--)
    $reverse.=substr($incoming, $i, 1);
    return $reverse;
    }

    $contents=file("file.txt");
    for($i=0; $i<count($contents); $i++)
    {
    $newlist[]=mirror($contents[$i]);
    }
    rsort($newlist);
    reset($newlist);

    print"<table border=1>";
    for($i=0; $i<count($newlist); $i++)
    {
    $temp=mirror($newlist[$i]);
    $temparray=split(":", $temp);
    print"<tr>";
    for($j=0; $j<count($temparray); $j++)
    {
    print"<td>$temparray[$j]</td>";
    }
    print"</tr>";
    }
    print"</table>";
    ?>
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    6
    Rep Power
    0
    Thnx, I'll give it a try!

    ------------------
    Quote the Dokus:"NeverMore!"
  6. #4
  7. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    6
    Rep Power
    0
    Damn

    the problem indeed is that when the nr is greater then 9 it's not working good!

    anyone have an idea how to solve this??

    laterz

    ------------------
    Quote the Dokus:"NeverMore!"
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    41
    Rep Power
    15
    It's actually pretty simple. Here's the code (tested and works):

    $filename = "file.txt";
    $fd = fopen ($filename, "r");
    $contents = fread ($fd, filesize ($filename));
    fclose ($fd);

    $lines = explode("n",$contents);
    $count = count($lines);

    $i=0;
    while ($i < $count) {

    $current = explode(":",$lines[$i]);
    $array[$current[2]] = "$current[0]:$current[1]";

    $i++;
    }

    asort($array);
    reset($array);

    print "<table>n";

    while (list($key,$body) = each($array)) {
    $body = explode(":",$body);
    print "<tr>n";
    print "<td>$body[0]</td>n";
    print "<td>$key</td>n";
    print "</tr>n";
    }

    print "</table>n";
  10. #6
  11. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    6
    Rep Power
    0
    Hi

    Thnx for replying Krucifyx but the code didn;t work! or maybe the code is working but im doing something wrong (that would be the problem i think )

    this is the url of the script have a look at it http://www.mycgiserver.com/~gertje/o...html?id=disney (where disney is also could be gert or bas)

    This is the code im using on that page

    <?

    $name = $id;
    $filename = "in.txt";


    $hits = file($filename); //* $hits includes the whole text

    $totaal = count($hits);

    for($i=0 ; $i < $totaal +1 ; $i++)
    {

    $line = $hits[$i]; //* $line includes one line
    $instukjes = split(":" , $line); //* $instukjes[0] include the first string
    //* untill ":" is reached


    if($instukjes[1] == $name)
    {

    $instukjes[2] = $instukjes[2] + 1;
    $regel = $instukjes[0] . ":" . $instukjes[1] . ":" . $instukjes[2] . "n";
    $hits[$i] = $regel;
    }

    }

    $fd = fopen($filename , "w");
    for($b=0 ; $b<count($hits) ; $b++)
    {
    fwrite($fd, $hits[$b]);
    }
    fclose($fd);


    $fd = fopen ($filename, "r");
    $contents = fread ($fd, filesize ($filename));
    fclose ($fd);
    $lines = explode("n",$contents);
    $count = count($lines);
    $i=0;
    while ($i < $count) {
    $current = explode(":",$lines[$i]);
    $array[$current[2]] = "$current[0]:$current[1]";
    $i++;
    }
    asort($array);
    reset($array);
    print "<table>n";
    while (list($key,$body) = each($array)) {
    $body = explode(":",$body);
    print "<tr>n";
    print "<td>$body[0]</td>n";
    print "<td>$key</td>n";
    print "</tr>n";

    }
    print "</table>n";


    ?>


    As you can see it's not sorting the arrays in the proper way

    can u see what is wrong?

    Thnx again!!



    ------------------
    Quote the Dokus:"NeverMore!"
  12. #7
  13. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    6
    Rep Power
    0
    Ok i was told by someone that i should use
    settype to convert the integer value to an string and then sort it!

    But that gives me the output: Array
    and nothing more

    Im desperate!!
    Help is needed
    im gonna cry!!!

    Untill next time, I Remain!

    ------------------
    Quote the Dokus:"NeverMore!"
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Annapolis, Maryland US
    Posts
    113
    Rep Power
    16
    I think I've made some progress on this one...

    <?
    $text=file("file.txt");

    function compare($inval1, $inval2)
    {
    $temparray_2=split(":", $inval2);
    $right=$temparray_2[2];
    $temparray_1=split(":", $inval1);
    $left=$temparray_1[2];
    return $right-$left;
    }

    uasort($text, "compare");

    print"<table border=1 cellpadding=3>";
    while(list($key, $val)=each($text))
    {
    $temp=split(":", $val);
    print"<tr>";
    print"<td>$temp[0]</td>";
    print"<td>$temp[1]</td>";
    print"<td>$temp[2]</td>";
    print"</tr>";
    unset($temp);
    }
    print"</table>";
    ?>

    Hope this helps,

    Kyuzo
  16. #9
  17. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    6
    Rep Power
    0
    YES!!

    thank you sooooo much!! it did work!

    i was trying something myself with this function
    function sort($hits[], $b)
    {

    for($s=0 ; $s < $b-1 ; $s++)
    {
    $lowest = $hits[$s];
    $nroflowest = $s;
    for($i=$s+1 ; $i < $b ; $i++)
    {
    if($hits[$i] < $lowest)
    {
    $lowest = $hits[$i];
    $nroflowest = $i;
    }
    }
    $hits[$nroflowest] = $hits[$s];
    $hits[$s] = $lowest;

    }
    }

    $hits would be the array in wich the nr's are stored and $b is the length of the array

    but did didn;t work! altough i don;t see why!!


    Thnx Again!
    you've maked my life alot easier now!!!



    ------------------
    Until next time, I'll Remain!!
    Quote the Dokus:"NeverMore!"

Similar Threads

  1. sorting arrays of strings using quicksort in C
    By gtpc in forum C Programming
    Replies: 1
    Last Post: December 4th, 2003, 04:08 PM
  2. manipulating char * strings
    By infamous41md in forum C Programming
    Replies: 2
    Last Post: June 1st, 2003, 01:48 AM
  3. strings vs. constant strings (what's the difference?)
    By Matthew Doucette in forum C Programming
    Replies: 8
    Last Post: March 7th, 2003, 02:00 PM
  4. problem sorting strings
    By cgraz in forum PHP Development
    Replies: 4
    Last Post: November 19th, 2002, 12:20 AM
  5. matching identical parts in two strings
    By mchess in forum PHP Development
    Replies: 11
    Last Post: June 14th, 2002, 04:07 AM

IMN logo majestic logo threadwatch logo seochat tools logo