Archive for the ‘Tutorials’ Category

Group Your Results with PHP

Tuesday, June 17th, 2008

Sort by first letter

While working on a personal project this evening, I wrote a small function to group a list of results by the first letter of each record.

As simple as it may be, I think it’s very effective in breaking up a long list of results and it’s very easy to implement.

First up I defined a list of names in an array, and then sorted them alphabetically.

  1. $contacts = array(‘John’, ‘Peter’, ‘Adam’, ‘Sam’, ‘Dick’, ‘Nicholas’, ‘Philip’, ‘Andrew’, ‘Thomas’, ‘Bruce’, ‘Steven’, ‘Brian’, ‘Terrence’);
  2. sort($contacts);

Next I created a function called ‘group_by_first’. Here’s my code, I’ll explain what’s going on underneath.

  1. function group_by_first($contact_name) {
  2.   global $cur_first;
  3.   $get_first = substr($contact_name, 0, 1);
  4.   if ($cur_first != $get_first) {
  5.     echo ‘<div style="width: 120px; height: 30px; background: #ccc;">’. $get_first .‘</div>’;
  6.     $cur_first = $get_first;
  7.     }
  8.   echo $contact_name . ‘<br />’;
  9.   }

Line 1: Here we name the function and add a parameter that stores the current record

Line 2: Creating a global variable named $cur_first (current first letter), which will be empty to start with

Line 3: Here I define a variable called $get_first, and retrieve the first character from the current record. For example, if we’re on ‘Bruce’ then $get_first will store ‘B’

Line 4: Comparing the current letter group we’re in with the first letter of the current record. If they don’t match, that means we’ve run out of records that begin with that letter and we’re ready to make a new group

Line 5: Echo out a div with some styling, and the first letter of the current record. The inline CSS is there purely for demonstration, please rememeber to define a class in your stylesheet!

Line 6: We’ve added a new group, so now we’ve got to tell $cur_first that we’re on the next letter in the list, which we do by assigning it the value of $get_first

Line 8: Finally, we echo out the current record

All we’ve got left to do now is to run each element of the array through our function!

  1. foreach ($contacts as $value) {
  2.   group_by_first($value);
  3.   }

And here’s a basic example of it in action.

With some better styling I think that this can be an elegant solution for breaking up your lists of records.

Style Your Wordpress Comments

Thursday, June 5th, 2008

The most important aspect of a blog is your readers. Without them, you’re only talking to yourself.

The main route of communication between you both is the comments under each post, so why not pretty them up a bit?

In this article I’ll show you the various CSS styles you need to change, and I’ll also explain how I added the custom background when I reply to comments on my own articles.

(more…)

Adding the Wordpress Sidebar to Every Page

Friday, February 22nd, 2008

When you first install Wordpress, the default Kubrick theme doesn’t display the sidebar when you view a single post. It’s a feature many people ask for and here’s how you do it.

In your Wordpress admin interface, navigate to Presentation > Theme Editor and then select Single Post from the right menu. The ’single.php’ file will appear in the editing window.

The first change is to replace the second line:

<div id="content" class="widecolumn">

with

<div id="content" class="narrowcolumn">

Once that’s done, scroll to the bottom of the page and find the line <?php get_footer(); ?> and replace it with:

  1. <?php
  2. get_sidebar();
  3. get_footer();
  4. ?>

And that’s it! Your sidebar will now appear on every page of your Wordpress blog.