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

i don't know if this will help, but have you considered using javascript to make random events? (it would be easy to make a random image accompany a link using js.)

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

$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

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

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

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

Many thanks thats brilliant, thanks for your time.

hi, nice.

One question though,

what if i want the code

<?
function getRandomText()
{
   $r = 0;
   $RANDOM_TXT_FILE = "sample_data.txt";

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

<?
function getRandomText()
{
   $r = 0;
   $RANDOM_TXT_FILE = "/episodes/sample_data.txt";

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

<?
function getRandomText()
{
   $r = 0;
   $RANDOM_TXT_FILE = "http://www.domain.com/episodes/sample_data.txt";

and it doesnt show anything at all in the index nor other pages.

Please help friend, i love this code, and really need it.

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.