Jump to content

[SOLVED] Need help sorting columns in a textdocument with PHP!


Recommended Posts

Hello there!

 

I've been trying to soulve a prob for quite some time now. Hopefully you can help!

 

I am making a site for a bunch of my friends, for them to use on their server. Their server does not support MySQL and it doesn't have a database. Though they still need info in tables, for instance a list of all available downloads. Instead of using a database to store the info in, I stored the info in a textdocument, using an X mark to set the columns. The textdocument looks like this:

 

DATE×URL×USER×DESCRIPTION×HITS×
DATE×URL×USER×DESCRIPTION×HITS×
(etc.)

 

Then, each time a fellow uploads a file another one of these lines is added to the textdocument with the current info, and each time someone downloads the file, the downloaded file's ×HITS× column will be added with one. I then use some php codes to get the info from that textdocument, and explode every X mark so that I know where the column ends etc. Like this:

 

   <table>
   <tr>
   <td colspan="3">All Uploads</td>
   </tr>
   <?php 
   $lines = file( "txts/uploads_info.txt" );

      for( $i = 0; $i < count($lines); $i++ ) { 

      $data = explode( '×', $lines[$i] ); 

      echo "
      <tr>
      <td>".$data[0]."</td>
      <td>".$data[1]."</td>
      <td><a href='user.php?user=".$data[2]."'>".$data[2]."</a></td>
      </tr>"; 

      } 

   ?>
   </table>

 

Now, this would show the contents of all columns 0, 1 and 2 (×DATE×URL×USER×) of the file: txts/uploads_info.txt from the begginning to the end, (and it does). But say I wanted to make a list of top ten downloads, containing the top ten most popular downloads. This list would have to be ordered by the hits that the line has got, by the column ×HITS×. How do I change my current PHP code, into one that would make the outcome become an ordered list, from most hits to least hits?

 

If you don't understand what I mean, the corresponding code to soulve my problem with MySQL would look like this:

 

   $Top_Ten_Downloads = mysql_query( "SELECT * FROM uploads_info ORDER BY HITS DESC LIMIT 0, 10" );

 

Please help me, because I'm stuck here! Remember I'm using a text document to get the info from, not a database!

That seems overly complicated and convoluted. If you don't mind me asking, why doesn't this server support MySQL?

The server admin hasn't bothered installing it yet, and to be honest I'm not very experienced with MySQL so I only know how to run my own database. Anyway, my question stands, and help would be apprechiated. I think you could use sort() on an array with the HITS column, but I can't soulve it. A working code would be so helpful.

though I suggest you look into using xml...

 

<?php
// get contents of file into an array
$lines = file( "data.txt" );

// store your info in a 2d array
foreach($lines as $l) {
  $data = explode( '×', $l); 

  $row[] = array('date' => $data[0],
                       'url'  => $data[1],
                       'user' => $data[2],
                       'desc' => $data[3],
                       'hits' => $data[4]);   
} // end foreach

// function to sort a 2d array by specified element
// taken from http://www.php.net/manual/en/function.sort.php#76547
function msort($array, $id="id", $sort_ascending=true) {
   $temp_array = array();
   while(count($array)>0) {
      $lowest_id = 0;
      $index=0;
      foreach ($array as $item) {
         if (isset($item[$id])) {
            if ($array[$lowest_id][$id]) {
               if ($item[$id]<$array[$lowest_id][$id]) {
                  $lowest_id = $index;
               } // end if
            } // end if
         } // end if
         $index++;
      } // end foreach
      $temp_array[] = $array[$lowest_id];
      $array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1));
   } // end while
   if ($sort_ascending) {
      return $temp_array;
   } else {
      return array_reverse($temp_array);
   } // end else
} // end function

/******example output**********/
print_r($row); // dumps out your current info
echo "<br><br>";

// call msort 
// msort(array, element, boolean);
// true = ascending order, false = descending order
$sorted_by_hits = msort($row, 'hits', false);
print_r($sorted_by_hits); // 
/*******end example output ***********/
?>

try

<?php 
$lines = file( "txts/uploads_info.txt" );

for( $i = 0; $i < count($lines); $i++ ) { 

$linedata[] = explode( '×', $lines[$i] );                   // put data in array

}

function hitsort($a,$b)
{
   		return $b[4] - $a[4] ;
}

usort ($linedata, 'hitsort');                                    // call custom sort

foreach ($linedata as $data)
{
      echo "
      <tr>
      <td>".$data[0]."</td>
      <td>".$data[1]."</td>
      <td><a href='user.php?user=".$data[2]."'>".$data[2]."</a></td>
      </tr>"; 

    } 
?>

try

<?php 
$lines = file( "txts/uploads_info.txt" );

for( $i = 0; $i < count($lines); $i++ ) { 

$linedata[] = explode( '×', $lines[$i] );                   // put data in array

}

function hitsort($a,$b)
{
   		return $b[4] - $a[4] ;
}

usort ($linedata, 'hitsort');                                    // call custom sort

foreach ($linedata as $data)
{
      echo "
      <tr>
      <td>".$data[0]."</td>
      <td>".$data[1]."</td>
      <td><a href='user.php?user=".$data[2]."'>".$data[2]."</a></td>
      </tr>"; 

    } 
?>

 

Hi again! After using this I soulved my problem of sorting hits (a number). But what if I wanted to sort characters? Say I have these columns I want to sort in alphabetical order:

 

hey_01.txt

hus_06.txt

wazuuup?.zip

I_love.jpg

 

I tried changing the function from b[4] to b[x] where x is the column with the names, though it wouldn't sort them! The outcome made no sence at all...

While I'm here I have yet another question.

 

How can I remove [X] in a textdocument? [X] being a specific piece of text. Note that I a texdocument with an unnknown content in it, so I don't know which bytes are the ones which I want to delete.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.