CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Randomize pages order (http://www.codingforums.com/showthread.php?t=266816)

krispol 07-01-2012 03:06 PM

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! :)

Fou-Lu 07-01-2012 05:19 PM

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

krispol 07-01-2012 05:36 PM

Quote:

Originally Posted by Fou-Lu (Post 1246953)
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?

dan-dan 07-01-2012 05:53 PM

Missing a bracket here:
Code:

while (false !== ($file = readdir($dh)))
For a page redirect you can use:
PHP Code:

header("location : somePage.php"); 


krispol 07-01-2012 10:25 PM

Quote:

Originally Posted by dan-dan (Post 1246963)
Missing a bracket here:
Code:

while (false !== ($file = readdir($dh)))

I fixed it. But unfortunately all I get is a blank page :confused:

Fou-Lu 07-02-2012 03:54 AM

Quote:

Originally Posted by krispol (Post 1247037)
I fixed it. But unfortunately all I get is a blank page :confused:

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 ) :/

krispol 07-02-2012 12:55 PM

Quote:

Originally Posted by Fou-Lu (Post 1247084)
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? :)

Fou-Lu 07-02-2012 05:05 PM

Quote:

Originally Posted by krispol (Post 1247167)
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);
}


krispol 07-02-2012 06:24 PM

Quote:

Originally Posted by Fou-Lu (Post 1247247)
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?:confused:

Fou-Lu 07-02-2012 06:29 PM

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.

krispol 07-02-2012 06:46 PM

Quote:

Originally Posted by Fou-Lu (Post 1247281)
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?

Fou-Lu 07-02-2012 06:49 PM

Quote:

Originally Posted by krispol (Post 1247287)
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.

krispol 07-02-2012 06:56 PM

Quote:

Originally Posted by Fou-Lu (Post 1247288)
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)

Fou-Lu 07-02-2012 09:24 PM

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.

krispol 07-02-2012 09:32 PM

Quote:

Originally Posted by Fou-Lu (Post 1247331)
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!!!!:)


All times are GMT +1. The time now is 11:14 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.