Javascript Random qoute

taha116

Member
Messages
505
Reaction score
0
Points
16
So this is what im basically gonna use for a random tips section in my MMORPG, What i want to know is
Am i using a method that you wouldn't suggest? Could anyone comeup or suggest a method that wont
require a link to someone elses site?

Code:
<script language="JavaScript">
// ==============================================
// Copyright 2004 by CodeLifter.com
// Free for all; but please leave in this header.
// ==============================================

var Quotation=new Array() // do not change this!

// Set up the quotations to be shown, below.
// To add more quotations, continue with the
// pattern, adding to the array.  Remember
// to increment the Quotation[x] index!

Quotation[0] = "Time is of the essence! Comb your hair.";
Quotation[1] = "Sanity is a golden apple with no shoelaces.";
Quotation[2] = "Repent! The end is coming, $9.95 at Amazon.";
Quotation[3] = "Honesty blurts where deception sneezes.";
Quotation[4] = "Pastry satisfies where art is unavailable.";
Quotation[5] = "Delete not, lest you, too, be deleted.";
Quotation[6] = "O! Youth! What a pain in the backside.";
Quotation[7] = "Wishes are like goldfish with propellors.";
Quotation[8] = "Love the river's \"beauty\", but live on a hill.";
Quotation[9] = "Invention is the mother of too many useless toys.";

// ======================================
// Do not change anything below this line
// ======================================
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
The JS solution you posted has 3 disadvantages: higher network utilization, requires the browser to support JS and requires editing the source to add or change quotes.

You could pick the quote server side. Advantages: works if JS is disabled or not supported by the browser and uses less bandwidth. Disadvantage: requires ever so slightly more processing on the server, but not enough to be noticeable, even with hundreds (10**3? 10**4?) of queries per second.

Note that caching will help with network utilization.
 
Last edited:

gomarc

Member
Messages
516
Reaction score
18
Points
18
Based on misson suggestions, you may find this php code useful.

Create a txt file with all your quotes/tips. One line per tip. Something like this:

tips.txt
Code:
This is a "test"
Turn on your computer before you start typing.
Go to bed early
Right-click on your desktop and select New > Folder to create a new folder
Press Change Icon in the Customize tab and select one that's better looking
It’s a good practice to give rep+ to those who help you.
In the same directory, lets test some php and see how it works.

showtip_test.php
PHP:
<?php
//Read entire file into an array
$text = file('tips.txt');

// Loop through array
foreach ($text as $line_num => $line) {
    echo "Tip #<b>{$line_num}</b> : " . $line . "<br />\n";
}

//Get the total number of items in the array
$totaltips = count($text);

echo "<br />\n";
echo "Total # of tips : <b>{$totaltips}</b>  <br />\n";

//mt_rand : Generate a random value between 0 and total tips
$randompick = mt_rand(0, ($totaltips)-1);

echo "<br />\n";
echo "Random number selected : " . $randompick . "<br />\n";

echo "<br />\n";
echo "Display the tip #" . $randompick . " : <br />\n";
echo $text[$randompick]; 

echo "<br />\n";


?>
Call showtip_test.php refreshing the page several times and see if this is what you want

If it works for you, here you have a condensed version, just echoing what you need:

showtip.php
PHP:
<?php
$tiptext = file('tips.txt');
echo $tiptext[mt_rand(0, (count($tiptext))-1)]; 
?>
You can easily edit your tips.txt to include as many as you want and showtip.php will echo one of those at random.

If you have an html/php page where you want to display the tip, here is how you can use it:

example.php
PHP:
<?php

//You can have other php code here

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <title></title>
  </head>
  <body>
  <p>Let me give you this tip:</p>
  <p><h2><?php include('showtip.php');?></h2></p>
  </body>
</html>
 

taha116

Member
Messages
505
Reaction score
0
Points
16
Thank u gormac and mission. im gona try it out ..

BTW @gor did u write that code urself?
 
Last edited:

gomarc

Member
Messages
516
Reaction score
18
Points
18
Yes, except for the ‘// Loop through array’ part that I got from somewhere else, sometime ago, probably php.net but I don’t remember. So if you like it, you can use it with no problem.
 

taha116

Member
Messages
505
Reaction score
0
Points
16
Ok, Thank you very very much gomarc

im backing up my site, as i havent done it in a couple days and every day there are atleast half a dozen major changes so for me a couple days of work is allot and then im using your script
 
Last edited:

gomarc

Member
Messages
516
Reaction score
18
Points
18
Thanks for the credits, taha116 ! Good luck with your project.
 

taha116

Member
Messages
505
Reaction score
0
Points
16
No problem Im editing my files now
Edit:
Hey im having some trouble i made the files in the right directories and the test worked with the condensed one skipped the other.

<?php



******* = <<<END
You are exploring the map, and nothing has happened. Continue exploring using the direction buttons or the Travel To menus.
<p align=center>

<!-- Begin: AdBrite, Generated: 2009-07-04 19:37:33 -->
<script type="text/javascript">
var AdBrite_Title_Color = '191919';
var AdBrite_Text_Color = '000000';
var AdBrite_Background_Color = 'FFFFFF';
var AdBrite_Border_Color = 'CCCCCC';
var AdBrite_URL_Color = '7F7F7F';
try{var AdBrite_Iframe=window.top!=window.self?2:1;var AdBrite_Referrer=document.referrer==''?document.location:document.referrer;AdBrite_Referrer=encodeURIComponent(AdBrite_Referrer);}catch(e){var AdBrite_Iframe='';var AdBrite_Referrer='';}
</script>
<script type="text/javascript">document.write(String.fromCharCode(60,83,67,82,73,80,84));document.write(' src="http://ads.adbrite.com/mb/text_group.php?sid=1242313&zs=3330305f323530&ifr='+AdBrite_Iframe+'&ref='+AdBrite_Referrer+'" type="text/javascript">');document.write(String.fromCharCode(60,47,83,67,82,73,80,84,62));</script>
<div><a target="_top" href="http://www.adbrite.com/mb/commerce/purchase_form.php?opid=1242313&afsid=1" style="font-weight:bold;font-family:Arial;font-size:13px;">Your Ad Here</a></div>
<!-- End: AdBrite -->
</p><p align=center><strong>Random Tips</strong><br>

<script language="JavaScript">

var Quotation=new Array()

Quotation[0] = "Hope youre reading these tips! They might just save your life!";
Quotation[1] = "Make sure you get to the purple dot on the map eventaully, thats your primary quest.";
Quotation[2] = "Make sure you dont move on right after you kill a monster, always check if there is something usefull it left for you.";
Quotation[3] = "Playing at higher dificulites may be harder but its more rewarding as well.";
Quotation[4] = "Having trouble?, use the support forums.";
Quotation[5] = "You can play with up to 3 charecters per account you know...";
Quotation[6] = "Dont travel to far away to quickly, the monsters get stronger.";
Quotation[7] = "Keep an eye on your health bar, the monsters health may be low but yours could be lower.";
Quotation[8] = "Visit the official site were you will have acess to the official library wich contains some intresting data on monsters";
Quotation[9] = "Dont pick up a item if your carying one of the same type you like better or it will overwrite your old one";
Quotation[10] = "Save your extra money in the bank, if you die you will lose all of the money you are carrying.";
Quotation[11] = "My spelling sucks, i know... Drop by the support forums and report it as a bug.";
Quotation[12] = "Somtimes traveling to places that look empty might actually lead you to quests... That is usally rare though, try to record the location and post or shout it for other people who might be interested.";

var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>


</p>

END;
?>
Can u throw in the right peace of code with the right syntax as it dosent seem to work for me and im probably just doin somthin wrong. Ill use whatever u put in and let u know if it works
 
Last edited:

gomarc

Member
Messages
516
Reaction score
18
Points
18
Replace all the javascript you have bolded


Code:
<script language="JavaScript">

var Quotation=new Array()

Quotation[0] = "Hope youre reading these tips! They might just save your life!";
Quotation[1] = "Make sure you get to the purple dot on the map eventaully, thats your primary quest.";
Quotation[2] = "Make sure you dont move on right after you kill a monster, always check if there is something usefull it left for you.";
Quotation[3] = "Playing at higher dificulites may be harder but its more rewarding as well.";
Quotation[4] = "Having trouble?, use the support forums.";
Quotation[5] = "You can play with up to 3 charecters per account you know...";
Quotation[6] = "Dont travel to far away to quickly, the monsters get stronger.";
Quotation[7] = "Keep an eye on your health bar, the monsters health may be low but yours could be lower.";
Quotation[8] = "Visit the official site were you will have acess to the official library wich contains some intresting data on monsters";
Quotation[9] = "Dont pick up a item if your carying one of the same type you like better or it will overwrite your old one";
Quotation[10] = "Save your extra money in the bank, if you die you will lose all of the money you are carrying.";
Quotation[11] = "My spelling sucks, i know... Drop by the support forums and report it as a bug.";
Quotation[12] = "Somtimes traveling to places that look empty might actually lead you to quests... That is usally rare though, try to record the location and post or shout it for other people who might be interested.";

var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>
With this 1 line:

Code:
<?php include('showtip.php');?>
Please note that the files tips.txt and showtip.php must be in the same directory as where you want to call the tips.

To keep things more organized, you could create a sub-directory named “tips” and put the tips.txt and showtip.php files in there. In this case, your call to the tips would be:

Code:
<?php include('tips/showtip.php');?>
 
Last edited:
Top