PDA

View Full Version : Need help with arrays



xanikseo
03-18-2006, 07:30 PM
I'm a bit of a n00b when it comes to PHP, anyway, I am in process of making a website about Gaming News, and I decided to put all of my different news stories into different .txt files on a different directory. I have everything done fine except one thing: my news pieces are shown in the alphabetical order of the .txt filenames. That means that the latest story could be on page 53 or whatever. I have tried making codes that shows the news in the order of the creation/modification date of the .txt file.

Here is what I tried:


<?php

function returnfiles($dirname="docs/") {
$pattern="\.(txt)$";
$files = array();
$fileloopcounter=0;
if($handle = opendir($dirname))
{
while(false !== ($file = readdir($handle)))
{
if(eregi($pattern, $file)){
$filedate=filemtime($dirname.$file);
$files[] = array($file, $filedate);
$fileloopcounter++;
}
}

closedir($handle);
}
return($files);
}

function DateCmp($a, $b)
{
return ($a[1] < $b[1]) ? -1 : 0;
}

function SortByDate($files)
{
usort($files, 'DateCmp');
}

$files = returnfiles($dirname="docs/");
SortByDate($files);
echo '<ol>';
foreach ($files as $file => $val)
{
echo '<li>', $val, '</li>';
}
echo '</ol>';
?>

And then I just get this when I open the file on a browser:

http://img86.imageshack.us/img86/8474/asdasd0di.png

Can someone please help me with my problem?

Thanks in advance :).

Twey
03-18-2006, 07:47 PM
echo '<li>', $val[0], '</li>';

xanikseo
03-18-2006, 08:10 PM
Ah :). Thank you very much :). How could I put the result in reverse order?

Twey
03-18-2006, 08:43 PM
function DateCmp($a, $b)
{
return !($a[1] < $b[1]) ? -1 : 0;
}

xanikseo
03-18-2006, 09:00 PM
function DateCmp($a, $b)
{
return !($a[1] < $b[1]) ? -1 : 0;
}
Doesn't work... Strange...

Twey
03-18-2006, 09:28 PM
Really? Hm. Try:
function DateCmp($a, $b)
{
return ($a[1] > $b[1]) ? -1 : 0;
}

xanikseo
03-19-2006, 03:58 PM
Still the same thing...

Twey
03-19-2006, 04:01 PM
Paste here (in [code] tags) the output when using all the last three code blocks I posted.

xanikseo
03-19-2006, 06:59 PM
The output is exactly the same as when you first fixed the code. All the files are listed in order of when they were modified, except all of the codes show the output with the newly modified files at the bottom.

djr33
03-21-2006, 06:09 AM
there's probly an easier way, but you could just create a new loop that reverses after the fact... Or just start a variable and count down.
Then again, I'm sure Twey's method is easier and will work once you figure out what the issue is.

Twey
03-21-2006, 03:03 PM
Yes... but I'm not entirely sure why the above code isn't working. I wlll investigate.

/EDIT: Oh, duh. This is an obvious one...

function DateCmp($a, $b)
{
return ($a[1] > $b[1]) ? -1 : 1;
}

xanikseo
03-21-2006, 06:33 PM
Still doesn't make a difference, I know part of the problem. I remove the contents of the function DateCmp(), and it still shows the same old thing (files in the wrong order)... So DateCmp() isn't making a difference at all, no matter what you do with it. There must be a problem somewhere else in the code... Perhaps I shouldn't use a usort method...

Twey
03-21-2006, 07:00 PM
Try using "DateCmp" instead of DateCmp as the argument to the usort function.

xanikseo
03-22-2006, 12:20 PM
Still no difference...

djr33
03-23-2006, 08:40 AM
Sounds painful, but you could rewrite it a bit. It's not too hard to write a loop that would reverse it.
Then again your stuff is pretty complex now and that would just confuse it.
Carry on.

Brainstorming... not getting too far.

...And learning from what you guys are figuring out. thanks.