Random text display with image, link

Reply

Join Date: Oct 2004
Posts: 15
Reputation: Hjcooke is an unknown quantity at this point 
Solved Threads: 0
Hjcooke Hjcooke is offline Offline
Newbie Poster

Random text display with image, link

 
0
  #1
Mar 19th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 11
Reputation: 12345letsgo is an unknown quantity at this point 
Solved Threads: 0
12345letsgo's Avatar
12345letsgo 12345letsgo is offline Offline
Newbie Poster

Re: Random text display with image, link

 
0
  #2
Mar 20th, 2005
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.)
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: Random text display with image, link

 
0
  #3
Mar 20th, 2005
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

  1. $RANDOM_TXT_FILE = "sample_data.txt";
  2. if (file_exists($RANDOM_TXT_FILE))
  3. {
  4. $file = file($RANDOM_TXT_FILE);
  5. srand((double)microtime()*1000000);
  6. $random_line = rand(0,sizeOf($file));
  7. echo $file[$random_line];
  8. }
  9. //else - no file
  10. //no need to look like and idiot and print an error to our users
  11. //we should use fwrite and log this to a file instead
  12. //or incorporate some other way to log this error
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 15
Reputation: Hjcooke is an unknown quantity at this point 
Solved Threads: 0
Hjcooke Hjcooke is offline Offline
Newbie Poster

Re: Random text display with image, link

 
0
  #4
Mar 20th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: Random text display with image, link

 
0
  #5
Mar 21st, 2005
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

  1. 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>
  2. http://daniweb.com/
  3. image1.gif
  4.  
  5.  
  6. 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>
  7. http://www.daniweb.com
  8. image2.jpg
  9.  
  10.  
  11. 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>
  12. http://www.brangle.com/
  13. image3.jpg
  14.  

and here is the php code, which i just dumped into a function:
  1. <?
  2. function getRandomText()
  3. {
  4. $r = 0;
  5. $RANDOM_TXT_FILE = "sample_data.txt";
  6. if (@file_exists($RANDOM_TXT_FILE))
  7. {
  8. $file = @file($RANDOM_TXT_FILE);
  9.  
  10. srand((double)microtime()*1000000);
  11. $random = 5 * rand(0, (int) (sizeOf($file)/5));
  12. unset($r);
  13.  
  14. $r['text'] = $file[$random];
  15. $r['link'] = $file[++$random];
  16. $r['img'] = $file[++$random];
  17. }
  18. return $r;
  19. }
  20.  
  21.  
  22. $random_text = getRandomText();
  23. if (is_array($random_text))
  24. {
  25. echo "<a href=".$random_text['link']." ><img src=".$random_text['img']." ></a> - ".$random_text['text'];
  26. }
  27. ?>

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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 15
Reputation: Hjcooke is an unknown quantity at this point 
Solved Threads: 0
Hjcooke Hjcooke is offline Offline
Newbie Poster

Re: Random text display with image, link

 
0
  #6
Mar 21st, 2005
Many thanks thats brilliant, thanks for your time.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 2
Reputation: familyguy108464 is an unknown quantity at this point 
Solved Threads: 0
familyguy108464's Avatar
familyguy108464 familyguy108464 is offline Offline
Newbie Poster

Re: Random text display with image, link

 
0
  #7
Jul 17th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 2
Reputation: familyguy108464 is an unknown quantity at this point 
Solved Threads: 0
familyguy108464's Avatar
familyguy108464 familyguy108464 is offline Offline
Newbie Poster

Re: Random text display with image, link

 
0
  #8
Jul 18th, 2005
never mind figured it out
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum


Views: 5804 | Replies: 7
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC