Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

How Do I Mank A Random Quote Come Up Radomly?


11 replies to this topic

#1 fsastraps

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 224 posts

Posted 29 March 2005 - 03:46 AM

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.

#2 no9t9

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 773 posts

Posted 29 March 2005 - 04:24 AM

<?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.

#3 snlildude87

    Moderator

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2,325 posts
  • Location:Mawson, Antarctica

Posted 29 March 2005 - 04:27 AM

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

:)

#4 round

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 463 posts

Posted 29 March 2005 - 04:41 AM

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

#5 spacemonkey

    Member [Level 2]

  • Kontributors
  • PipPipPipPipPip
  • 76 posts
  • Location:USA

Posted 29 March 2005 - 06:55 AM

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

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.

#6 no9t9

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 773 posts

Posted 29 March 2005 - 07:04 AM

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];
?>

#7 fsastraps

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 224 posts

Posted 30 March 2005 - 03:08 AM

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

#8 no9t9

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 773 posts

Posted 30 March 2005 - 04:41 AM

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.

#9 spacemonkey

    Member [Level 2]

  • Kontributors
  • PipPipPipPipPip
  • 76 posts
  • Location:USA

Posted 31 March 2005 - 05:23 AM

no9t9, on Mar 29 2005, 11:41 PM, said:

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.

View Post

This assumes that your web server supports PHP.

#10 Spectre

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 873 posts

Posted 31 March 2005 - 07:13 AM

Well, I screwed that one up good and proper. Please ignore all of my posts within this thread, and could a moderator please delete the above? Thanks.




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users