User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2004
Posts: 15
Reputation: Hjcooke is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Hjcooke Hjcooke is offline Offline
Newbie Poster

Help Random text display with image, link

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2005
Posts: 11
Reputation: 12345letsgo is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
12345letsgo's Avatar
12345letsgo 12345letsgo is offline Offline
Newbie Poster

Re: Random text display with image, link

  #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  
Join Date: Oct 2004
Location: San Francisco, CA
Posts: 338
Reputation: paradox814 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: Random text display with image, link

  #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

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

Re: Random text display with image, link

  #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  
Join Date: Oct 2004
Location: San Francisco, CA
Posts: 338
Reputation: paradox814 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: Random text display with image, link

  #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

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

Solution Re: Random text display with image, link

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

Solution Re: Random text display with image, link

  #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  
Join Date: Jul 2005
Posts: 2
Reputation: familyguy108464 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
familyguy108464's Avatar
familyguy108464 familyguy108464 is offline Offline
Newbie Poster

Re: Random text display with image, link

  #8  
Jul 17th, 2005
never mind figured it out
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 1:28 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC