Hai
I have some html files and i would like to display them in a random manner using the php.
can any one help me out..I have been trying this one..
but it is not working..

<?php
$dir="./clock/";
$nimg="10";
$itype="html";
$rnumb=rand(1,$nimg);

echo "<a href=".$dir."clock".$rnumb.".".$itype."></a>";
?>

Recommended Answers

All 10 Replies

Hai
I have some html files and i would like to display them in a random manner using the php.
can any one help me out..I have been trying this one..
but it is not working..

<?php
$dir="./clock/";
$nimg="10";
$itype="html";
$rnumb=rand(1,$nimg);

echo "<a href=".$dir."clock".$rnumb.".".$itype."></a>";
?>

I have modified your code -

//by speifying this and using in the echo below, all the files in the array should be from this directory only
$dir = "./clock/";
$arr_file_names = array(1=>'my_string.html','my_script.html','new_counntry.html','test.php','test1.php','test2.php','etst2.php','something.php','ejej.php','bored.php');

$number_of_html_files = count($arr_file_names);

$rnumb = ceil(rand(1,$number_of_html_files));
// test the file name with its file path here - 
//echo $dir."clock".$arr_file_names[$rnumb];
echo "<a href=".$dir."clock".$arr_file_names[$rnumb]."></a>";
?>

I have modified your code -

//by speifying this and using in the echo below, all the files in the array should be from this directory only
$dir = "./clock/";
$arr_file_names = array(1=>'my_string.html','my_script.html','new_counntry.html','test.php','test1.php','test2.php','etst2.php','something.php','ejej.php','bored.php');

$number_of_html_files = count($arr_file_names);

$rnumb = ceil(rand(1,$number_of_html_files));
// test the file name with its file path here - 
//echo $dir."clock".$arr_file_names[$rnumb];
echo "<a href=".$dir."clock".$arr_file_names[$rnumb]."></a>";
?>

hai pal
this is not displaying any thing..
i have the code changed to this one..
then it is just showing me the link. where as i require the code to display the html file directly

<?php
$dir = "./clock/";

$arr_file_names = array(1=>'clock1.html','clock2.html','clock3.html','clock4.html','clock5.html','clock6.html','clock7.html','clock8.html','clock9.html','clock10.html');

 

$number_of_html_files = count($arr_file_names);

 

$rnumb = ceil(rand(1,$number_of_html_files));

// test the file name with its file path here -

//echo $dir."clock".$arr_file_names[$rnumb];
echo "<a href=".$dir."clock".$arr_file_names[$rnumb].">$arr_file_names[$rnumb]</a>";

?>

hai pal
this is not displaying any thing..
i have the code changed to this one..
then it is just showing me the link. where as i require the code to display the html file directly

<?php
$dir = "./clock/";

$arr_file_names = array(1=>'clock1.html','clock2.html','clock3.html','clock4.html','clock5.html','clock6.html','clock7.html','clock8.html','clock9.html','clock10.html');

 

$number_of_html_files = count($arr_file_names);

 

$rnumb = ceil(rand(1,$number_of_html_files));

// test the file name with its file path here -

//echo $dir."clock".$arr_file_names[$rnumb];
echo "<a href=".$dir."clock".$arr_file_names[$rnumb].">$arr_file_names[$rnumb]</a>";

?>

That means you want to include the file randomly in the page (yes, its called 'include'), so need to use the anchor tags
replace the line10 in above code with the below one -

include(".$dir."clock".$arr_file_names[$rnumb].");

That means you want to include the file randomly in the page (yes, its called 'include'), so need to use the anchor tags
replace the line10 in above code with the below one -

include(".$dir."clock".$arr_file_names[$rnumb].");

hai
it is displaying this error

Parse error: parse error, unexpected T_STRING in C:\Program Files\xampp\htdocs\chaitu\magpierss\re.php on line 17

<?php
$dir = "./clock/";

$arr_file_names = array(1=>'1.html','2.html','3.html','4.html','5.html','6.html','7.html','8.html','9.html','10.html');

 

$number_of_html_files = count($arr_file_names);

 

$rnumb = ceil(rand(1,$number_of_html_files));

// test the file name with its file path here -

//echo $dir."clock".$arr_file_names[$rnumb];
include("$dir."clock".$arr_file_names[$rnumb]");
echo "<a href=".$dir."clock".$arr_file_names[$rnumb].">$arr_file_names[$rnumb]</a>";

?>

its right, line17 should have been like this -

include("$dir".clock."$arr_file_names[$rnumb]");

its right, line17 should have been like this -

include("$dir".clock."$arr_file_names[$rnumb]");

Thank you so much pal...
The issue is now resolved.....:icon_razz:

good, keep it up :)

Thank you so much pal...
The issue is now resolved.....:icon_razz:

but the only thing is that it is displaying the path as http://localhost/
when i am checking in my machine..
how do i change it to my machine ip

an if i changed it to my ip address it dispalying a big error.

Warning: include() [function.include]: URL file-access is disabled in the server configuration in C:\Program Files\xampp\htdocs\chaitu\magpierss\re.php on line 17

Warning: include(http://192.168.0.35/chaitu/magpierss/clock/clock9.html) [function.include]: failed to open stream: no suitable wrapper could be found in C:\Program Files\xampp\htdocs\chaitu\magpierss\re.php on line 17

Warning: include() [function.include]: Failed opening 'http://192.168.0.35/chaitu/magpierss/clock/clock9.html' for inclusion (include_path='.;C:\Program Files\xampp\php\pear\') in C:\Program Files\xampp\htdocs\chaitu\magpierss\re.php on line 17


dir is chaned to

$dir = "http://192.168.0.35/chaitu/magpierss/clock/";

hai
silly me:$ i have done the wrong in the html files
it is displaying this error from that files..
Thank you for your help pal...

but the only thing is that it is displaying the path as http://localhost/
when i am checking in my machine..
how do i change it to my machine ip

an if i changed it to my ip address it dispalying a big error.

Warning: include() [function.include]: URL file-access is disabled in the server configuration in C:\Program Files\xampp\htdocs\chaitu\magpierss\re.php on line 17

Warning: include(http://192.168.0.35/chaitu/magpierss/clock/clock9.html) [function.include]: failed to open stream: no suitable wrapper could be found in C:\Program Files\xampp\htdocs\chaitu\magpierss\re.php on line 17

Warning: include() [function.include]: Failed opening 'http://192.168.0.35/chaitu/magpierss/clock/clock9.html' for inclusion (include_path='.;C:\Program Files\xampp\php\pear\') in C:\Program Files\xampp\htdocs\chaitu\magpierss\re.php on line 17


dir is chaned to

$dir = "http://192.168.0.35/chaitu/magpierss/clock/";

Its fine if you put the relative path, instead of the absolute one.
Its about how you define the $dir
make it this way -

$dir = "/clock/";

at line3
or else you can set it to the ip too, as below -

$dir = "http://192.168.0.35/chaitu/magpierss/clock/";
include("$dir".$arr_file_names[$rnumb]);
echo "<a href=".$dir.$arr_file_names[$rnumb].">$arr_file_names[$rnumb]</a>";

at line3

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.