Random Quotes from File—My First Plug-in Attempt
Posted: 23 August 2004 07:40 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  136
Joined  11-21-2002

Just to learn about plugins, I’m taking some PHP code that used on my pMachine-based site and turing it into an EE plugin. The code reads a file containing quotes—one per line—and picks one at random, displaying it.

I want to pass the local path to the quotes file as a parameter. The pass seems to work, I can print out the parameter and it is, in fact, the path to the file. But, I can’t seem to get that path assigned and the file opened.

Here is the complete code (without the php tags):

$plugin_info = array(
    
'pi_name' => 'Random Quote',
    
'pi_version' => '1.0',
    
'pi_author' => 'Rick Robinson',
    
'pi_author_url' => 'http://blue.homeunix.net',
    
'pi_description' => 'Returns a random quote from a text file.',
    
'pi_usage' => random_quote::usage()
    );

class
random_quote
{
    
var $return_data = "";
    
    function
random_quote()
    
{
    
        
global $TMPL;
        
$parameter = $TMPL->fetch_param('path');
        
        
$delim = "\n";
        
$quotefile = "/Library/WebServer/Documents/blog/quotes.txt";
        
//$quotefile = $parameter;
        
$fp = fopen($quotefile, "r");
        
$contents = fread($fp, filesize($quotefile));
        
$quote_arr = explode($delim,$contents);
        
fclose($fp);  
        
// generate random quote index
        
$quote_index = (mt_rand(1, sizeof($quote_arr)) - 1);
        
// get quote at $quote_index and return it
        
$thequote = $quote_arr[$quote_index];
        
$this->return_data = $thequote;
        
//$this->return_data= $parameter;
    
}
    
    
function usage()
    
{
    ob_start
();
    
?>
    The random_quote plugin returns a random quote pulled from a text file named quotes
.txt which is passed as the path parameter. The text file should have one quote per line.
    
    
{exp:random_quote path='<absolute path to quote file>'}
    <?php
    $buffer
= ob_get_contents();
    
ob_end_clean();
    return
$buffer;
    
}
}

You can see that I’ve got two lines that assign a value to $quotefile as well as two lines that provide the return value. I’ve done this to debug only. This particular version “hard codes” the path to the file, and it works fine. However, if I assign the path to $quotefile via the $parameter variable, it does not work…

I know this is more a php question than plugin question, but maybe someone has a quick answer?

Rick R

Profile
 
 
Posted: 23 August 2004 09:05 AM   [ Ignore ]   [ # 1 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6093
Joined  08-04-2002

Rick...

Is this going to be different then the one Rick wrote and is available in the PlugIns Area?
There is also this one that randomizes comments. Maybe there’s something in that code to help.

Profile
 
 
Posted: 23 August 2004 09:30 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  136
Joined  11-21-2002

Rick’s will provide higher performance because the quotes are embedded in the plugin, whereas mine must read from a file, then select the quote. So, his is superior from a performance perspective.

However, mine would allow the user to continuously add quotes by simply editing a text file, rather than add to php code.

I’m not sure I’ll ever “publish” mine, it is more a learning experience. And, it is the passing of the file name parameter that is hanging me up, so nothing in Rick’s helps…

Rick R

Profile
 
 
Posted: 23 August 2004 09:45 AM   [ Ignore ]   [ # 3 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6093
Joined  08-04-2002

Rick...

However, mine would allow the user to continuously add quotes by simply editing a text file, rather than add to php code.
Ahhh, that to me is a little handier.
Right now I’m entering them in a separate weblog and then using the “random” variable and limiting to one. One thing I don’t see anywhere is something pMachine had in pBlocks. In addition to “random”, you could have it just cycle through starting at the first ending at the last and starting over.

I’m not sure I’ll ever “publish” mine, it is more a learning experience.
Ahhh, always fun!

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 65102 Total Logged-in Users: 63
Total Topics: 82263 Total Anonymous Users: 30
Total Replies: 442113 Total Guests: 241
Total Posts: 524376    
Members ( View Memberlist )