Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Generate Random Phrases, ...from a txt file
snlildude87
post Mar 14 2005, 08:44 PM
Post #1


Moderator
***************

Group: Members
Posts: 2,325
Joined: 8-March 05
From: Mawson, Antarctica
Member No.: 4,254



<question>
I have a question on how to generate random phrases (facts, quotes, etc) which the server will randomly pick from txt file. Normally, I would use SSI to accomplish this task, but I recently added a blog (Nucleus) to my site which uses PHP (.php file). So my question is, how can you display random phrases in which the server will pick from a specified text file?

Thanks!
</end question>

I saw a thread very similar to my problem, but the code supplied by Inspired does not allow the server to select the phrase from a file. The only reason I want this is because I have over 100 quotes, and putting all 100 in a big php file would be a hassel.
Thread: http://www.trap17.com/forums/Random-Messag...ator-t7603.html

In case you're interested, this is how you can generate random facts using SSI:
CODE

#!/usr/bin/perl -wT
use strict;
my $quote;
open(QUOTES, "facts.txt") or die "Oops! Can't find quote file: $!";
srand;
rand($.) < 1 && ($quote = $_) while <QUOTES>;
print "Content-type: text/html\n\n";
print $quote;

Then you have a name the code above to facts.cgi and create a facts.txt file (or any name you wish) where you will store all your phrases. You must have one phrase per line.

After that, place the code
CODE
<!--#include virtual="cgi-bin/facts.cgi"-->
in a place in your web page that you want a chosen phrase to appear, change the permission of facts.cgi to 755, rename your web page to xxx.cgi, and upload the three files.
Go to the top of the page
 
+Quote Post
Blix
post Mar 14 2005, 09:27 PM
Post #2


Newbie [Level 2]
**

Group: Members
Posts: 36
Joined: 1-March 05
From: Portugal
Member No.: 4,075



Hi, you can do this very easy with javascript...

just an example:
Put the following text into the body tag of your website
<script LANGUAGE="JavaScript">

<!--
QuoteText = new Array(3);
QuoteText[0] = 'text1';
QuoteText[1] = 'text2';
QuoteText[2] = 'text3';

i = Math.floor(Math.random() * QuoteText.length);
document.write(QuoteText[i]);
//-->
</SCRIPT>
have fun
Blix
Go to the top of the page
 
+Quote Post
snlildude87
post Mar 14 2005, 10:54 PM
Post #3


Moderator
***************

Group: Members
Posts: 2,325
Joined: 8-March 05
From: Mawson, Antarctica
Member No.: 4,254



Blix, thanks for the reply, but the phrase would not show if a person has JavaScript disabled because JavaScript can sometimes be intrusive.

PHP, however, will always show because the server processes the request, not the browser.
Go to the top of the page
 
+Quote Post
OwrLam
post Mar 17 2005, 05:44 PM
Post #4


Newbie [Level 3]
***

Group: Members
Posts: 46
Joined: 17-March 05
Member No.: 4,610



One way:


CODE
<?
$file="base.txt"; # имя файла, где хранятся фразы

$array=file($file);
echo $array[rand(0,count($array)-1)];
?>
Go to the top of the page
 
+Quote Post
snlildude87
post Mar 17 2005, 09:51 PM
Post #5


Moderator
***************

Group: Members
Posts: 2,325
Joined: 8-March 05
From: Mawson, Antarctica
Member No.: 4,254



You, my friend, are THE man! laugh.gif

I case can't tell by my reaction, OwrLam's code works!

Thank you so much, and welcome to trap17. smile.gif
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Css Layout Generator - *free*(5)
  2. My File Manager Is Working(2)
  3. Import From Excel File Into Mysql Database(9)
  4. Transfer File Of Any Size Using Winsock Control(5)
  5. My Review Of Megaupload.com(25)
  6. Simple C File Handling In Action(3)
  7. How To Put Music In The Background Of A Powerpoint Presentation(10)
  8. Linux Question: Amarok And File Permissions(4)
  9. How To Open A .daa File(39)
  10. A Trap17 How-to Guide For Beginners(12)
  11. Read File (.txt) On Another Website Using Jsp?(3)
  12. Need To Shrink The File Size Of Web Image? - Photoshop Tricks(3)
  13. Hate Trying To Do File Sharing On Vista(2)
  14. Windows Ntfs Folder And File Compression. Good Or Bad?(6)
  15. Question Regarding File Transfer!(4)
  1. Php Configuration File(16)
  2. Defraggler - Free Software To Defrag Your File(12)
  3. A Good File Explorer For Windows Xp?(6)
  4. File Manager Not Working(2)
  5. Debug Exe Files(4)
  6. Where Is The Bookmarks File Stored With Ff2?(4)
  7. Free Software For File Recovery(4)
  8. Internal File Transfer(5)
  9. File Sharing Hosts!(3)
  10. File Sharing Hosts!(0)
  11. How To Extract The Audio From Youtube Videos(2)
  12. Xampp How Can I Generate "automatic Start Sites"(6)
  13. Just A Random Poem. . .(0)


 



- Lo-Fi Version Time is now: 5th September 2008 - 11:55 PM