Help - Search - Members - Calendar
Full Version: random archive using PHP
Movable Type Community Forum > Additional Resources > Tips and Tricks
Buckuxc
hey all,

   Ok, there is a whole lot to learn for one to get caught up to speed when it comes to code and stuff like that. I am no where near even beginning. I found Essential Blogging in my school library and the Advanced Movable Type section had a PHP script to run a feature that provided someone with a random archived post of mine. I followed the instructions as best I could. I created a new index template, named it Random Entry Pool and set the output file to entry-pool.dump.
In the template body I put the following

CODE
<MTEntries lastn-"100">
<$MTEntryTitle$>
<$MTEntryLink$>
<$MTEntryExcerpt$>

------------

</MTEntries>


I saved that. Then the book said to write a script that would grab the random entry. It provided PHP script to do this and said to put it directly into your template. I assumed this meant my Main Index template since this is where I want the link to be displayed. I wanted it under the Archive sidetitle after the months. So that's where I typed the script.
CODE
<div class="sidetitle">
 Archives
 </div>
<div class="side">
 <MTArchiveList archive_type="Monthly">
 <a href="<$MTArchiveLink$>"><$MTArchiveTitle$></a><br
 />
 </MTArchiveList>
<?php
$filename = "entry-pool.dump";
$posts = explode('--------', implode('', file($filename)));
srand((double) microtime() * 1000000);
list($num, $it) = each($posts);
$it = trim($it);
while (list($num, $line) = each($posts)) {
   $line = trim($line);
   if ($line != '')
       if (rand(0, $num+1) < 1)
           $it = $line;
}
list($title, $url, $excerpt) = explode("\n", $it, 3);
echo "<a href=\"$url\">$title</a> - $excerpt<br />";
?>
 </div>

<div id="llinks">
 <div class="sidetitle">other blogs</div>
 <div class="side">
 <a href="http://furst.bigwhoop.org" target=_blank>the Words of E1st</a><br />
 <a href="http://bigwhoop.org" target=_blank>the entire Bigwhoop organization</a><br />
 <a href="http://www.me3dia.com/cinnamon" target=_blank>Did You Know...?</a><br />
 </div>


What I got on my webpage was an unlinked bit of code text: $title - $excerpt
"; ?>

Obviously I'm a extreme novice, but can someone tell me where I went wrong? If I was supposed to put the PHP script in the new index template I created, then what do I use to display it on the Main Index? Thanks for the help.

sps

(parenthetical digressions)
maddy
I don't know enough about PHP to help out, but just wanted to point out that  
CODE
<MTEntries lastn-"100">
should be
CODE
<MTEntries lastn="100">

Was that just a typo when you posted your code here? smile.gif

And, does your main index template have a .php extension, or is your server otherwise configured to parse html pages for php scripts? smile.gif
Buckuxc
Yeah, that - instead of = was a typo. Either way, it doesn't work   sad.gif

My webhosting company runs PHP 4.12, so I assume what you asked does work. I'm getting up to speed on my terminology and coding ability. But the only way to do it is trial and error.

Hopefully someone knows what I did wrong.

sps(parenthetical digressions)
faf
It works for me.

I just tested you code here.

The problem with your code was the seperator, i think.

In the dump file, - appears 12 times.
but in the php code, it's only 8. I dont know why that would cause an error, but i set both to 10 on my test page and it worked a treat.
faf
ok i rushed to a conclusion... even though that code you posed would have reulted in errors, it would still have displayed some data.

The problem is with what maddy mentioned earlier. Your index pages are not php file is not a php file and as such the code isnt being parsed.

View the source of ur page and u'll see the code exactly as you inserted.

If you have php enabled on your site, it's most likely your file extensions have to be .php.

I read a bit of your site and it seems you know little about it, the simplest way to put it is this:

In your MT config, change the extensions of files generated to .php, go into your archives and remove any files MT generates automatically. this excludes popup image files To be on the safer site, download the index and archive files.

Rebuild your pages. All your pages.

Load your site again, that code should now work, and remember when linking to pages on your sites now to check whether the page is a .php file.

You may need to fix any links in your templates that may be proken because of the change in extensions.
jayant
There is a RandomnEnteries Plugin by David Rayners, on rayners.org, check it out
Buckuxc
Well thanks again everyone. I ended up using David Raynes RandomEntries plugin. I have a question about it now though. When I refresh my site, I get the same random entry every single time. The only time it changes is when I rebuild my pages and go back to View Site. Here's the snippet I have

CODE
<div class="sidetitle">
 Archives
 </div>
<div class="side">
 <MTArchiveList archive_type="Monthly">
 <a href="<$MTArchiveLink$>"><$MTArchiveTitle$></a><br
 />
 </MTArchiveList>
<MTRandomEntries lastn="1">
<a href="<$MTEntryLink$>">View a Random Archived Post</a><br />
</MTRandomEntries>
 </div>


What exactly am I doing wrong? I want it to be a completely different random entry every time the page is refreshed.

Also, I tried using the Word Count plugin, but I'm not exactly sure how to add it to my templates. I want it to be applied to my entries both on the main page and in my archives. I'd like it to be listed right after the time in the by line, but not linked to anything. Any suggestions? Thanks in advance.

sps
maddy
The RandomEntries Plugin only changes when you rebuild, that's how it is supposed to behave. If you want to have a more dynamic random entry, there are a few threads on the forum here using javascript and php. If you do a search for random AND entries, you'll be able to check 'em out. smile.gif

Sorry, can't help with WordCount!
faf
your previos script works fine if you want it to change on refresh. take a look at the test page link i  gave above.
kadyellebee
I believe that the info for your original idea came from this thread, so maybe you'll find something interesting by reading the comments there.  And then this thread has a couple solutions that I've found work really well. smile.gif

Kristine
girlie
Okay - why won't that URL convert to an actual link??
maddy
It's the spaces in there, need to add in some %20's in the randomly generated bit, ie. http://www.movabletype.org/cgi-bin....entries. I've noticed that with a lot of the links to the manual, and the anchor bit of the link.  smile.gif

Not that you need anything past the 4684, the rest just highlights the search query. smile.gif
girlie
Teehee! Thanks. I started to delete that part, but...  :)
kadyellebee
Thanks for fixing that girls!  I should probably check my lovelinks thread where I copied it from :giggle:

Kristine
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.