Web Analytics Made Easy -
StatCounter Reversing order of files loaded - CodingForum

Announcement

Collapse
No announcement yet.

Reversing order of files loaded

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reversing order of files loaded

    Hey everyone,

    I have a CMS that creates an XML file for each article submitted. I use the following code to display a list of the articles with their headers etc but have two changes I would like to make to it.

    Firstly, how can I reverse the order they are loaded? Currently it loads the most recently created article first but I would like this the other way around.

    Secondly, is there a way I can set it to load from a certain number? So for example, it skips the first 12 then loads the next 5?

    PHP Code:
    <?php

    function extractText($array){
        if(
    count($array) <= 1){
            for (
    $i 0$i<count($array); $i++){
                
    $node $array[$i];
                
    $value $node->get_content();
            }
            return 
    $value;
        } 
    }    
    error_reporting(0);
    ?>



     <?php
    $dh 
    opendir('./xml/');

    $fileCount 0;
    while (
    $file readdir($dh) and $fileCount 5){
        if (
    eregi("^..?$"$file)) {
            continue;
        }
        
    $open "./xml/" $file;
        
    $xml domxml_open_file($open);
        
    $root $xml->root();
        
    $stat_array $root->get_elements_by_tagname("status");
        
    $status extractText($stat_array);
        
        
    $r_array $root->get_elements_by_tagname("regarding");
        
    $regarding extractText($r_array);
        
        
    $ab_array $root->get_elements_by_tagname("abstract");
        
    $abstract extractText($ab_array);

        
    $h_array $root->get_elements_by_tagname("headline");
        
    $headline extractText($h_array);

        if (
    $status != "live"){
            continue;
        }
        
        echo 
    "<tr><td><table>";
        echo 
    "<tr align=left valign=top><td width='90' align='center'>";
        echo 
    "<img src='images/newscat/".$regarding ".png'></td><td>";
        echo 
    "<a class='headline' href=\"showArticle.php?file=".$file "\">".$headline "</a><br>";
        echo 
    "<span class='bytext'>Posted: " date ("F d Y H:i"filemtime($open));
        echo 
    "</span><br>" $abstract;
        echo 
    "</td></tr></table></td></tr>";
        
        
    $fileCount++;
    }
    ?>
    Thanks

  • #2
    Looks like your using readdir() to cycle through the files. The documentation for that function says "The filenames are returned in the order in which they are stored by the filesystem" so it doesn't sound like using that function is an effective way to do what you want. http://ca3.php.net/manual/en/function.readdir.php

    Is a record of the articles in a database somewhere? That would make things much easier.

    I suppose you could use readdir() to read in the entire directory contents to an array then manipulate it to get what you want, but a database would be best.

    Comment


    • #3
      Originally posted by >ssp-cdr< View Post
      Looks like your using readdir() to cycle through the files. The documentation for that function says "The filenames are returned in the order in which they are stored by the filesystem" so it doesn't sound like using that function is an effective way to do what you want. http://ca3.php.net/manual/en/function.readdir.php

      Is a record of the articles in a database somewhere? That would make things much easier.

      I suppose you could use readdir() to read in the entire directory contents to an array then manipulate it to get what you want, but a database would be best.

      Unfortunately I'm not using a database for this at the moment. I know that it would be far far better and I am trying to get my head around MySQL at the moment but for the sake of this project I need to get it functional asap.

      Could you suggest how I may go about using a manipulated array?

      Thanks

      Comment


      • #4
        Why not use array_reverse()?

        PHP Code:
        $reversed_array array_reverse($old_array); 
        Last edited by Jinxy; Aug 23, 2011, 11:41 AM.

        Comment


        • #5
          A lot of that code can be simplified using glob().

          For your other requests, have a look at array_multisort() and array_slice():
          PHP Code:
          // Grab all XML files
          $files glob'./xml/*.xml' );

          // Sort oldest to newest
          array_multisort(
              
          array_map'filemtime'$files ),
              
          SORT_ASC,
              
          SORT_NUMERIC,
              
          $files
          );

          // Skip the first 12 and save the next 5
          $files array_slice$files12);

          // What do we have?
          print_r$files ); 
          ZCE

          Comment


          • #6
            Originally posted by kbluhm View Post
            A lot of that code can be simplified using glob().

            For your other requests, have a look at array_multisort() and array_slice():
            PHP Code:
            // Grab all XML files
            $files glob'./xml/*.xml' );

            // Sort oldest to newest
            array_multisort(
                
            array_map'filemtime'$files ),
                
            SORT_ASC,
                
            SORT_NUMERIC,
                
            $files
            );

            // Skip the first 12 and save the next 5
            $files array_slice$files12);

            // What do we have?
            print_r$files ); 
            Unfortunately this doesn't appear to work :-\ It simply returns:
            Array()

            Comment


            • #7
              Did you just plug in the code as written? It is tested and working as expected.

              Answer me two questions:

              1. What are the naming conventions of the XML files and their location relative to the current script?

              If the script above is located in /public_html, it is looking for files in /public_html/xml with the extension .xml

              2. Are there at least 13 XML files (you'd mentioned skipping the first 12 and only displaying the next 5)?
              Last edited by kbluhm; Aug 31, 2011, 10:24 PM.
              ZCE

              Comment


              • #8
                Ok, I haven't quite figured out why that isn't working, however, I don't think it would do what I need to anyway. As I don't want to post the whole content of each .xml file and I want to add structuring to certain parts as I do it (as in the latter part of my op).

                What I'm now wondering is if it would work if I were to find a way to open the directory and assign it to a var, then set that into an array, reverse the order of that array then pass that into the var that is accessed by the readdir?

                Comment


                • #9
                  Originally posted by kbluhm View Post
                  Did you just plug in the code as written? It is tested and working as expected.

                  Answer me two questions:

                  1. What are the naming conventions of the XML files and their location relative to the current script?

                  If the script above is located in /public_html, it is looking for files in /public_html/xml with the extension .xml

                  2. Are there at least 13 XML files (you'd mentioned skipping the first 12 and only displaying the next 5)?
                  Hey,

                  This is what I'm using:

                  Code:
                  $files = glob( './xmlnews/*.xml' ); 
                  array_multisort( 
                      array_map( 'filemtime', $files ), 
                      SORT_ASC, 
                      SORT_NUMERIC, 
                      $files 
                  ); 
                  $files = array_slice( $files, 1, 2 ); 
                  print_r( $files );
                  I had forgotten to adjust the rout before but its pointing to the right directory now. I have three files so this should emit the first and display the contents of the second two, however, it is simply printing the names of the two files:

                  Code:
                  Array ( [0] => ./xmlnews/testarticle03.xml [1] => ./xmlnews/testarticle01.xml )
                  rather than the contents (which as explained in my previous post isn't what I really need anyway. Any ideas at all?

                  Thanks

                  Comment


                  • #10
                    Ok, so what I basically need to figure out the syntax for is this:

                    - Grab the names of each file in said directory
                    - Set them into an array
                    - Reverse the order of the array
                    - Call a function which extracts specific elements from the file relative to the first item in the array
                    - Repeat this function for the next 4(for example) items in the array, then stop.

                    Any ideas?

                    Comment


                    • #11
                      Right, I eventually sorted the problem, this is how I did it, but thanks for all your help all:

                      PHP Code:
                       <?php
                      $files 
                      glob'./xml/*.xml' ); 
                      array_multisort
                          
                      array_map'filemtime'$files ), 
                          
                      SORT_ASC
                          
                      SORT_NUMERIC
                          
                      $files 
                      ); 
                      $files array_reverse($files);

                      $fileCount 0;
                      while (
                      $file current($files) and $fileCount 8){
                          if (
                      eregi("^..?$"$file)) {
                              continue;
                          }
                          
                      $open $file;
                          
                      $xml domxml_open_file($open);
                          
                      $root $xml->root();
                          
                      $stat_array $root->get_elements_by_tagname("status");
                          
                      $status extractText($stat_array);
                          
                          
                      $r_array $root->get_elements_by_tagname("regarding");
                          
                      $regarding extractText($r_array);
                          
                          
                      $ab_array $root->get_elements_by_tagname("abstract");
                          
                      $abstract extractText($ab_array);

                          
                      $h_array $root->get_elements_by_tagname("headline");
                          
                      $headline extractText($h_array);

                          if (
                      $status != "live"){
                              continue;
                          }
                          list(
                      $emptvar$emptyvar2$filename01) = explode("/"$file);
                          echo 
                      "<tr><td><table>";
                          echo 
                      "<tr align=left valign=top><td width='90' align='center'>";
                          echo 
                      "<img src='images/newscat/".$regarding ".png'></td><td>";
                          echo 
                      "<a class='headline' href=\"showArticle.php?file=".$filename01 "\">".$headline "</a><br>";
                          echo 
                      "<span class='bytext'>Posted: " date ("F d Y H:i"filemtime($open));
                          echo 
                      "</span><br>" $abstract;
                          echo 
                      "</td></tr></table></td></tr>";
                          
                          
                      $fileCount++;
                          
                      $file next($files);
                      }
                      ?>

                      Comment

                      Working...
                      X
                      😀
                      🥰
                      🤢
                      😎
                      😡
                      👍
                      👎