Member Avatar for kirtan_thakkar

I am using the code as under but always getting 0 value means the same folder

$random_folder=array("folder1","folder2","folder3");
$folder=$random_folder[rand(0,count($random_folder)-1)];

Getting folder1 always.. What's wrong??
Please Help.......

Recommended Answers

All 7 Replies

Hi kirtan_thakkar,

There's nothing wrong with your code. The reason your getting all folder1 is that there is also 3 items on your array. And its randomized, so maybe "0" today could have been your lucky number. :)

Tip:
Keep refreshing the page and let's see it doesn't change.

Regards,
Al

Member Avatar for kirtan_thakkar

Hi kirtan_thakkar,

There's nothing wrong with your code. The reason your getting all folder1 is that there is also 3 items on your array. And its randomized, so maybe "0" today could have been your lucky number. :)

Tip:
Keep refreshing the page and let's see it doesn't change.

Regards,
Al

I have tried more than 50 time but getting on folder1...

Hi,

Can you post the whole code please?

I tried myself, it does change.

Regards,
Al

Member Avatar for kirtan_thakkar

Hi,

Can you post the whole code please?

I tried myself, it does change.

Regards,
Al

$random_folder=array("folder1","folder2","folder3");

$folder=$random_folder[rand(0,count($random_folder)-1)];

if($folder="folder1")
	include("folder1.php");
elseif($folder="folder2")
	include("folder2.php");
else
	include("folder3.php");

$random_file=$file[rand(0,count($file)-1)];
$filename=$folder."/".$random_file.".txt";
$handle=fopen($filename,"r");
$contents=fread($handle,filesize($filename));
echo($contents);

And that include in if includes that php file where i have stored the arrays for that folder and the array name is file.
I am getting files from only folder1..

Hi,

Try replacing this lines,

$folder="folder1"

with

$folder=="folder1"

Also,

elseif($author_folder="folder2")

with

elseif($folder=="folder2")
Member Avatar for kirtan_thakkar

Hi,

Try replacing this lines,

$folder="folder1"

with

$folder=="folder1"

Also,

elseif($author_folder="folder2")

with

elseif($folder=="folder2")

Hey thanks...
It has worked.. I have tried 4 hours to find it and you have solved in a minut..

try to caluclate it outside the array do like this.

$folderarrayNumb = count($random_folder)-1;
$myrand = rand(0,$folderarrayNumb);
$folder=$random_folder[$myrand];
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.