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)

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
5 like 0 dislike
1 answer
2 like 0 dislike
1 answer
9 like 1 dislike
2 answers
asked 1 year ago by william (91,210 points)