I need to assign a variable based on its value.

ie:

$variable = 'value' (if value != "Session"

My brain is not really working atm I know there has to be a simple way to do this.

I am selecting a random file from a directory and do not want to repeat the selection:
eg:

<?php
$path = "/home/folder/";
$narray=array();
$dir_handle = @opendir($path) or die("Unable to open $path please notify the administrator. Thank you.");
$i=0;
while($file = readdir($dir_handle))
{
     if($file != '.' && $file != '..' && $file !=' ' && $file !="SESSION['incl1']")
    {
        $narray[$i]=$file;
        $i++;
        


    }
}

//I want to make sure when I assign the value below it is not repeated from another //instance of this script embedded in the php page
$j = rand(0, $i-1);

        include ("$path$narray[$j]");

//closing the directory
closedir($dir_handle);

?>

Recommended Answers

All 3 Replies

ok here is an idea if someone can help me.

I want to be able to exclude specific numbers from the random range for the array:

eg:

//if  have already had the value 5 assignment to $j before can I exclude 5 from this variable assignment.
$j = rand(0, $i-1);

Exclusion from rand is usually not a good idea as it makes things incredibly difficult rather fast (at least from my own experiences).

In your file thing above, loop through each file and add each one to an array:

// PSEUDO CODE!!!!!!!!!!!!!!!!!!
foreach($file found in $directory){
   if(!in_array($file,$directory){
      $array[] = file;
   }
}

oK I think I found a way.

I added a while loop before for the include


$j = rand(0, $i-1);
while ($j !="val1" && $j !="val2")
{
include ("$path$narray[$j]");
}

Does this seem to make sense ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.