954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to display random html pages using php

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>";
?>
ym_chaitu
Newbie Poster
18 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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>";
?>
network18
Practically a Master Poster
619 posts since Sep 2009
Reputation Points: 29
Solved Threads: 76
 

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>";

?>
ym_chaitu
Newbie Poster
18 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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].");
network18
Practically a Master Poster
619 posts since Sep 2009
Reputation Points: 29
Solved Threads: 76
 

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>";

?>
ym_chaitu
Newbie Poster
18 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

its right, line17 should have been like this -

include("$dir".clock."$arr_file_names[$rnumb]");
network18
Practically a Master Poster
619 posts since Sep 2009
Reputation Points: 29
Solved Threads: 76
 

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:

ym_chaitu
Newbie Poster
18 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

good, keep it up :)

network18
Practically a Master Poster
619 posts since Sep 2009
Reputation Points: 29
Solved Threads: 76
 
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/";
ym_chaitu
Newbie Poster
18 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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...

ym_chaitu
Newbie Poster
18 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

network18
Practically a Master Poster
619 posts since Sep 2009
Reputation Points: 29
Solved Threads: 76
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You