Php

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

dudeking

Thread Starter
Joined
Feb 7, 2007
Messages
483
Hi,
I need to get this working for friday because it's my coursework.
I need the script to include all the files in a directory in numerical order.

http://beta.dudeking.co.uk/comp/news/

At the moment I am using this Script:

PHP:
<?php
	$path = 'stories/';
	$handle = opendir($path);
		while (false!== ($file = readdir($handle))) {
			include($path.$file);
	}
	closedir($handle); 
?>
But have tried something like:

PHP:
<?php
	$dh = opendir("/comp/news/stories/");
		$count='0';
			while ($entry = readdir($dh)) {
				if ((strpos($entry,'.php') !== false) && ($entry!=='index.php')) {
					if ($count < '0' ) {
						include('stories/'.$entry);
					}
				$count++;
			}
		}
	closedir($dh);
?>
Any help would be great :)
 
Joined
Oct 9, 2001
Messages
27,087
well it's failing on the path "stories/" not being valid from the directory it's running in

try "./stories"
 

dudeking

Thread Starter
Joined
Feb 7, 2007
Messages
483
try "./stories" made it worse.

I'll look into that now, thanks.
 
Joined
Jul 8, 2002
Messages
14,681
Are you trying to include all the files in a directory?
PHP:
<?php
foreach(glob('./stories/*') as $story)
{
     include($story);
}?>
 

dudeking

Thread Starter
Joined
Feb 7, 2007
Messages
483
Yes I want all of the files.
I tried that, got this message:
Warning: main(./stories/./stories/994story.php): failed to open stream: No such file or directory in /home/fhlinux210/b/beta.dudeking.co.uk/user/htdocs/comp/include/news/scriptmain.php on line 4

Do I need the dot?
 
Joined
Jul 8, 2002
Messages
14,681
I edited my post a couple minutes after writing it, it looks like you might have copied it before I fixed it?

The dot means stories is in the same directory as your current script. Otherwise you need to specify the path to where it is.
 

dudeking

Thread Starter
Joined
Feb 7, 2007
Messages
483
OMG!!!!!!!!!!!!!!!

Thank you so much!!!!!!!!!!!!!!!!!!!!!!!!

You have no idea how much you have just helped me :):):):):):):):):)
 

dudeking

Thread Starter
Joined
Feb 7, 2007
Messages
483
Do you have any idea about why it orders them 1,2,3,6,5,4?

They should be 123456

Thanks You :)
 
Joined
Jul 8, 2002
Messages
14,681
Are you trying to sort them by filename?
You can use GLOB_NOSORT to list them as they appear in the directory. Otherwise you can use asort() to put them in order.
 

dudeking

Thread Starter
Joined
Feb 7, 2007
Messages
483
the files are just called 1.php 2.php 3.php ect.

I need them listing in order starting with 1.php.
 
Joined
Jul 8, 2002
Messages
14,681
PHP:
<?php
$stories = glob('./stories/*');
natsort($stories);
foreach($stories as $story)
{
     include($story);
}?>
 
Joined
Jul 8, 2002
Messages
14,681
Something else is going on then, above code should sort by number. You can use print_r() to find out where the error is.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top