Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-22-2011, 05:12 PM   PM User | #1
YourDirector
Regular Coder

 
Join Date: Jul 2011
Posts: 116
Thanks: 6
Thanked 0 Times in 0 Posts
YourDirector is an unknown quantity at this point
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
YourDirector is offline   Reply With Quote
Old 08-22-2011, 08:35 PM   PM User | #2
>ssp-cdr<
Regular Coder

 
Join Date: May 2007
Posts: 100
Thanks: 16
Thanked 11 Times in 11 Posts
>ssp-cdr< is an unknown quantity at this point
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.
>ssp-cdr< is offline   Reply With Quote
Old 08-23-2011, 10:12 AM   PM User | #3
YourDirector
Regular Coder

 
Join Date: Jul 2011
Posts: 116
Thanks: 6
Thanked 0 Times in 0 Posts
YourDirector is an unknown quantity at this point
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
YourDirector is offline   Reply With Quote
Old 08-23-2011, 04:27 PM   PM User | #4
Jinxy
New Coder

 
Join Date: Feb 2007
Posts: 86
Thanks: 0
Thanked 2 Times in 2 Posts
Jinxy has a little shameless behaviour in the past
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..
Jinxy is offline   Reply With Quote
Old 08-23-2011, 04:47 PM   PM User | #5
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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
kbluhm is offline   Reply With Quote
Old 09-01-2011, 02:22 AM   PM User | #6
YourDirector
Regular Coder

 
Join Date: Jul 2011
Posts: 116
Thanks: 6
Thanked 0 Times in 0 Posts
YourDirector is an unknown quantity at this point
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:
Quote:
Array()
YourDirector is offline   Reply With Quote
Old 09-01-2011, 03:16 AM   PM User | #7
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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)?
__________________
ZCE

Last edited by kbluhm; 09-01-2011 at 03:24 AM..
kbluhm is offline   Reply With Quote
Old 09-01-2011, 03:21 AM   PM User | #8
YourDirector
Regular Coder

 
Join Date: Jul 2011
Posts: 116
Thanks: 6
Thanked 0 Times in 0 Posts
YourDirector is an unknown quantity at this point
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?
YourDirector is offline   Reply With Quote
Old 09-01-2011, 03:36 AM   PM User | #9
YourDirector
Regular Coder

 
Join Date: Jul 2011
Posts: 116
Thanks: 6
Thanked 0 Times in 0 Posts
YourDirector is an unknown quantity at this point
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
YourDirector is offline   Reply With Quote
Old 09-01-2011, 04:08 AM   PM User | #10
YourDirector
Regular Coder

 
Join Date: Jul 2011
Posts: 116
Thanks: 6
Thanked 0 Times in 0 Posts
YourDirector is an unknown quantity at this point
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?
YourDirector is offline   Reply With Quote
Old 09-06-2011, 09:57 AM   PM User | #11
YourDirector
Regular Coder

 
Join Date: Jul 2011
Posts: 116
Thanks: 6
Thanked 0 Times in 0 Posts
YourDirector is an unknown quantity at this point
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);
}
?>
YourDirector is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:20 PM.


Advertisement
Log in to turn off these ads.