Questionaries.org
Login
Register
Questions
Unanswered
Tags
Users
Ask a Question
Welcome to Questionaries, where you can ask questions and receive answers from other members of the community.
Let us know at info@questionaries.org
Using Switch to Display Random Quotes?
10
like
0
dislike
Using Switch to Display Random Quotes?
asked
1 year
ago
by
monirulislam
(
51,620
points)
using
switch
display
random
quotes
2 Answers
1
like
0
dislike
Best answer
Try this :
<?
//Chooses a random number
$num = Rand (1,6);
//Based on the random number, gives a quote
switch ($num)
{
case 1:
echo "Time is money";
break;
case 2:
echo "An apple a day keeps the doctor away";
break;
case 3:
echo "Elmo loves dorthy";
break;
case 4:
echo "Off to see the wizard";
break;
case 5:
echo "Tomorrow is another day";
break;
case 6:
echo "PHP is cool!";
}
?>
To add more quotes you would simply change the rand () function to allow for higher numbers, and then add in their corresponding cases below.
If you wanted to include a random quote on your PHP web page, you could simply use include () to pull the quote from this file, like this:
INCLUDE '
http://www.yoursite.com/path/to/quote_file.php'
answered
1 year
ago
by
monirulislam
(
51,620
points)
0
like
0
dislike
This shows you how to create a random number in Javascript:
http://www.javascriptkit.com/javatutors/randomnum.shtml
That random number would allow you to go trough your array of quotes. That array can be filled by reading from a file:
http://www.javascripter.net/faq/reading2.htm#top
Although I suggest just filling an array in another .js file and then including it in your html file like this:
<script type="text/javascript" src="quotes.js">
in quotes.js:
javascript Syntax (Toggle Plain Text)
1.
var quotes = new Array(100);
2.
quotes[0]="SomeRandomQuote";
3.
quotes[1]="AnotherRandomQuote";
4.
quotes[2]="GuessWhat";
5.
// ... Etcetera
6.
function returnQuote(randomNumber) {
7.
return quotes[randomNumber];
8.
}
var quotes = new Array(100); quotes[0]="SomeRandomQuote"; quotes[1]="AnotherRandomQuote"; quotes[2]="GuessWhat"; // ... Etcetera function returnQuote(randomNumber) { return quotes[randomNumber]; }
This would mean everytime you want a random quote you just generate the number and call returnQuote(randomNum) with it.
EDIT: example to help you on the way : - )
html Syntax (Toggle Plain Text)
1.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "
http://www.w3.org/TR/html4/strict.dtd">
2.
<html>
3.
<head>
4.
<script type="text/javascript">
5.
var quotes = new Array(100);
6.
quotes[0]="SomeRandomQuote";
7.
quotes[1]="AnotherRandomQuote";
8.
quotes[2]="GuessWhat";
9.
// ... Etcetera
10.
function returnQuote(randomNumber) {
11.
return quotes[randomNumber];
12.
}
13.
</script>
14.
</head>
15.
16.
<body>
17.
<div id="randomQuote" style="width: 200px; height: 300px; background-color: #bbbbbb;" onClick="javascript:getElementById('randomQuote').innerHTML = returnQuote('1');">
18.
</div>
19.
</body>
20.
</html>
answered
1 year
ago
by
marck_don
(
191,010
points)
Related questions
3
like
0
dislike
1
answer
How to display latest product in magento?
asked
1 year
ago
by
marck_don
(
191,010
points)
magento
product
display
5
like
0
dislike
1
answer
How can you switch from one to another ?
asked
1 year
ago
by
rad_joshep
(
19,390
points)
multiple
database
machine
switch
oracle
2
like
0
dislike
1
answer
Do you have any idea about "double quotes" and "Single quotes" in PHP..
asked
1 year
ago
by
eagle09
(
95,490
points)
double
quotes
single
php
9
like
1
dislike
2
answers
Great programming quotes
asked
1 year
ago
by
william
(
91,210
points)
subjective
polls
fun
quotes
offtopic
13
like
0
dislike
1
answer
is this right : Converting specific javascript commands to php such as math.random()
asked
1 year
ago
by
webmaster
(
25,340
points)
this
right
specific
javascript
commands
php
such
math
random