•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 426,018 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,709 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 4199 | Replies: 7
![]() |
•
•
Join Date: Oct 2004
Posts: 15
Reputation:
Rep Power: 5
Solved Threads: 0
Hi, im creating a website at the moment and i want to use this php code to create random effects on my page when its refrshed. Im using this php code which enetrs random text into a web page stored rom a flat file. Can any one help me, and customise it further so a corresponding image and even a link can be displayed as well.
<?php
$RANDOM_TXT_FILE = "sample_data.txt";
srand((double)microtime()*1000000);
if (file_exists($RANDOM_TXT_FILE)) {
$arry_txt = preg_split("/--NEXT--/", join('', file($RANDOM_TXT_FILE)));
echo $arry_txt[rand(0, sizeof($arry_txt) -1)];
} else {
echo "Error: can't open $RANDOM_IMG_FILE file";
}
?>
Man.y thanks
<?php
$RANDOM_TXT_FILE = "sample_data.txt";
srand((double)microtime()*1000000);
if (file_exists($RANDOM_TXT_FILE)) {
$arry_txt = preg_split("/--NEXT--/", join('', file($RANDOM_TXT_FILE)));
echo $arry_txt[rand(0, sizeof($arry_txt) -1)];
} else {
echo "Error: can't open $RANDOM_IMG_FILE file";
}
?>
Man.y thanks
•
•
Join Date: Oct 2004
Location: San Francisco, CA
Posts: 338
Reputation:
Rep Power: 4
Solved Threads: 2
something else you could do (since i've never used preg_split)....
this requires that every new line have the code that you want on it so if your content spans two lines, then make sure you check if $random_line is even, if not then add one to it, etc..., etc...
show me a few lines of your file and i can see if ican help modify this code to better suit your needs
this requires that every new line have the code that you want on it so if your content spans two lines, then make sure you check if $random_line is even, if not then add one to it, etc..., etc...
show me a few lines of your file and i can see if ican help modify this code to better suit your needs
$RANDOM_TXT_FILE = "sample_data.txt";
if (file_exists($RANDOM_TXT_FILE))
{
$file = file($RANDOM_TXT_FILE);
srand((double)microtime()*1000000);
$random_line = rand(0,sizeOf($file));
echo $file[$random_line];
}
//else - no file
//no need to look like and idiot and print an error to our users
//we should use fwrite and log this to a file instead
//or incorporate some other way to log this error•
•
Join Date: Oct 2004
Posts: 15
Reputation:
Rep Power: 5
Solved Threads: 0
Here is the first few lines of mt data file.
Great family holidays at Haven Book your UK holiday or short break today at Haven and
British Holidays. Visit our website today for a great family holiday. Fantastic offers
at 38 great UK parks.<BR>
--NEXT--
La Manga Club luxury villas & apartments, Spain La Manga Club luxury villas and
apartments to rent including the exclusive Los Olivos development with indoor pool,
jacuzzi, sauna and spa. 3 championship golf courses on this 5 star resort.<BR>
--NEXT--
Discount cruises with Ocean Village Discover the best of the Mediterranean and Caribbean
aboard the biggest, newest and best-equipped ship in its class - Ocean Village. It's
cruising gone casual!.<BR>
MAny thanks
Great family holidays at Haven Book your UK holiday or short break today at Haven and
British Holidays. Visit our website today for a great family holiday. Fantastic offers
at 38 great UK parks.<BR>
--NEXT--
La Manga Club luxury villas & apartments, Spain La Manga Club luxury villas and
apartments to rent including the exclusive Los Olivos development with indoor pool,
jacuzzi, sauna and spa. 3 championship golf courses on this 5 star resort.<BR>
--NEXT--
Discount cruises with Ocean Village Discover the best of the Mediterranean and Caribbean
aboard the biggest, newest and best-equipped ship in its class - Ocean Village. It's
cruising gone casual!.<BR>
MAny thanks
•
•
Join Date: Oct 2004
Location: San Francisco, CA
Posts: 338
Reputation:
Rep Power: 4
Solved Threads: 2
here is the copy of the text file i used,
line one: entire text ad
line two: link to website
line three: image link
all new ads must start on lines 1,6,11,....
5 lines between each ad, which gives you two empty slots to grow into
and here is the php code, which i just dumped into a function:
i sure hope this makes sense, if you start making this any more complicated then I stronly recomment moving this into a database otherwise this will work. if you have any questions with any of the lines of code above let me know and i will explain it to the best of my knowledge
line one: entire text ad
line two: link to website
line three: image link
all new ads must start on lines 1,6,11,....
5 lines between each ad, which gives you two empty slots to grow into
Great family holidays at Haven Book your UK holiday or short break today at Haven and British Holidays. Visit our website today for a great family holiday. Fantastic offers at 38 great UK parks.<BR> http://daniweb.com/ image1.gif La Manga Club luxury villas & apartments, Spain La Manga Club luxury villas and apartments to rent including the exclusive Los Olivos development with indoor pool, jacuzzi, sauna and spa. 3 championship golf courses on this 5 star resort.<BR> http://www.daniweb.com image2.jpg Discount cruises with Ocean Village Discover the best of the Mediterranean and Caribbean aboard the biggest, newest and best-equipped ship in its class - Ocean Village. It'scruising gone casual!.<BR> http://www.brangle.com/ image3.jpg
and here is the php code, which i just dumped into a function:
<?
function getRandomText()
{
$r = 0;
$RANDOM_TXT_FILE = "sample_data.txt";
if (@file_exists($RANDOM_TXT_FILE))
{
$file = @file($RANDOM_TXT_FILE);
srand((double)microtime()*1000000);
$random = 5 * rand(0, (int) (sizeOf($file)/5));
unset($r);
$r['text'] = $file[$random];
$r['link'] = $file[++$random];
$r['img'] = $file[++$random];
}
return $r;
}
$random_text = getRandomText();
if (is_array($random_text))
{
echo "<a href=".$random_text['link']." ><img src=".$random_text['img']." ></a> - ".$random_text['text'];
}
?>
i sure hope this makes sense, if you start making this any more complicated then I stronly recomment moving this into a database otherwise this will work. if you have any questions with any of the lines of code above let me know and i will explain it to the best of my knowledge
hi, nice.
One question though,
what if i want the code
[PHP]<?
function getRandomText()
{
$r = 0;
$RANDOM_TXT_FILE = "sample_data.txt";[/PHP]
see the sample_data.txt well mine is in another folder like episodes/episodesmenu.txt but also when others go to other pages, i want it to load the same thing but it comes out blank. I mean in the index page it works fine because it actually finds the episodes/ folder and text, but when im in page like http://www.domain.com/episodes/pullingtogether.php it wont show because it is looking for the directory episodes/ and text file. i tried adding a slash to the code like so
[PHP]<?
function getRandomText()
{
$r = 0;
$RANDOM_TXT_FILE = "/episodes/sample_data.txt";[/PHP]
still no luck, it doesnt load in the index nor other pages.
i also tried and put the url of the site in there like so
[PHP]<?
function getRandomText()
{
$r = 0;
$RANDOM_TXT_FILE = "http://www.domain.com/episodes/sample_data.txt";[/PHP]
and it doesnt show anything at all in the index nor other pages.
Please help friend, i love this code, and really need it.
One question though,
what if i want the code
[PHP]<?
function getRandomText()
{
$r = 0;
$RANDOM_TXT_FILE = "sample_data.txt";[/PHP]
see the sample_data.txt well mine is in another folder like episodes/episodesmenu.txt but also when others go to other pages, i want it to load the same thing but it comes out blank. I mean in the index page it works fine because it actually finds the episodes/ folder and text, but when im in page like http://www.domain.com/episodes/pullingtogether.php it wont show because it is looking for the directory episodes/ and text file. i tried adding a slash to the code like so
[PHP]<?
function getRandomText()
{
$r = 0;
$RANDOM_TXT_FILE = "/episodes/sample_data.txt";[/PHP]
still no luck, it doesnt load in the index nor other pages.
i also tried and put the url of the site in there like so
[PHP]<?
function getRandomText()
{
$r = 0;
$RANDOM_TXT_FILE = "http://www.domain.com/episodes/sample_data.txt";[/PHP]
and it doesnt show anything at all in the index nor other pages.
Please help friend, i love this code, and really need it.
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the PHP Forum
- Previous Thread: Trying to create a login system
- Next Thread: PHP Installation


Linear Mode