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-03-2012, 08:33 PM   PM User | #31
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
Hmm, think this will work but test it!

PHP Code:
<?php
session_start
();
error_reporting (E_ALL E_STRICT);
$files = array();
$path "images/"
$sRedirectTo 'http://yoursite.com/index.php';
$_SESSION['numQuestions'] = 2// Number of questions presented per session

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) && (count($aUnvisited) < $_SESSION['numQuestions']))
    {
        
// 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.
        
if (count($aUnvisited) == 0
        {
            
session_unset();
        }
        
header('Location: ' $sRedirectTo);
        exit();
    }
}
else
{
    
printf("Could not open %s for reading" PHP_EOL$path);

?>
__________________
Logic will get you from A to B. Imagination will take you everywhere.
Albert Einstein
dan-dan is offline   Reply With Quote
Old 07-03-2012, 08:39 PM   PM User | #32
krispol
New Coder

 
Join Date: May 2012
Posts: 56
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
Quote:
Originally Posted by dan-dan View Post
Hmm, think this will work but test it!

PHP Code:
<?php
session_start
();
error_reporting (E_ALL E_STRICT);
$files = array();
$path "images/"
$sRedirectTo 'http://yoursite.com/index.php';
$_SESSION['numQuestions'] = 2// Number of questions presented per session

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) && (count($aUnvisited) < $_SESSION['numQuestions']))
    {
        
// 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.
        
if (count($aUnvisited) == 0
        {
            
session_unset();
        }
        
header('Location: ' $sRedirectTo);
        exit();
    }
}
else
{
    
printf("Could not open %s for reading" PHP_EOL$path);

?>
Hmmm I have only this text showing on a blank background:

Array ( [0] => questions/0014023.html [1] => questions/0011023.html [2] => questions/0003023.html [3] => questions/0001023.html [4] => questions/0008023.html [5] => questions/0016023.html [6] => questions/0002023.html [7] => questions/0013023.html [8] => questions/0015023.html [9] => questions/0006023.html [10] => questions/0010023.html [11] => questions/0005023.html [12] => questions/0007023.html [13] => questions/0009023.html [14] => questions/0020023.html [15] => questions/0018023.html [16] => questions/0004023.html [17] => questions/0012023.html [18] => questions/0019023.html [19] => questions/0017023.html )
krispol is offline   Reply With Quote
Old 07-03-2012, 09:01 PM   PM User | #33
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
Can you post any errors please. I noticed one thing. Try this:

PHP Code:
<?php
session_start
();
error_reporting (E_ALL E_STRICT);
$files = array();
$path "images/"
$sRedirectTo 'http://yoursite.com/index.php';
$_SESSION['numQuestions'] = 2// Number of questions presented per session

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) && (count($aUnvisited) < $_SESSION['numQuestions']))
    {
        
// 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.
        
if ((count($aUnvisited) == 0) || ($_SESSION['numQuestions'] > count($aUnvisited))) 
        {
            unset(
$_SESSION['visited']);
        }
        unset(
$_SESSION['numQuestions']);
        
header('Location: ' $sRedirectTo);
        exit();
    }
}
else
{
    
printf("Could not open %s for reading" PHP_EOL$path);

?>
Edit: Just noticed another potential error so updated.
Edit: Been updated again. One problem is that the remaining unanswered questions (over mulitple restarts) might be greater than the questions per page. For this it'll just reset like if there were none.
__________________
Logic will get you from A to B. Imagination will take you everywhere.
Albert Einstein

Last edited by dan-dan; 07-03-2012 at 09:21 PM..
dan-dan is offline   Reply With Quote
Old 07-03-2012, 09:16 PM   PM User | #34
krispol
New Coder

 
Join Date: May 2012
Posts: 56
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
Quote:
Originally Posted by dan-dan View Post
Can you post any errors please. I noticed one thing. Try this:

PHP Code:
<?php
session_start
();
error_reporting (E_ALL E_STRICT);
$files = array();
$path "images/"
$sRedirectTo 'http://yoursite.com/index.php';
$_SESSION['numQuestions'] = 2// Number of questions presented per session

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) && (count($aUnvisited) < $_SESSION['numQuestions']))
    {
        
// 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.
        
if (count($aUnvisited) == 0
        {
            unset(
$_SESSION['visited']);
        }
        unset(
$_SESSION['numQuestions']);
        
header('Location: ' $sRedirectTo);
        exit();
    }
}
else
{
    
printf("Could not open %s for reading" PHP_EOL$path);

?>
Edit: Just noticed another potential error so updated.
It doesn't give me any errors. It openes te question page window as usually but instead of randomly picked question page it shows me the same text as I posted.
Here is the screenshot: http://dl.dropbox.com/u/13971705/screenshot.jpg
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 02:54 PM.


Advertisement
Log in to turn off these ads.