By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,825 Members | 1,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,825 IT Pros & Developers. It's quick & easy.

Reversing order of a text list with ksort?

P: 5
I'm new to php and am trying to bring in code from a text list in REVERSE order. I understand that I need to use ksort, but apparenlty am not understanding where to use this. The code below works, but what I'd like it to do is list the most recently added items at top. I tried ksort($name) before the echo "a href..." statement. It didn't work. I know I'm missing something. Trying to figure it out. Like I said-- just learning. Thanks in advance.

[PHP]
<?php

$text = (file("http://www.brewerthompson.com/classes/sitesee/sites.txt"));

echo '<ul>';

for($i = 0; $i < count($text); $i++)
{
$name=trim($text[$i]) ;

echo "<li><a href='http://".$name."' target='_blank'>".$name."</a></li>";

}
?>
[/PHP]
Feb 10 '07 #1
Share this Question
Share on Google+
5 Replies


ronverdonk
Expert 2.5K+
P: 4,258
I had a look at that text file and sorting it will not help a great deal because the urls in this list sometimes start with just the domain name, others with www.domainname and others with http://www.domainname.

This list also does not contain any data from which you could deduct what was added last.

Only thing you can do to accomplish this is normalizing the url addresses, adding dates, keeping track of changes, etc. And keep everything on a separate server file to keep track of any changes. That's a lot of effort.

Ronald :cool:
Feb 11 '07 #2

P: 99
ksort sorts the keys but you use the file function so the keys are already sorted.

you have to use krsort to reverse the array.
Feb 11 '07 #3

ronverdonk
Expert 2.5K+
P: 4,258
Problem remains that it is not possible (look at the shown url) to distinguish between old and new entries.

Ronald :cool:
Feb 11 '07 #4

P: 99
i think we can assume the latest urls are near the bottom of the text file.

if not he has to put dates next to the urls like you suggested. he can do it like this

yyyymmddhhmmss|url

and the code to sort them would be
[PHP]
$array = file('url');
$tosort = array();
foreach($array as $line){
$temp = split('|', $line);
$tosort[$temp[0]] = $temp[1];
}
// oldest urls first
$oldtonew = ksort($tosort);
// oldest urls last
$newtoold = krsort($tosort);
[/PHP]

This is only good if the dates are unique. If more flexibility is needed the best thing is to store the urls in a better stuctured file (xml) or a database
Feb 11 '07 #5

P: 5
Yes, I haven't gotten to databases yet, but maybe that's the way to go. Yes- the most recenlty added items are on the bottom. Thanks for your help.

i think we can assume the latest urls are near the bottom of the text file.

if not he has to put dates next to the urls like you suggested. he can do it like this

yyyymmddhhmmss|url

and the code to sort them would be
[PHP]
$array = file('url');
$tosort = array();
foreach($array as $line){
$temp = split('|', $line);
$tosort[$temp[0]] = $temp[1];
}
// oldest urls first
$oldtonew = ksort($tosort);
// oldest urls last
$newtoold = krsort($tosort);
[/PHP]

This is only good if the dates are unique. If more flexibility is needed the best thing is to store the urls in a better stuctured file (xml) or a database
Feb 11 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.