Jump to content

How to make a "Random Quote"/"Tip of the Day" widget


Recommended Posts

Hello,

So the following is really just a bit of PHP code I found and have been using for a while that lets you create a custom widget which picks and displays a random line of information from a file.  Random quotes can be very useful for engaging your members obliquely - giving them food for thought or ideas they may carry with them throughout the day after they leave your site, or they may even become topics for discussion or debate in your community.  Random tips are extremely useful for those of us (like myself) who don't yet have time to develop FAQs or "how-to"s for the suite's many functions.  You can use this to offer useful tips unobtrusively to your members, giving them a chance to explore the site's functionality while they browse your community.

I'm sure there are several similar solutions out there, but this is what I've been using for some time now and it works just fine.  I offer this as-is, with no promise of troubleshooting or guarantees.  This code is not mine and I do not take credit for writing it.  If you have problems you can respond to this post and I'll help if I can, but I do not guarantee any technical support - I'm just sharing what works for me.

You will need Pages in your suite in order to use this tip.  You will also need access to your site or server's folder directory (e.g. through CPanel or equivalent).

So:

  1. Go to Pages in the AdminCP and click Blocks.  Then click the the Create New Block button.  For Type, click Custom; in the Content Editor category, select Manual PHP.
  2. On the next (Create New Block) screen, fill out the information as you want (name, description, key, etc) in the Details tab.  When finished, click the Content tab and copy/paste the following code into the text box:
     
    $file= "yourfile.txt";
    $quotes = file($file);
    srand((double)microtime()*1000000);
    $randomquote = rand(0, count($quotes)-1);
    echo $quotes[$randomquote];

    do not enable caching as this will be a dynamic widget whose content will refresh every time the widget is loaded.

  3. Save your new custom widget.

 

The next thing you need to do is create the file referenced in the first line ("yourfile.txt" for our purposes; name it whatever you want, but make sure the first line matches the file you're about to create!).  With these default settings, this file needs to be in your root (public_html) directory.

So make a blank yourfile.txt, and to add an item of data simply edit the file and add a line with <div> tags.  For example, to add a quote:

<div style="font-family: Georgia;">Life can only be understood backwards, but it must be lived forwards. - <i>S0REN KIERKEGAARD</i></div>

One instance per line.  Change the style to suit your needs with whatever on-the-fly code you like.

One more example I use: to insert a random tip on using the IPB suite, just create another custom widget using the above directions, and rename the file to something different (tips.txt or whatever).  You can do something like this:

<div style="font-family: Verdana; color: green;"><strong>TIP:</strong> Put your mouse pointer over a Forums topic to generate a brief preview of its content.</div>

Again, one line like this per line of the file.  You get the idea.  So now, wherever you place your custom widget, every time that page is loaded you should see a random selection from your file.

I'm not a professional developer at all, but the only possible drawback this might pose is that every time this widget is invoked it calls a PHP function which reads and references a file, which in theory may slow things down a bit if you have a ton of users.  But this might be negligible.  As to whether or not this would work better than creating an Invision database of quotes/tips and calling that, I have no idea.

At any rate, I hope this helps someone out there who, like myself, was looking for an easy, straightforward solution to generating random quotes/tips.

Link to comment
Share on other sites

56 minutes ago, kmk said:

Which are??

Well, I searched for this code some time ago by Googling something like "PHP random quote."  There were several different pages in the results, and I remember choosing what seemed to be the most straightforward and least time-consuming option.  Because it's really such a simple function I surmise there are several different ways to accomplish it, perhaps using Javascript or something else.  Google search is outstanding, so enter whatever search terms you want for however you want to solve the problem and give it a shot. :)

 

Link to comment
Share on other sites

  • 1 year later...

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...