Reversing order of a text list with ksort?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brewthom
    New Member
    • Dec 2006
    • 5

    Reversing order of a text list with ksort?

    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.brewerthomp son.com/classes/sitesee/sites.txt"));

    echo '<ul>';

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

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

    }
    ?>
    [/PHP]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    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:

    Comment

    • xwero
      New Member
      • Feb 2007
      • 99

      #3
      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.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Problem remains that it is not possible (look at the shown url) to distinguish between old and new entries.

        Ronald :cool:

        Comment

        • xwero
          New Member
          • Feb 2007
          • 99

          #5
          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

          Comment

          • brewthom
            New Member
            • Dec 2006
            • 5

            #6
            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.

            Originally posted by xwero
            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

            Comment

            Working...