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 = $files[ rand( 0, count( $files )-1 ) ];
// 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?
// 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
// 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.
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:
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:
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.
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.
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?
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.
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)
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.
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.