943,514 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 7319
  • PHP RSS
Mar 19th, 2005
0

Random text display with image, link

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hjcooke is offline Offline
15 posts
since Oct 2004
Mar 20th, 2005
0

Re: Random text display with image, link

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.)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
12345letsgo is offline Offline
11 posts
since Mar 2005
Mar 20th, 2005
0

Re: Random text display with image, link

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

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Mar 20th, 2005
0

Re: Random text display with image, link

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hjcooke is offline Offline
15 posts
since Oct 2004
Mar 21st, 2005
0

Re: Random text display with image, link

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

PHP Syntax (Toggle Plain Text)
  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:
PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Mar 21st, 2005
0

Re: Random text display with image, link

Many thanks thats brilliant, thanks for your time.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hjcooke is offline Offline
15 posts
since Oct 2004
Jul 17th, 2005
0

Re: Random text display with image, link

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
familyguy108464 is offline Offline
2 posts
since Jul 2005
Jul 18th, 2005
0

Re: Random text display with image, link

never mind figured it out
Reputation Points: 10
Solved Threads: 0
Newbie Poster
familyguy108464 is offline Offline
2 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Trying to create a login system
Next Thread in PHP Forum Timeline: PHP Installation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC