Jump to content
xisto Community
Sign in to follow this  
fsastraps

How Do I Mank A Random Quote Come Up Radomly? if anything is random......

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 = 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

Share this post


Link to post
Share on other sites

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 Arrayquotes[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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.


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

 

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.


Share this post


Link to post
Share on other sites

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)

 

 

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.


66195[/snapback]


 


 

This assumes that your web server supports PHP.


Share this post


Link to post
Share on other sites
THANK YOU!!How Do I Mank A Random Quote Come Up Radomly?

Thank you so very much!! I've been looking for how to do this for a while and all the ways I was trying just weren't working right. This one worked!!

-reply by Rebecca

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.