Fredish Posted June 23, 2008 Share Posted June 23, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/111535-solved-need-help-sorting-columns-in-a-textdocument-with-php/ Share on other sites More sharing options...
Jabop Posted June 23, 2008 Share Posted June 23, 2008 That seems overly complicated and convoluted. If you don't mind me asking, why doesn't this server support MySQL? Quote Link to comment https://forums.phpfreaks.com/topic/111535-solved-need-help-sorting-columns-in-a-textdocument-with-php/#findComment-572446 Share on other sites More sharing options...
Fredish Posted June 23, 2008 Author Share Posted June 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111535-solved-need-help-sorting-columns-in-a-textdocument-with-php/#findComment-572453 Share on other sites More sharing options...
.josh Posted June 23, 2008 Share Posted June 23, 2008 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 ***********/ ?> Quote Link to comment https://forums.phpfreaks.com/topic/111535-solved-need-help-sorting-columns-in-a-textdocument-with-php/#findComment-572477 Share on other sites More sharing options...
Barand Posted June 23, 2008 Share Posted June 23, 2008 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/111535-solved-need-help-sorting-columns-in-a-textdocument-with-php/#findComment-572503 Share on other sites More sharing options...
Fredish Posted June 23, 2008 Author Share Posted June 23, 2008 I tried what you said Barand and it worked very great! Thanks a lot man! Quote Link to comment https://forums.phpfreaks.com/topic/111535-solved-need-help-sorting-columns-in-a-textdocument-with-php/#findComment-572556 Share on other sites More sharing options...
Fredish Posted June 26, 2008 Author Share Posted June 26, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/111535-solved-need-help-sorting-columns-in-a-textdocument-with-php/#findComment-574956 Share on other sites More sharing options...
Barand Posted June 26, 2008 Share Posted June 26, 2008 to sort on a string, say, url in $data[1], the sort function would be function urlsort($a, $b) { return strcmp($a[1], $b[1]); } Quote Link to comment https://forums.phpfreaks.com/topic/111535-solved-need-help-sorting-columns-in-a-textdocument-with-php/#findComment-574989 Share on other sites More sharing options...
Fredish Posted June 26, 2008 Author Share Posted June 26, 2008 to sort on a string, say, url in $data[1], the sort function would be function urlsort($a, $b) { return strcmp($a[1], $b[1]); } This worked, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/111535-solved-need-help-sorting-columns-in-a-textdocument-with-php/#findComment-575284 Share on other sites More sharing options...
Fredish Posted June 26, 2008 Author Share Posted June 26, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111535-solved-need-help-sorting-columns-in-a-textdocument-with-php/#findComment-575413 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.