After using David Raynes’ MTRandomEntries plugin for about a day, I decided I wanted a truly dynamic randomly-generated list of blog entries (especially now that I have more than 700 archives from which to draw). I also wanted to be able to include the list throughout my site, and not just in my blog, which meant putting it in a PHP file rather than creating a new index template within Movable Type.
The MTRandomEntries plugin updates the list only when you rebuild your templates. And I wanted the list of random entries to change on reload. So below is what I did, with thanks to empty pages for filling my need to dirify entry titles to create the URLs.
You’ll now see a revamped sidebar throughout the site that includes my random_entries.php template at the bottom (there’s also this stand-alone version, which is fun to play with…if you’re me, anyway, and like taking trips down memory lane).
Dynamic MT Random Entries PHP Code
<?php
// connect to your database
// dirify function to create links to archives
// that use archive file templates for naming
function dirify ($s) {
$s = strtolower($s);
$patterns = array(‘/<[\/\!]*?[^<>]*?>/s’, ‘/&[^;\s]+;/’,’/[^\w\s]/’, ‘/ /’);
$replace = array(”, ”, ”, ‘_’);
$s = preg_replace($patterns, $replace, $s);
return $s;
}
$sql = “SELECT DATE_FORMAT(entry_created_on,’%Y/%m/%d/’) AS date,
DATE_FORMAT(entry_created_on,’%m.%d.%Y’) AS displayDate,
entry_title
from mt_entry
WHERE entry_blog_id=’2′
order by RAND() LIMIT 5″;
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$url = $row[‘entry_title’];
$url = str_replace(” &”, “”, $url);
$url = dirify ($url);
printf(“<a href=\”/life/%s$url.php\”>%s</a>
(<a href=\”/life/%s\”>%s</a>)<br>”,
$row[“date”],$row[“entry_title”],
$row[“date”],$row[“displayDate”]);
}
?>
19 responses so far ↓
1 morpheous // Oct 7, 2003 at 1:42 pm
very nice!
I am wondering how do you limit listed entries to only one category ?
2 gabe // Oct 7, 2003 at 2:05 pm
Limiting to 1 category is pretty easy…just specify the category ID of the category or categories whose entries you want to randomly display. You’d have to look up category IDs in the mt_category table (or do a sub-query), but basically, it would look like this (see bold for addition):
$sql = “SELECT DATE_FORMAT(entry_created_on,’%Y/%m/%d/’) AS date,
DATE_FORMAT(entry_created_on,’%m.%d.%Y’) AS displayDate,
entry_title
from mt_entry
WHERE entry_blog_id=’2′
AND entry_category_id=’xx’
order by RAND() LIMIT 5″;
$result = mysql_query($sql);
…or for multiple categories:
AND entry_category_id in (‘xx1’, ‘xx2’, ‘xx3’)
…the above would be the easiest (assuming you know or can query your database for the category ID(s) you want), but if you want to select the category by name in the same query, do this:
$sql = “SELECT DATE_FORMAT(entry_created_on,’%Y/%m/%d/’) AS a.date,
DATE_FORMAT(entry_created_on,’%m.%d.%Y’) AS a.displayDate,
a.entry_title,
a.entry_category_id,
b.category_id,
b.category_label
from mt_entry a,
mt_category b
WHERE a.entry_category_id = b.category_id
AND a.entry_blog_id=’x’
AND b.category_label=’xx’
order by RAND() LIMIT 5”;
$result = mysql_query($sql);
Either of these methods should work. Good luck!
3 gabe // Oct 15, 2003 at 10:24 am
i realized this morning that if you have any entries in your blog that are in Draft that you don’t want to appear on your site (or in your random entries queue), then you need to account for that in this script. see bold below for additions:
$sql = “SELECT DATE_FORMAT(entry_created_on,’%Y/%m/%d/’) AS date,
DATE_FORMAT(entry_created_on,’%m.%d.%Y’) AS displayDate,
entry_title, entry_status
from mt_entry
WHERE entry_blog_id=’2′
AND entry_status=’2′
order by RAND() LIMIT 5″;
$result = mysql_query($sql);
an entry_status of 2 is Publish (1 is Draft).
4 Erik // Oct 18, 2003 at 8:09 am
Hm..I’m getting
mysql_fetch_array(): supplied argument is not a valid MySQL
?
Regards,
Erik
5 Erik // Oct 20, 2003 at 8:18 am
Hmm.. My SQL isn’t good. How do I use this script with the default archiving setup of MT. Like http://coolios.flabber.nl/archives/2003_10.php#5019
6 Erik // Oct 20, 2003 at 8:36 am
BTW mysql_fetch_array() error is solved 🙂
7 Benny // Oct 23, 2003 at 9:19 am
I want to display the content (entrybody) beside entry title. How?
8 gabe // Oct 23, 2003 at 9:45 am
Adding entry_text is quite simple. See bold below for additions.
$sql = “SELECT DATE_FORMAT(entry_created_on,’%Y/%m/%d/’) AS date,
DATE_FORMAT(entry_created_on,’%m.%d.%Y’) AS displayDate,
entry_title, entry_text, entry_status
from mt_entry
WHERE entry_blog_id=’2′
AND entry_status=’2′
order by RAND() LIMIT 5″;
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$url = $row[‘entry_title’];
$url = str_replace(” &”, “”, $url);
$url = dirify ($url);
printf(“<a href=\”/life/%s$url.php\”>%s</a>
(<a href=\”/life/%s\”>%s</a>)<br>
%s<p>“,
$row[“date”],$row[“entry_title”],
$row[“date”],$row[“displayDate”],
$row[“entry_text”]);
That 5th %s above is the string for entry_text.
Also, a good reference for MT table structure is here:
http://www.bradchoate.com/past/mtsql.php
Use it as a guide for figuring out what you can add to your query.
9 Ben // Oct 24, 2003 at 3:32 am
Thanks.
But I get error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xxx/public_html/mt/plugins/random.php on line 24
when I try to call it from index page with .
What should I do?
10 Ben // Oct 26, 2003 at 8:52 am
The error fixed.
Currently, the problem is I don’t want to put the link and date. How to remove them from your script?
11 gabe // Oct 26, 2003 at 6:03 pm
just modify this part to display what you want…this will display only entry_title and entry_text:
printf(“%s<br>
%s<p>”,
$row[“entry_title”],
$row[“entry_text”]);
note that each %s corresponds (in order) to the rows you call.
you could also modify the query to call only the fields you need, but it doesn’t really matter.
12 Ben // Oct 26, 2003 at 9:40 pm
Thanks Gabe.
It’s work! 🙂
I still have a question.
In the result of entry_text, how to enable ‘automatic ‘ like in the normal entries list?
13 Eric Wallace // Oct 29, 2003 at 12:02 am
Any ideas as to why my mt-entry table has a NULL value for entry_category_id on every row? It looks like MT is not updating this column, but why? I have categories assigned to each entry.
14 Xu // Nov 22, 2003 at 4:58 am
I have the same problem.
If I add “AND entry_category_id” in the script I get blank result, because “entry_category_id” is NULL on every row…
Please could you help me??
15 john eklund // Dec 16, 2003 at 8:28 am
Two of you have mentioned you’ve been able to fix this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
But haven’t mentioned how you fixed it. Could you share how you fixed it? I’m still getting this message.
16 Eliah Holiday // Dec 26, 2003 at 4:26 pm
I’d love to use this but I have no idea where to put the php file. I just want to add a single random entry link to my blog navi that is updated on page reload.
Thanks for any insight.
17 Eddie // Jan 7, 2004 at 5:00 pm
I’m getting the following error:
Parse error: parse error in /usr/local/plesk/apache/vhosts/digitalleap.com/httpdocs/gallery/random.php on line 12
My line 12 is:
function dirify ($s) {
Thoughts on what I’ve done wrong?
Thanks!!
18 mojotexas // Jan 15, 2004 at 10:26 pm
Can you shed a little light on the database connection method. An example connection string would be useful. I know my database info from the mt.cfg :
ObjectDriver DBI::mysql
Database mojotex_database
DBUser mojotex_mojotex
but what code goes in there?
19 geoff pedder // Aug 26, 2004 at 9:33 am
this works mostly but the url’s to archives use a max 14 character title for the url, the stuff in the database outputs the full title. How do I get around this?