We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Could randomizer reference an external file?

Development and Programming

textdriven's avatar
textdriven
107 posts
17 years ago
textdriven's avatar textdriven

It’s really annoying when you update to the latest build only to realise you overwrote all the quotes you put in pi.randomizer.php

       
Ingmar Greil's avatar
Ingmar Greil
29,243 posts
17 years ago
Ingmar Greil's avatar Ingmar Greil

Ah, the randomizer plugin… Try this in your template:

<?php srand((double)microtime()*1000000); 
$quote_txt = preg_split("/--NEXT--/", join('', file("path/to/quotes.txt"))); 
echo $quote_txt[rand(0, sizeof($quote_txt) -1)];  ?>

Now you only need a quotes.txt file where you separate your entries with <newline> –NEXT– <newline>. You can even use some XHTML markup in there. Makes sense? Working perfectly for me.

I suppose you could turn that into a plugin, but I never bothered.

       
textdriven's avatar
textdriven
107 posts
17 years ago
textdriven's avatar textdriven

Excellent. Thank you kindly.

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
17 years ago
Lisa Wess's avatar Lisa Wess

As a note, you could also set up all your random items in a weblog with each one as an entry, and use orderby=”random”. Essentially, though, plugins are an “all in one” kind of deal - you’d want something in the database to separate it out and store the quotes randomly, so a randomizer “module” - which is essentially what the weblog method is. =)

       
Ingmar Greil's avatar
Ingmar Greil
29,243 posts
17 years ago
Ingmar Greil's avatar Ingmar Greil

Yes, my method is decidely low tech. I forgot one thing, actually: I have my quote.txt file setup as a template, and saved as a file, so for once this going the other way: I am not editing en external file to update a template, I am editing a template to update my external file. (I actually have this working on config.php as well on one of my sites, although that took a symbolic link. It’s very cool to use EE to edit your .htaccess or config.php files.)

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.