Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 34
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    57
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Randomize pages order

    Hey

    Here's my php code:
    PHP Code:
    <?php  
    error_reporting 
    (E_ALL E_STRICT);

    $files = array();

    $path "./questions/";

    if( 
    $handle opendir$path ) ) {
            while (
    false !== ($file readdir($handle))) {
                    if( 
    $file != "." && $file != ".." && !is_dir$path.$file ) ) {
                                       
    $files[] = $path.$file;
                                    }
                    }
            
    // You need to populate the $files variable before you count it;)
            
    $file $filesrand0count$files )-) ];
            
    // Now we can include it!
            
    include ("$file");
    }

    else {
            print 
    "no files found";
    }       
    ?>
    It does what I need: randomly calls for html pages from the questions folder.

    What I would need help with is:
    1. How to prevent pages from repeating?
    2. How to stop and redirect to some other page after all the html pages from the questions folder have been displayed?

    I would appretiate any help!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You can't do any of this without using some external storage. If its unique to an individual client, then you may best use sessions.
    PHP Code:
    <?php
    error_reporting 
    (E_ALL E_STRICT);
    session_start();
    $files = array();
    $path "./questions/"

    if (!isset(
    $_SESSION['visited']))
    {
        
    $_SESSION['visited'] = array();
    }

    if (
    $dh = @opendir($path))
    {
        while (
    false !== ($file readdir($dh))
        {
            if (
    $file != "." && $file != ".." && is_file($file))
            {
                
    $files[] = $path $file;
            }
        }
        
    closedir($dh);
        
        
    // mkay, here remove what's already been visited
        
    $aUnvisited array_diff($files$_SESSION['visited']);
        if (
    count($aUnvisited) > 0)
        {
            
    // Still have places to go
            
    shuffle($aUnvisited);
            
    $sToInclude array_shift($aUnvisited);
            
    $_SESSION['visited'][] = $sToInclude;
            include(
    $sToInclude);
        }
        else
        {
            
    // Everything has been visited.  Put your end case scenario here.
        
    }
    }
    else
    {
        
    printf("Could not open %s for reading" PHP_EOL$path);
    }
    Something like that. Untested, but works alright in my head

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    57
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    You can't do any of this without using some external storage. If its unique to an individual client, then you may best use sessions.
    PHP Code:
    <?php
    error_reporting 
    (E_ALL E_STRICT);
    session_start();
    $files = array();
    $path "./questions/"

    if (!isset(
    $_SESSION['visited']))
    {
        
    $_SESSION['visited'] = array();
    }

    if (
    $dh = @opendir($path))
    {
        while (
    false !== ($file readdir($dh))
        {
            if (
    $file != "." && $file != ".." && is_file($file))
            {
                
    $files[] = $path $file;
            }
        }
        
    closedir($dh);
        
        
    // mkay, here remove what's already been visited
        
    $aUnvisited array_diff($files$_SESSION['visited']);
        if (
    count($aUnvisited) > 0)
        {
            
    // Still have places to go
            
    shuffle($aUnvisited);
            
    $sToInclude array_shift($aUnvisited);
            
    $_SESSION['visited'][] = $sToInclude;
            include(
    $sToInclude);
        }
        else
        {
            
    // Everything has been visited.  Put your end case scenario here.
        
    }
    }
    else
    {
        
    printf("Could not open %s for reading" PHP_EOL$path);
    }
    Something like that. Untested, but works alright in my head
    There is something wrong on line 15. After this line:
    PHP Code:
    if ($file != "." && $file != ".." && is_file($file)) 
    it shows that there is smth wrong with the curly brace.

    Im not sure what is the right way to fix it?

  • #4
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    Missing a bracket here:
    Code:
    while (false !== ($file = readdir($dh)))
    For a page redirect you can use:
    PHP Code:
    header("location : somePage.php"); 
    Logic will get you from A to B. Imagination will take you everywhere.
    Albert Einstein

  • Users who have thanked dan-dan for this post:

    krispol (07-02-2012)

  • #5
    New Coder
    Join Date
    May 2012
    Posts
    57
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dan-dan View Post
    Missing a bracket here:
    Code:
    while (false !== ($file = readdir($dh)))
    I fixed it. But unfortunately all I get is a blank page

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by krispol View Post
    I fixed it. But unfortunately all I get is a blank page
    With no error? Did you put anything into the else condition to indicate there were no more files to iterate?

    Lol, I always miss that last ) :/

  • #7
    New Coder
    Join Date
    May 2012
    Posts
    57
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    With no error? Did you put anything into the else condition to indicate there were no more files to iterate?

    Lol, I always miss that last ) :/
    Hey

    Nope, no error, just a blank page. I didn't put anything into the else condition, what should I put there though?

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by krispol View Post
    Hey

    Nope, no error, just a blank page. I didn't put anything into the else condition, what should I put there though?
    Anything will do, sounds to me like you want a redirect. Dan posted a header to change the location.
    The problem you'll have is that is_file requires a valid file. It can be relative, but I missed the path in the check. Use this instead:
    PHP Code:
    <?php
    session_start
    ();
    error_reporting (E_ALL E_STRICT);
    $files = array();
    $path "./randompages/"
    $sRedirectTo 'http://yoursite.com/index.php';

    if (!isset(
    $_SESSION['visited']))
    {
        
    $_SESSION['visited'] = array();
    }

    if (
    $dh = @opendir($path))
    {
        while (
    false !== ($file readdir($dh)))
        {
            if (
    $file != "." && $file != ".." && is_file($path $file))
            {
                
    $files[] = $path $file;
            }
        }
        
    closedir($dh);
        
    // mkay, here remove what's already been visited
        
    $aUnvisited array_diff($files$_SESSION['visited']);
        
    print_r($aUnvisited);
        if (
    count($aUnvisited) > 0)
        {
            
    // Still have places to go
            
    shuffle($aUnvisited);
            
    $sToInclude array_shift($aUnvisited);
            
    $_SESSION['visited'][] = $sToInclude;
            include(
    $sToInclude);
        }
        else
        {
            
    // Everything has been visited.  Put your end case scenario here.
            
    header('Location: ' $sRedirectTo);
            exit();
        }
    }
    else
    {
        
    printf("Could not open %s for reading" PHP_EOL$path);
    }

  • #9
    New Coder
    Join Date
    May 2012
    Posts
    57
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Anything will do, sounds to me like you want a redirect. Dan posted a header to change the location.
    The problem you'll have is that is_file requires a valid file. It can be relative, but I missed the path in the check. Use this instead:
    PHP Code:
    <?php
    session_start
    ();
    error_reporting (E_ALL E_STRICT);
    $files = array();
    $path "./randompages/"
    $sRedirectTo 'http://yoursite.com/index.php';

    if (!isset(
    $_SESSION['visited']))
    {
        
    $_SESSION['visited'] = array();
    }

    if (
    $dh = @opendir($path))
    {
        while (
    false !== ($file readdir($dh)))
        {
            if (
    $file != "." && $file != ".." && is_file($path $file))
            {
                
    $files[] = $path $file;
            }
        }
        
    closedir($dh);
        
    // mkay, here remove what's already been visited
        
    $aUnvisited array_diff($files$_SESSION['visited']);
        
    print_r($aUnvisited);
        if (
    count($aUnvisited) > 0)
        {
            
    // Still have places to go
            
    shuffle($aUnvisited);
            
    $sToInclude array_shift($aUnvisited);
            
    $_SESSION['visited'][] = $sToInclude;
            include(
    $sToInclude);
        }
        else
        {
            
    // Everything has been visited.  Put your end case scenario here.
            
    header('Location: ' $sRedirectTo);
            exit();
        }
    }
    else
    {
        
    printf("Could not open %s for reading" PHP_EOL$path);
    }

    Thank you so much for helping me!!!

    It is almost working now. At first I got all my questions randomly without repeating, only thing was that it was also displaying some code on the background, probably, how many questions are left or smth.

    After the last question where it was supposed to redirect to http://mypage.com it just showed a blank page with a text: Array () on it.

    Now that I refreshed/closed and opened the page and tried to start answering again, it right away shows me that blank page with Array () on it.

    Any idea?
    Last edited by krispol; 07-02-2012 at 06:27 PM.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yes. I left this in while debugging: print_r($aUnvisited);.
    The blank page is actually caused by an error. The call to header() fails since I already flushed output with the print_r. When its removed, it should function properly.

  • #11
    New Coder
    Join Date
    May 2012
    Posts
    57
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Yes. I left this in while debugging: print_r($aUnvisited);.
    The blank page is actually caused by an error. The call to header() fails since I already flushed output with the print_r. When its removed, it should function properly.
    Great it worked, no blank page anymore. Only thing is that ig goes to that
    PHP Code:
    $sRedirectTo 'http://mypage.com/index.html'
    right away.

    Even if I refresh it still probably takes it as I've already visited the question pages?

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by krispol View Post
    Great it worked, no blank page anymore. Only thing is that ig goes to that
    PHP Code:
    $sRedirectTo 'http://mypage.com/index.html'
    right away.

    Even if I refresh it still probably takes it as I've already visited the question pages?
    Its still tracking your sessions even after page modification. You'll have to destroy the sessions or at least the cookies associated with the session in order to test it out again.

  • Users who have thanked Fou-Lu for this post:

    krispol (07-02-2012)

  • #13
    New Coder
    Join Date
    May 2012
    Posts
    57
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Its still tracking your sessions even after page modification. You'll have to destroy the sessions or at least the cookies associated with the session in order to test it out again.
    Yay, working! I can't thank you enaugh!!! You literally made my day!

    One question: does it mean then that if a visitor would like to answer those questions once more, he wouldn't be able to? (I assume majority won't come up with deleting cache,cookies or sessions)

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Sessions only last until the browser is closed. If you need it to persist, you'll need to implement a datastorage through files or a db in order to block.

  • Users who have thanked Fou-Lu for this post:

    krispol (07-02-2012)

  • #15
    New Coder
    Join Date
    May 2012
    Posts
    57
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Sessions only last until the browser is closed. If you need it to persist, you'll need to implement a datastorage through files or a db in order to block.
    Oh ok, well I don't need that then, Im happy enaugh with what I got.

    Thank you thank you thank you!!!!


  •  
    Page 1 of 3 123 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •