www.webdeveloper.com
Results 1 to 6 of 6

Thread: Sorting a php .txt file in numerical order

  1. #1
    Join Date
    Mar 2012
    Posts
    6

    Sorting a php .txt file in numerical order

    I'm trying to create a scoring system using flat file (I know, I'd prefer to use mysql too). I have managed to create enough so that the variables are pulled out of the game (username and score) and put into said text file. I can also output this text file in a formatted table. However i cannot get the scores to sort in any order - ideally i want descending. however this doesn't seem to. I know its probably a stupid mistake somewhere but i could do with a fresh pair of eyes to look over it.
    Just to stress, it MUST be flat file, cannot use any form of sql.
    This is the code snippet that looks right to me, but obviously isn't as its not doing what i need it to do.

    Code:
      $filename = file("./highscores.txt");
          sort($filename);
          file_put_contents("./highscores.txt", implode($filename));
    The full page...

    Code:
    <?php
    echo '<table width="5%" border="5"><tr><th>Your Score</th><th>Your Name</th></tr>';
    echo '<tr><td>'.$_POST["m_score"].'</td><td>'.$_POST["name"].'</td></tr>';
    echo '</table>';
    echo "<br />";
    
    
    $data=$_POST["m_score"].",".$_POST["name"].",";
          $filename = "./highscores.txt";
          $handle = fopen($filename, "a");
    
          if (!$handle)
          {
               print "There were problems opening the file";
               exit();
          }
    
          fwrite($handle, $data);
          fclose($handle);
    
          $handle = fopen($filename, "r");
          $datain = fread($handle, filesize($filename));
          $value = explode(",",$datain);        
          $row = floor(count($value)/2);
          $count = 0;
    
    
          $filename = file("./highscores.txt");
          sort($filename);
          file_put_contents("./highscores.txt", implode($filename));
    
    
        echo '<table width="5%" border="1"><tr><th>Top Scores</th><th>Top Users</th></tr>';
        for ($i=0;$i<$row;$i++)
        {
            echo '<tr>';
            for($x=0;$x<2;$x++){
                echo "<td>$value[$count]</td>";
                $count++;
            }
            echo "</tr>";
        }
        echo '</table>';
    
        fclose($handle);
    
    ?>
    Many thanks, Harry

  2. #2
    Join Date
    Mar 2012
    Posts
    6
    219 views and no response? Anybody need anymore details or got questions to ask? Or am i just screwed,lol

  3. #3
    Join Date
    Mar 2011
    Posts
    1,139
    Try:
    sort($filename, SORT_NUMERIC);
    Rick Trethewey
    Rainbo Design

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,322
    Without knowing what is actually in the file and what part of each line should drive the ordering, it's hard to know exactly how it should be sorted. Without that info, my best guess would be to use natsort() or natcasesort().
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Mar 2012
    Posts
    6
    Quote Originally Posted by rtrethewey View Post
    Try:
    sort($filename, SORT_NUMERIC);
    Didn't work

    Quote Originally Posted by NogDog;
    Without knowing what is actually in the file and what part of each line should drive the ordering, it's hard to know exactly how it should be sorted. Without that info, my best guess would be to use natsort() or natcasesort(). ;
    The text file looks like this at the moment:

    13,test,15,test1,1,test2,

    I have tried natsort() and again, no effect. I've hunted around for information everywhere and some people seem to suggest i should be able to sort as it is using natsort etc whereas others suggestions imply i should be placing this in an array of sorts. Just wondered if one of you guys could clarify. In all honesty i am at this point but well and truely out of my depth now..
    Cheers

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,322
    So, how is it supposed to sort: just on the first numeric value ("13" in your example)?

    Or perhaps you should be reading it with fgetcsv() if you need to break it up into fields based on the commas?

    Or maybe none of the above?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles