Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> How Do I Mank A Random Quote Come Up Radomly?, if anything is random......
fsastraps
post Mar 29 2005, 03:46 AM
Post #1


Super Member
*********

Group: Members
Posts: 224
Joined: 22-February 05
Member No.: 3,909



Well i was just wondering b/c i have a lot of quotes that i would like to put on my site, and i was wondering how i could do is so that there are quotes that will come up randomly? If anybody can help me, i would greately appreciate it, thanks.
Go to the top of the page
 
+Quote Post
no9t9
post Mar 29 2005, 04:24 AM
Post #2


Privileged Member
*********

Group: Members
Posts: 773
Joined: 4-November 04
Member No.: 2,118



<?php $rdm=rand(1,3);
if ($rdm==1) $quote="quote text 1";
else if ($rdm==2) $quote="quote text 2";
else if ($rdm==3) $quote="quote text 3";
echo $quote; ?>

the statement $rdm=rand(1,3); basically generates a random number between 1 and 3. Change the 3 to suit your needs.

this will work but may not be ideal if you have A LOT of quotes.
Go to the top of the page
 
+Quote Post
snlildude87
post Mar 29 2005, 04:27 AM
Post #3


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

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



n09t9's method will not work if you have a huge amount of quotes, so I have just the method for you! Luckily, I asked this question not too long ago, and you can look at my question here: http://www.trap17.com/forums/generate-rand...ases-t8488.html

smile.gif
Go to the top of the page
 
+Quote Post
round
post Mar 29 2005, 04:41 AM
Post #4


Super Member
*********

Group: Members
Posts: 463
Joined: 8-November 04
Member No.: 2,186



You didn't mention what kind of script you were looking for so this is javascript one.

Here's your script - copy and paste then name the file quotes.js

function makeArray(len) {
for (var i = 0; i < len; i++) this[i] = null;
this.length = len;
}

// you add as many quotes as you want here just make sure to change the makeArray(number)

ideas = new makeArray(22);
ideas[0] = "1";
ideas[1] = "2"
ideas[2] = "3"
ideas[3] = "4"
ideas[4] = "5"
ideas[5] = "6"
ideas[6] = "7"
ideas[7] = "8"
ideas[8] = "9"
ideas[9] = "10"
ideas[10] = "11"
ideas[11] = "12"
ideas[12] = "13"
ideas[13] = "14"
ideas[14] = "15"
ideas[15] = "16"
ideas[16] = "17"
ideas[17] = "18"
ideas[18] = "19"
ideas[19] = "20"
ideas[20] = "21"
ideas[21] = "22"

// The random number generator.
function rand(n) {
seed = (0x015a4e35 * seed) % 0x7fffffff;
return (seed >> 16) % n;
}
var now = new Date()
var seed = now.getTime() % 0xffffffff




Here's your html doc
<HTML>
<HEAD>
<script src="quotes.js"></script>
</HEAD>
<BODY>

<script LANGUAGE = "JavaScript">
// this you put into you html doc where you want your quotes to appear
document.write(ideas[rand(ideas.length)])
</SCRIPT>

</BODY>
</HTML>

round
Go to the top of the page
 
+Quote Post
spacemonkey
post Mar 29 2005, 06:55 AM
Post #5


Member [Level 2]
*****

Group: Members
Posts: 76
Joined: 2-March 05
From: USA
Member No.: 4,110



Here is an easy way to make a random quote or block of HTML code appear on your page quite easily with javascript:

CODE
var a = Math.random() + ""
var rand1 = a.charAt(5)
quotes = new Array
quotes[1] = "QUOTE 1"
quotes[2] = "QUOTE 2"
quotes[3] = "QUOTE 3"
quotes[4] = "QUOTE 4"
quotes[5] = "QUOTE 5"
quotes[6] = "QUOTE 6"
quotes[7] = "QUOTE 7"
quotes[8] = "QUOTE 8"
quotes[9] = "QUOTE 9"
quotes[0] = "QUOTE 10"
var quote = quotes[rand1]
document.write(quote);


Even though this code is simple, there are two downsides to using it: 1. You can only have up to 10 quotes (because the random number is retrieved from a single character in a string of numbers) and 2. Unless you know some Javascript and can do some simple modifications, you would have to place this whole script in the body of your HTML document where you would want it to appear. So you wouldn't be able to call it externally (and would, hence, clutter your HTML). But, like I said before, you could make this script do that. I could change it for you but I am too lazy. I can not be bothered.
Go to the top of the page
 
+Quote Post
no9t9
post Mar 29 2005, 07:04 AM
Post #6


Privileged Member
*********

Group: Members
Posts: 773
Joined: 4-November 04
Member No.: 2,118



if you want to use files (PHP) it is better for A LOT of quotes. I wasn't sure how many you have. IF you save the quotes in a text file (one quote per line). You can use the following code.

<?php
$quote=file("quotes.txt");
$totalnumberofquotes=count($quote);
$rdm=rand(1,$totalnumberofquotes);
echo $quote[$rdm];
?>
Go to the top of the page
 
+Quote Post
fsastraps
post Mar 30 2005, 03:08 AM
Post #7


Super Member
*********

Group: Members
Posts: 224
Joined: 22-February 05
Member No.: 3,909



well, thanks for all of theses, but i cant seem to make any of them work. Or is it because they only work from a server and not in my computer? Or do i have to save the file on were im putting them at as an .shtml? i dont know, maybe im just not doing it right.....If someone could help i would appreciate it.
thanks
Go to the top of the page
 
+Quote Post
no9t9
post Mar 30 2005, 04:41 AM
Post #8


Privileged Member
*********

Group: Members
Posts: 773
Joined: 4-November 04
Member No.: 2,118



PHP code does not display in frontpage or other WYSIWYG editors. You should upload the files to your web space.

(1) Save this code directly in your HTML file.

QUOTE
<?php
$quote=file("quotes.txt");
$totalnumberofquotes=count($quote);
$rdm=rand(1,$totalnumberofquotes);
echo $quote[$rdm];
?>


(2) rename the HTML file so that the extension is .php

(3) Upload the file to your webspace

(4) upload the quote.txt file to your webspace (same directory as the php file)

(5) access the php file by typing http://yourwebsite.com/quote.php

Note: if you are putting this code in your index file, you must rename your index.html file to index.php and delete the old index.html or index.htm.
Go to the top of the page
 
+Quote Post
spacemonkey
post Mar 31 2005, 05:23 AM
Post #9


Member [Level 2]
*****

Group: Members
Posts: 76
Joined: 2-March 05
From: USA
Member No.: 4,110



QUOTE(no9t9 @ Mar 29 2005, 11:41 PM)
PHP code does not display in frontpage or other WYSIWYG editors.  You should upload the files to your web space.

(1) Save this code directly in your HTML file.
(2) rename the HTML file so that the extension is .php

(3) Upload the file to your webspace

(4) upload the quote.txt file to your webspace (same directory as the php file)

(5) access the php file by typing http://yourwebsite.com/quote.php

Note: if you are putting this code in your index file, you must rename your index.html file to index.php and delete the old index.html or index.htm.
*


This assumes that your web server supports PHP.