scriptygoddess

27 Jul, 2002

Master Author Archive, MySQL version

Posted by: kristine In: MT hacks

A few months back, I posted my techniques for creating Author Archives. Now with the advent of MT2.2 and the MySQL backend, this can be automatic instead of having to be edited when an author is added.

Part 1: Author List
Part 2: Author Archives

The files described below don't need to be created inside of MT unless you are using MT tags for formatting outside of the data that you are trying to capture.

(special thanks goes to Brenna for much of the Author List code, as well as help on the Archives. The connect.php file was her idea as well. Lynda came up with the PHP to make authors show/hide on the Archives page. And PHP.net was invaluable in this!)

Setup for MySQL connection file
When working with MySQL databases, you need to tell your script to connect to the database first. And so you don't need to have this information on every MySQL page you work with, you can create a connection file that stores this and call on it every page you work with.

Each persons configuration of their server might be a bit different but here's what mine looks like:

<?
// name of your database
$database = "database_name";

// connect to database
$db=mysql_connect ("localhost", "user_name", "password") or die ('I cannot connect to the database.');
mysql_select_db ("database_name");
?>

When you set up your MySQL database, you have a database (mine's love_mt), username (love_kristine), and password, and those are the things you'll fill in above. If your server is setup differently, you might be able to check the database section of your cpanel/control panel and see if it has a connection string in the section where you set up your database.

Part 1: Author List
Now that you have a connection file, you can use it at the top of your file to create an author list. I have 4 pieces of info that I want to display – Author name, Author email, Author URL, and then the Author name is used to point to the "about" page for each author.

<?php
//connection info
include ("/home/USERNAME/public_html/PATHTO/connect.php");

//We want to make a list of all of the authors who have permission to post in one blog – for me, this is blog_id=3, theredkitchen. So we join the tables for mt_author and mt_permission to get this information. Each of the pieces below are table names and row names.
$listauth = "SELECT a.author_id, a.author_name, a.author_email, a.author_url FROM mt_author a, mt_permission p WHERE (p.permission_author_id = a.author_id) and (p.permission_blog_id = 3)";

$result = mysql_query($listauth);

while ($row = mysql_fetch_array($result)) {

//Here's where I tell it what I want to show. You can see how each row name is an item that was SELECTed above.
echo "<b>", ($row['author_name']), ": </b>", "<a href=\"", ($row['author_name']), ".html\">about</a> <a href=\"mailto:", ($row['author_email']), "\">email</a> <a href=\"", ($row['author_url']), "\">homepage</a><br />\n";
}
mysql_free_result($result);
?>

The results of that query are shown on my About the Cooks page on TRK.

Part 2. Author Archives
This was a bit tricker, and I spent a whole afternoon trying to figure out how to put a query inside of a query. I want to display a list of every entry that an author has made, with a link to the individual entry for that post. I also want to filter it out so I can use a URL like http://theredkitchen.net/authors.php?author=AUTHORNAME to only show the posts by that Author.

<?php
//connection info
include ("/home/love/public_html/mt/templates/connect.php");

//first, create a list of authors to be clickable to show one author at a time on the page.
echo "<b>Quick links – click to show recipes</b>:<br>";

//this is the same query as the Author List, but without selecting as many terms out of the database.
$authorarray = "SELECT a.author_name FROM mt_author a, mt_permission p WHERE (p.permission_author_id = a.author_id) and (p.permission_blog_id = 3)";
$resulta = mysql_query($authorarray) or die (mysql_error());
while ($rowa = mysql_fetch_array($resulta)) {
echo "<a href=\"?author=", ($rowa['author_name']), "\">", ($rowa['author_name']), "</a> | \n";
}
echo "<br>";

//Now get more author information to put on the header of each query. This works as a list of authors so that the query below it knows how many authors to get the entry information from, and what their names are.
$authorlist = "SELECT a.author_id, a.author_name, a.author_url, a.author_email FROM mt_author a, mt_permission p WHERE (p.permission_author_id = a.author_id) and (p.permission_blog_id = 3)";

$result = mysql_query($authorlist) or die (mysql_error());

while ($row = mysql_fetch_array($result)) {

//This works along with the show one author piece above.
if ($_GET['author'] == $row['author_name']) {

//This is the header for each section of the authors.
echo "<div class=\"blog\"><a name=\"", ($row['author_name']), "\"></a><div id=\"banner\">", ($row['author_name']), "</div><div align=\"right\"><a href=\"", ($row['author_name']), ".html\">about</a> <a href=\"", ($row['author_url']), "\">home</a> <a href=\"mailto:", ($row['author_email']), "\">email</a></div>\n";

//Now, for each author, select the entries that they have published. We are pulling out the Title, ID, Authorname, Date, and Blog ID for blog_id=3 and the author from the above query.
$authorentry = "SELECT entry_title, entry_id, entry_author_id, entry_created_on, entry_blog_id FROM mt_entry WHERE (entry_blog_id = 3) and (entry_author_id = (".$row['author_id']."))";

$results = mysql_query($authorentry) or die (mysql_error());

while ($rows = mysql_fetch_array($results)) {

//Because my entries are saved in an archive filename like this – <$MTArchiveDate format="%Y/%m"$>/<$MTEntryID pad="1"$>.php – I had to pull the date out of the entry_created_on to point to the proper directory.
$year = substr($rows['entry_created_on'], 0, 4);
$month = substr($rows['entry_created_on'], 5, 2);

//and here is where I put my link together for it to display a link to each recipe by the author.
echo "<a href=\"", "http://theredkitchen.net/oldrecipes/", $year, "/", $month, "/00", ($rows['entry_id']), ".php\">", ($rows['entry_title']), "</a><br>\n";

}
echo "</div>";
}
}
?>

The results of this query are displayed at the Author Archive on theredkitchen.

If you need help with this, please feel free to post back. I'll try my best to help you out, even though this is quite new to me!

24 Responses to "Master Author Archive, MySQL version"

1 | Richard

July 27th, 2002 at 10:51 pm

Avatar

I anticipate a problem with this, at least for me. I've deleted, and then re-added authors (some of them multiple times!). How would that affect me, considering that their author ID's would likely have changed?

2 | kristine

July 28th, 2002 at 12:15 am

Avatar

Hmmm… I'd say that the easiest solution would be to get into the Power Edit mode and change the author name on the older posts to match the current author ids. But I suppose it depends on how many posts you have that fit this situation!
You might (I'm not sure) also need to make sure that the old author ids don't have permission to the blog in question – Since I set it up to take all authors with permission to post to a certain blog, it probably would pick up the old author ids even without them having any posts.

3 | Sarah

July 30th, 2002 at 4:42 am

Avatar

This is amazing!! just what I have wanted! You just rock Kristine!

4 | Sarah

July 30th, 2002 at 8:01 pm

Avatar

I am having a problem, in the code where it says

//Because my entries are saved in an archive filename like this – <$MTArchiveDate format="%Y/%m"$>/<$MTEntryID pad="1"$>.php – I had to pull the date out of the entry_created_on to point to the proper directory.
$year = substr($rows['entry_created_on'], 0, 4);
$month = substr($rows['entry_created_on'], 5, 2);

It makes it so the entry links are 404. I understand it is because I do not have the same archiving but how do I have it work with just the defauly MT individual entry archives?

5 | kristine

July 30th, 2002 at 9:00 pm

Avatar

So when you look at your archives, they go to archives/entryID.php?

You can take out the quoted code with the $year and $month. And use this instead of the section below that:

//and here is where I put my link together for it to display a link to each recipe by the author.
echo "<a href=\"", "http://yourURL.com/archives/00", ($rows['entry_id']), ".php\">", ($rows['entry_title']), "</a><br>\n";

I think that should work for you, just change the yourURL.com to whichever blog you are working with – I wasn't sure which one you were trying to configure this for so I could customize it for you!!!!

6 | Sarah

July 31st, 2002 at 2:36 am

Avatar

It works perfectly! Oh how I bow down to you!

7 | jazy

January 12th, 2003 at 4:46 pm

Avatar

I've tried this and it gives me an error that each of the MySQL queries are null. I modified the PHP to just spit out the raw MySQL dump, and it dumps nothing. And this was using both the Root user and a special one I created. However if I run the MySQL query in PHPMyAdmin, it returns the author list. Would this mean PHP is misconfigured?

8 | jazy

January 12th, 2003 at 5:50 pm

Avatar

nevermind…I upgraded to the latest version of PHP and it seems to work now ::knock wood::

9 | Phillip Winn

May 1st, 2003 at 11:43 am

Avatar

This works quite beautifully, but I'm struggling with the fact that MySQL leaves me without any MT tags or plugins on which I can rely. So when I present the list of articles, I can't just print an excerpt with HTML stripped out. I'm apparently going to have to write some PHP code to strip the HTML out manually, and also grab the first X words of the post if there is no excerpt on file. Has anybody done this already?

10 | Phillip Winn

May 2nd, 2003 at 10:36 pm

Avatar

I figured out how to do this, by the way. Posted my findings over on movabletype.org. Thanks for the tutorial!

11 | Sean

July 15th, 2003 at 6:19 pm

Avatar

I'm currently setting up a blog and my archives display in this format:

http://url/archives/000001.html

…is there any way to make this script pad the zeroes properly? I've been trying to puzzle this out to no avail.

12 | Kevin

August 7th, 2003 at 10:52 pm

Avatar

Kristine – one quickie if you're still getting these emails.

I'm trying to pull archives from multiple blogs off the same sql database. All of my archive paths look like this:

http://www.blog.com/blog_name/archives/entry_id

I can pull the entries from the db and create a list no problem. How do I format the link? It needs to read like this (spoken version):

www(dot)mydomain(dot)com(slash)blogname(slash)archives(slash)the_entry_id

:)

13 | Maria

August 24th, 2003 at 10:11 pm

Avatar

If anyone can help me with this it would be much appriciated. I'm trying to use Movable Type as a tool to make a fanfiction archive site. This addition would be great as I have about 30 writers… This is very much in the state of trying something new to hopefully implement on the real site later on.

I have never used PHP or any of the other languages other than HTML.. but I'm trying to learn.

Questions:

1) I can't connect to a database of any kind. Not as you describe it…

My mt.cfg file states this:

DataSource /home/USERNAME/public_html/db/
ObjectDriver::postgres
Database XXX
DBUser XXX

But I can't find a connect.php file in the db folder.. or any folder for that matter – did a search on my FTP program. (FlashFXP)

My host has no useraccess to the databases, PHP or otherwise.. not as you describe it anyway. They're really tight on security, if that could be a reason…

The question being – is this even possible for me to use?

2) Can I use this by posting it into a MT template or something of the likes? (Since I don't have the connect.php I don't see how.. but best to ask)

3) And if I can use this (by copying and pasting the code) through the MT editor/manager – how? Remember I'm completely dumb (- as in has no knowledge of this yet really, trying hard though) when it comes to this…

Anyone who wants to help out a newbie at this is very much appriciated.. but I do guess you get lots and lots of these questions so..

Thanks for a great site.. even though I still don't understand most of it 😉

14 | Scott Manning

August 30th, 2003 at 8:16 am

Avatar

This code works great. The only problem I've found is that it lists all entries even if they are in "Draft" mode. Any idea how to get around that?

Thanks in advance.

Scott

15 | Kevin

October 1st, 2003 at 12:44 pm

Avatar

Is there a function to pad with 0's the Entry ID? I notice you are hard coding "00", which only works for posts 1000 and higher. Post 999 generates an invalid URL (ie. 00999.php when the post is actually 000999.php)

16 | Kevin

October 6th, 2003 at 4:57 pm

Avatar

I found the answer to my question… To pad the proper number of 0's, replace this:

"/00", ($rows['entry_id']),

With this:

"/", printf('%06d', ($rows['entry_id'])),

17 | Rob

April 4th, 2004 at 1:22 pm

Avatar

Eep, I seem to be kind of late on this one. I noticed on the redkitchen site, the recent comments and all that dynamic goodness is still there. Is this all done in php too? That would seem extremely painful

18 | kadyellebee

July 27th, 2002 at 7:58 pm

Avatar

Author Archives with mysql
I finally finished writing up my new tutorial for Scripty Goddess: MT hacks: Master Author Archive, MySQL version. This is

19 | MT Plugin Directory

December 7th, 2002 at 6:09 pm

Avatar

Authors
Not Yet Implemented. Some of the data from this could be gotten from by using the Master Author Archive, MySQL

20 | Primo Bloggo

August 10th, 2003 at 8:25 pm

Avatar

Author lists: More
This makes much more sense, pero igual es un poco sucio. Hm. No sé. Fijesé: scriptygoddess…

21 | Bdoggers

April 17th, 2004 at 2:58 am

Avatar

Author Archives
投稿者別一覧について、これまでに紹介された方法や、私のやり方について述べます。MTでは、投稿者別一覧を自動的に出すことはできませんので、MTを改造するか、自動的にというのをぅ

22 | Bdoggers

April 17th, 2004 at 3:24 am

Avatar

Author Archives
投稿者別一覧について、これまでに紹介された方法や、私のやり方について述べます。MTでは、投稿者別一覧を自動的に出すことはできませんので、MTを改造するか、自動的にというのをぅ

23 | bill

December 16th, 2004 at 4:17 pm

Avatar

Is this still the best (& current) solution to implement author archives?

24 | cogdogblog

December 21st, 2004 at 6:19 pm

Avatar

MovableType Multiple Author Blogs / Contribution Lists
As reported a few times before, I have been prying time here and there to work on a prototype web site, that replaces a current, hand edited Low Threshold Applications web site with a dynamic one authored in MovableType. I…

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements