HTML Logo by World Wide Web Consortium (www.w3.org). Click to learn more about our commitment to accessibility and standards.

Moving forward with Composr

ocPortal has been relaunched as Composr CMS, which is now in beta. ocPortal 9 will be superseded by Composr 10.

Head over to compo.sr for our new site, and to our migration roadmap. Existing ocPortal member accounts have been mirrored.


Random Quote Modification

Login / Search

 [ Join | More ]
 Add topic 
Posted
Rating:
#14146 (In Topic #3575)

Fan in action

Grabbing a quote randomly over multiple text files

ocPortal 2.5 Final is required for using this modification, I have not tested it on older versions.

This modification will randomly pick a quote from a multiple number of (user-created) files, which is useful for websites that have multiple topics and webmasters have a large number of quotes for each of those topics. All this is done using a single comcode block line (i.e <block>someblock</block>).

You can also override the script's filename choice by providing the filename in the block's parameter (<block="somefile">someblock</block>) which would be useful if you also want to use only X quotes in a section specifically related to X topic. (like using a Star Wars quote file so that all the quotes that are displayed in the Star Wars section are related to the star wars universe)

Code (php)

function run($map)
   {
      assert('check_parameters(debug_backtrace())');

      $quote_rand = mt_rand(1,3);
       if ($quote_rand == 1) { $file=array_key_exists('param',$map)?$map['param']:'quotes'; }
       if ($quote_rand == 2) { $file=array_key_exists('param',$map)?$map['param']:'filename1'; }
       if ($quote_rand == 3) { $file=array_key_exists('param',$map)?$map['param']:'filename2'; }
   
      $a='text/'.filter_naughty(user_lang().'/'.$file).'.txt';
      $b='text/'.filter_naughty(get_lang().'/'.$file).'.txt';
      $c='text/'.filter_naughty(fallback_lang().'/'.$file).'.txt';
      if (file_exists(get_file_base().'/'.$a)) $place=$a; elseif (file_exists(get_file_base().'/'.$b)) $place=$b; else $place=$c;
   
      return do_template('BLOCK_MAIN_QUOTES',array('CONTENT'=>smile(get_random_line($place))));
   }
 

As shown on this example/source, the script will choose 1 of 3 files at random and will then display a quote from that file. If the random number is 2, for example, then the script will grab a quote (line) from 'filename1.txt' (specifically => [website]/text/en/filename1.txt) You can increase the number of files by changing the '3' in mt_rand to a higher number, and adding a "if equal" line for the new files, but make sure that each new line uses its own number.

You can also specify a parameter which overrides the randomly selected file. Examples:

This will make the script choose a file at random:

Code

[block]main_quotes[/block]

And this will make the script use a specific text file:

Code

[block="filename1"]main_quotes[/block]

Attachment
Download here
» Download: (DefMod) RandomQuoteMultipleFiles.zip (1.19 Kb, 14 downloads so far)

ocProducts :wub: :wub:

Click here to drop by my website!
Back to the top
 
1 guests and 0 members have just viewed this: None
Control functions:
 Add topic 

Quick reply   Contract

Your name:
Your message: