1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Php

Discussion in 'Web Design & Development' started by dudeking, Feb 7, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. dudeking

    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 :)
     
  2. Gibble

    Gibble

    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"
     
  3. Gibble

    Gibble

    Joined:
    Oct 9, 2001
    Messages:
    27,087
    You can also look into the realpath function.
     
  4. dudeking

    dudeking Thread Starter

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

    I'll look into that now, thanks.
     
  5. brendandonhu

    brendandonhu

    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);
    }
    ?>
     
  6. dudeking

    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?
     
  7. brendandonhu

    brendandonhu

    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.
     
  8. dudeking

    dudeking Thread Starter

    Joined:
    Feb 7, 2007
    Messages:
    483
    Yup, that script is different :)
    I'll test now :p
     
  9. dudeking

    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 :):):):):):):):):)
     
  10. dudeking

    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 :)
     
  11. brendandonhu

    brendandonhu

    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.
     
  12. dudeking

    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.
     
  13. brendandonhu

    brendandonhu

    Joined:
    Jul 8, 2002
    Messages:
    14,681
    PHP:
    <?php
    $stories 
    glob('./stories/*');
    natsort($stories);
    foreach(
    $stories as $story)
    {
         include(
    $story);
    }
    ?>
     
  14. dudeking

    dudeking Thread Starter

    Joined:
    Feb 7, 2007
    Messages:
    483
    Still getting the dodgy ordering :(
     
  15. brendandonhu

    brendandonhu

    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.
     
  16. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/542062

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice