Jump to content


Photo

little php help


  • Please log in to reply
3 replies to this topic

#1 Tratos

Tratos

    title available

  • Project Team
  • 562 posts

Posted 06 July 2006 - 12:53 AM

Ok, Say i have a directory with a few php files in it, for the sake of example let's call them file1.php, file2.php and file3.php. Each file contains the following;


<tr><td>
<span class="link"><a href="http://www.mywebsite.com">this is my link</a></span>
</td></tr>


Now, i want to include all the file's in that directory on a certain page within a table so that each file includes as a seperate table row, preferably with a single line of code, i woud like the page to auto-update of sorts when a new file is added to the directory. Can php do this? And are there any methods of php sorting and including the files based on their age?

Edited by Tratos, 06 July 2006 - 01:10 AM.

Posted Image

#2 DCoder

DCoder

    One of the Few

  • Members
  • 331 posts
  • Location:Lithuania, Central Europe
  •  Old Grumpy Bastard

Posted 06 July 2006 - 04:28 AM

opendir() opens a directory, readdir() returns a filename from that directory one at a time, stat() returns the file's statistics, including creation/modification time, asort() sorts an associative array by values.

So, the whole process would be like
$dir = opendir('/path');
while(($file = readdir($dir)) !== false)
{
if(!in_array($file, array('.', '..'))
{
$stat = stat($file);
$files[$file] = $stat['ctime']; //creation time, atime for last modification time, mtime for last modification time
}
}
closedir($dir);
asort($files, SORT_NUMERIC); //sorts by time increasing, use arsort() to sort by time decreasing
foreach($files as $file => $time)
{
// include file / whatever else
}

Edited by DCoder, 06 July 2006 - 04:30 AM.

Ares - How do you want to improve YR today?
What's stopping you from migrating to Ares? We can't implement it unless you tell us!

ModEnc - C&C Modding Encyclopedia | Modders' Quote Database | Yet Another C&C Modding Forum

#3 Tratos

Tratos

    title available

  • Project Team
  • 562 posts

Posted 11 August 2006 - 04:49 PM

Cheers DC that helps a lot, following on from this; can i assign each filename as a variable without the extension?

And can i only return files in the directory with a certain extension i.e. .txt or .php?
Posted Image

#4 Mastermind

Mastermind

    Server Technician

  • Undead
  • 7,014 posts
  • Location:Cambridge, MA
  • Projects:MasterNews 3
  •  The Man Behind the Curtain

Posted 11 August 2006 - 05:15 PM


if(strpos($file, '.php')!==false)

{

// do the add stuff here

}


Posted Image

Well, when it comes to writing an expository essay about counter-insurgent tactics, I'm of the old school. First you tell them how you're going to kill them. Then you kill them. Then you tell them how you just killed them.

Too cute! | Server Status: If you can read this, it's up |




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users