The following warnings occurred: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Warning [2] Undefined array key "allowautourl" - Line: 584 - File: inc/class_parser.php PHP 8.1.31 (Linux)
|
Random Quotes - Printable Version +- GetSimple Support Forum (http://get-simple.info/forums) +-- Forum: GetSimple (http://get-simple.info/forums/forumdisplay.php?fid=3) +--- Forum: Scripts & Components (http://get-simple.info/forums/forumdisplay.php?fid=11) +--- Thread: Random Quotes (/showthread.php?tid=653) |
Random Quotes - NickC - 2010-04-19 Many people like to have some sort of general interest stuff in the sidebar such a thumbnail photo or a random quote. This will show how simple it is to generate a random quote in the sidebar. It is suitable for people with limited or no programming experience. PHP has a handy function to return a random element from an array, so we will use that. We will create a component of quotes then put the component in the sidebar. I like to keep things neat for editing, so you might like to notice that I use line breaks to suit myself and make it easy to edit, rather than in their perhaps 'natural' place. First create the component; I call it "Random Quotes": Code: <small><u>Random Quotes</u></small><br /> You should look out and check that the quote doesn't contain a ' character within it. Otherwise when PHP sees the ' character it thinks it has reached the end of the quote and will then likely get confused with the rest of the quote and probably just give up. If the quote does contain a ' character, either change it to a " character if you can, else edit the text to remove it (e.g. change "haven't" to "have not". You can't escape it with a backslash or two because the parser in the editor removes the backslashes. [EDIT removed reference to "put two \ characters in front of the ' character. The \\ is a special indication to PHP to ignore the following character (the ' ) and to keep going." The html parser will remove a backslash each time you use it. ] The PHP function array_rand() returns an random index number into the array. We echo the element which has that index number. Save and we are done with the component. You may notice how easy it is to add new quotes as we are using line breaks in the editor to suit ourselves. Just cut and paste, enclose within ' and a comma if needed. By the way, the "author" class is defined in my custom theme's css file and simply makes the text small and coloured. It looks like this: Code: .author { Now to include the random-quote component in the sidebar: Code: <p><?php get_component('random-quotes'); ?></p> Save and we are done. You can see the results here: http://www.nickcoleman.org Random Quotes - homershines - 2010-04-21 I think the final comma is missing at the end of the Terry Pratchett quote in the component. Thanks. homershines Random Quotes - NickC - 2010-04-22 I was wondering if I needed to be a bit more detailed so here is some more info for those new to PHP. [EDIT] I moved some text and incorporated it into the first post. It no longer appears here. There are plenty of sources of quotes on the web, you could also try searching for "fortune unix". Fortune is a famous quote program written for Unix machines and there are many publicly available lists of quotes for it. Personally I like to make up my own list as I go, adding memorable ones when I come across them. Random Quotes - homershines - 2010-04-23 Thanks for the correction. Do you think that adding the last bit of info into the first post would be helpful? I know for me, step by step instructions really help when I know little about the behind the scenes process. Then after the fact, I can review the steps and learn more about their purpose. It is truly amazing the value of good instructions. I'm working on a group dynamics game that I am using as a tool for community organizers, and I can see just how everything can and does get lost in translation. Thanks. homershines Random Quotes - NickC - 2010-04-23 Yes, you are right, I'll do that. I often forget we can edit the original. |