Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-01-2012, 03:06 PM   PM User | #1
krispol
New Coder

 
Join Date: May 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
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!
krispol is offline   Reply With Quote
Old 07-01-2012, 05:19 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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
Fou-Lu is offline   Reply With Quote
Old 07-01-2012, 05:36 PM   PM User | #3
krispol
New Coder

 
Join Date: May 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
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?
krispol is offline   Reply With Quote
Old 07-01-2012, 05:53 PM   PM User | #4
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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
dan-dan is offline   Reply With Quote
Users who have thanked dan-dan for this post:
krispol (07-02-2012)
Old 07-01-2012, 10:25 PM   PM User | #5
krispol
New Coder

 
Join Date: May 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
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
krispol is offline   Reply With Quote
Old 07-02-2012, 03:54 AM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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 ) :/
Fou-Lu is offline   Reply With Quote
Old 07-02-2012, 12:55 PM   PM User | #7
krispol
New Coder

 
Join Date: May 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
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?
krispol is offline   Reply With Quote
Old 07-02-2012, 05:05 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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);
}
Fou-Lu is offline   Reply With Quote
Old 07-02-2012, 06:24 PM   PM User | #9
krispol
New Coder

 
Join Date: May 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
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..
krispol is offline   Reply With Quote
Old 07-02-2012, 06:29 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 07-02-2012, 06:46 PM   PM User | #11
krispol
New Coder

 
Join Date: May 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
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?
krispol is offline   Reply With Quote
Old 07-02-2012, 06:49 PM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
krispol (07-02-2012)
Old 07-02-2012, 06:56 PM   PM User | #13
krispol
New Coder

 
Join Date: May 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
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)
krispol is offline   Reply With Quote
Old 07-02-2012, 09:24 PM   PM User | #14
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
krispol (07-02-2012)
Old 07-02-2012, 09:32 PM   PM User | #15
krispol
New Coder

 
Join Date: May 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
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!!!!
krispol is offline   Reply With Quote
Reply

Bookmarks

Tags
array, php, random, randomize

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:22 AM.


Advertisement
Log in to turn off these ads.