• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

simple text quote or banner rotator

I'd like to find a simple application (run on linux) to rotate entries in a text file on my page.  I have a bunch of quotes and I'd like them to rotate every x seconds.

Anyone know of anything simple and (preferably) free?
0
marglar
Asked:
marglar
  • 5
  • 4
1 Solution
 
MasonWolfCommented:
The code below assumes that your text file uses newline character to separate quotes. If this is not the case, simply replace the character in the explode function call with whatever you do use.


<script type="text/javascript">
var quotes = new Array();
<?php
$indx = 0;
$quotes = explode("\n", file_get_contents("quotes.txt"));
$init_quote = array_rand($quotes);
foreach( $quotes AS $quote)
{
      echo "quotes[$indx]='".str_replace("'","\'",$quote)."';\n";
            $indx++;
}
?>
function quote_switch()
{
   var rand=Math.floor(Math.random()*quotes.length);
   document.getElementById('quote').innerHTML=quotes[rand];
}
setInterval("quote_switch()", 20000);
</script>
<div id="quote"><?php echo htmlspecialchars($init_quote); ?></div>
0
 
marglarAuthor Commented:
wow!  thanks!

now for the confession... I'm a complete neophyte.  

I'm assuming the <script> part goes in the head and the <div> goes where I want the quotes.  Is that right?

One question.  Whats a new line character?

Again, thanks!
0
 
MasonWolfCommented:
It is irrelevant whether the <script> part is above or below the <div> part. I put it that way to make it more readable (you're welcome, by the way :D ).

A newline character is the ASCII representation of this:
"
"
(it's the bit between the quotes)

There is another way of separating two lines which looks identical, and that's with the line feed character, represented as "\r".

It is important to use double quotes for both the newline and line feed character representations. Single quotes will just give you a slash followed by the letter n or the letter r.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
marglarAuthor Commented:
whew!  Ok!

so, just to make sure I understand...

In my quotes.txt file I should put a \r at the end of each discrete quote?

this is quote #1 /r
This is quote #2 /r

like that?
(sorry to be dense)
0
 
MasonWolfCommented:
No, that's not what I meant. In php, a double-quoted string that includes \r is equivalent to a linefeed. In a text document separating two lines with the enter key is equivalent to a linefeed. Now, whether it's technically equivalent to \r or \n depends on your text editor. It makes sense to just test both ways to see which works.
0
 
marglarAuthor Commented:
uhhhh.   Ok, now I feel totally stupid.  I have no idea what we are discussing...

I'm cool to drop your script into my page where I want to show the quotes.  That part is straight forward for me.

I see that I can put my quotes into quotes.txt (or change that parameter in the script to what I want).

What I don't understand is how the script can tell one quote from another.  What should quotes.txt look like?

Thanks a million for all the help!

0
 
MasonWolfCommented:
A bird in the hand is worth two in the bush.
All good things must come to an end.
All's well that ends well.
As you sow, so shall you reap
...
Zounds, Watson! The chase is afoot!

Does that make sense? (alphabetizing is totally up to you)
0
 
marglarAuthor Commented:
Ok then.  I just use plain old carriage returns?

I assume if I want a line break in the quote itself that I should use a <br>..  


Thanks so much for all of your help (and patients)


0
 
MasonWolfCommented:
Yes that's right. You're welcome.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now