/    Sign up×
Community /Pin to ProfileBookmark

I’m trying to sort the list of files from ABC, Currently its displaying rand, How would I organize this?
THANKS

[CODE]
$ndir = @opendir(‘news_articles/title’);
$counter = 0;
while(($filename = readdir($ndir)) !== false){
if(!($counter < 2)){
$display = file_get_contents(‘news_articles/title/’ . $filename);
echo $display . ‘<br>’;
}
$counter++;
}
?>
[/CODE]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@SpectreReturnsMar 30.2006 — Add it to an array and then sort that.
Copy linkTweet thisAlerts:
@balloonbuffoonMar 30.2006 — You might also consider using glob(), as it automatically sorts by alphabet:
[code=php]
foreach(glob("news_articles/title/*.*") as $filename) {
$display = file_get_contents('news_articles/title/' . $filename);
echo $display . '<br>';
}
[/code]

If that doesn't work, you may have to supply glob() with the full path to that folder (using $_SERVER["DOCUMENT_ROOT"] and such).

--Steve
Copy linkTweet thisAlerts:
@NogDogMar 30.2006 — [code=php]
$files = glob('news_articles/title/*');
// default for glob is an alpha-sort listing
$counter = 0;
foreach($files as $file)
{
if(is_dir($file))
{
continue; // it's a directory, so ignore it
}
readfile($file); // reads and outputs file in one fell swoop
echo "<br>";
if(++$counter > 2){
break;
}
}
[/code]
Copy linkTweet thisAlerts:
@SpectreReturnsMar 30.2006 — Real men use the *dir functions.
Copy linkTweet thisAlerts:
@BrutusUnixauthorMar 31.2006 — [code=php]
$files = glob('news_articles/title/*');
// default for glob is an alpha-sort listing
$counter = 0;
foreach($files as $file)
{
if(is_dir($file))
{
continue; // it's a directory, so ignore it
}
readfile($file); // reads and outputs file in one fell swoop
echo "<br>";
if(++$counter > 2){
break;
}
}
[/code]
[/QUOTE]


Your code works but it lists in reverse to what I want

test 1

test 2

test 3

I want:

test 3

test 2

test 1

How would I just reverse this?

Thanks again
Copy linkTweet thisAlerts:
@balloonbuffoonMar 31.2006 — [code=php]
foreach(arsort(glob("news_articles/title/*.*")) as $filename) {
readfile('news_articles/title/' . $filename);
echo '<br>';
}
[/code]
×

Success!

Help @BrutusUnix spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...