Recently in PHP Category

List Random Authors

| 2 Comments

As communities grow, so does the need to showcase your authors. Having a hefty community of authors is a great thing (content producers FTW!) but everyone wants their fifteen minutes. This handy little tip can help make sure everyone gets their fair share of the limelight.

A couple of things to note first: Make sure that you’ve got author archives set up. You can hack this to work with author profiles, but that’s not the scope of this tutorial. Also, this tutorial assumes that your server is parsing whatever page this is on as PHP. As a rule of thumb, if your file extension is .html, this probably won’t work; if you’re file extension is .php, you should be fine.

<?php
    $displayed_authors = array(); // Will hold indexes from $authors for authors already displayed
    $show = 25; // How many authors should we show?

    <mt:Authors>
        $authors[<mt:AuthorId />] = '<li><a href="<mt:EntryLink archive_type="Author" />"><mt:EntryAuthorDisplayName encode_php='1' /></a></li>';
    </mt:Authors>

    for ($i=1; $i <= $show; $i++) {
        $rn = array_rand($authors); 
        // Loops until it finds an author not displayed
        while(in_array($rn, $displayed_authors)) {
            $rn = array_rand($authors);
        }
        array_push($displayed_authors, $rn);
        echo $authors[$rn];
    }
?>

First, we set up an array where we’ll store all the authors that have already been displayed. More on that in a minute. Then we define how many authors we want to show with this block.

Tag Cloud on PHP in Movable Type 4

| 6 Comments

Movable Type has become one of the first platforms in which tags have appeared . It happened in the third version. And in the fourth version it is possible to add tags not only to post, but also to uploaded files or created pages. But functionality of the tag MTTags is not thought over up to the end in the current version as well as in the previous version MT. It is necessary to make some efforts to have a normal Tag Cloud.

(This article is cross posted at MovableTweak.)

A client recently asked me if they could have an easy-to-manage banner ad setup, with the ability to add advertisements and specify links and alternate text, while at the same time being able to manage them easily. Sounds like a job for… Movable Type!

This tip can be used for any ads, or any images for that matter. There are four basic steps: upload the ad, create an entry for it, create the php file and include it in your template.

PHP-and-MySQL-book.gif

Some of the more interesting customizations you can do with your Movable Type weblog require using PHP scripts, either on their own or in conjunction with the MySQL database. Some scripts are easy enough to install and require very little configuration. Others, however, require that you know something about how the script works, or how your MT database is set-up, or both.

Larry Ullman's PHP and MySQL for Dynamic Web Sites is a well organized, fairly comprehensive, easy-to-follow introduction to PHP and MySQL. Ullman expects you to know basic HTML and have had some exposure to programming. I took a basic Pascal programming class 25 years ago, but that's about it and I still managed to learn a lot from this book. The book is filled with examples and clearly laid out code. I had never heard of PHP or MySQL before using Movable Type, so the basic introductions to both were very helpful. I highly recommend this book if you are wanting to do more customizations with your MT blog that require knowing more about PHP or MySQL.

Random Entries Using PHP

| 10 Comments

There are two methods to create a random entry, pulled from your weblog database (if you aren't using dynamic publishing). The easiest is David Raynes' MTRandomEntries plugin. However, MTRandomEntries generates a random entry only when you rebuild the page on which the MTRandomEntry code is located. If you want a random entry to be generated each time the page is refreshed in a browser, you can do that with a PHP script that pulls the data from your MySQL database. For this method to work your blog needs to be PHP enabled and you need to be using a MySQL database.

This tutorial will outline variations of a PHP script you can use to generate random entries, similar to what can be seen in my Quotes blog. In the Quotes blog if you refresh the page in your browser a new quote will be displayed as the main entry. This tutorial builds on the material already covered in the LMT tutorial, Querying a MySQL database with PHP - Random Quotes. Please make sure you are comfortable with that tutorial before proceeding with this one.

In the previous tutorial, we created a random quotes script, and in so doing, covered how to connect to a MySQL database, how to create a basic MySQL query statement, how to query the database and display the results, and how to use a PHP include to include the script on to a web page. There are several differences between the simple script to generate random quotes and a random entry script. You may want your random entry to have a permalink to its individual archive page. If the entry has an extended entry section, this needs to be indicated. You might also want your random entry to include author and date posted information. The random entry may need to be formatted using style tags that are consistent with how you style your entries.

If you are using a MySQL database and your blog is PHP enabled (see Converting to PHP), you can use PHP scripts to pull blog data from your database to add customizations to your blog. For example, say you would like to have on your sidebar a quote, randomly pulled from a selection of quotes, with a new quote displayed every time you refresh the page. You can accomplish this by creating a separate weblog for quotes, then using a simple PHP script to pull the data from your MySQL database, and using a PHP include to put the script results into your main weblog page.

PHP scripts are a level of difficulty up from Movable Type, and require careful study to understand. I recommend a book by Larry Ullman called PHP and MySQL for Dynamic Web Sites. Somehow, without reading this book, I managed to get a PHP script to work that I found somewhere on the MT forums. However, I had no idea how it worked. So after reading the book on a long airplane flight and comparing what I learned to the script I had, I created a new, simplified script to generate random quotations. There are several other, probably easier, methods to generate random quotations. The script detailed here is an example of how a PHP script that queries a MySQL database functions and should be regarded in that context.

Here is an example of the script in action. Refresh the browser to see a new quote.