Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 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
    Regular Coder
    Join Date
    May 2007
    Posts
    100
    Thanks
    16
    Thanked 11 Times in 11 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote 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

  • #4
    New Coder
    Join Date
    Feb 2007
    Posts
    86
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Why not use array_reverse()?

    PHP Code:
    $reversed_array array_reverse($old_array); 
    http://php.net/manual/en/function.array-reverse.php
    Last edited by Jinxy; 08-23-2011 at 04:41 PM.

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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 ); 

  • #6
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote 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()

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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; 09-01-2011 at 03:24 AM.

  • #8
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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?

  • #9
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote 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

  • #10
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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?

  • #11
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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);
    }
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •