Articles

Optimize Your MySQL Database Tables

You can optimize MySQL tables using the OPTIMIZE command. This can be used if you have a table with any variable length rows and you make lots of changes to the data in that table. Variable length columns are VARCHAR, VARBINARY, BLOB, or TEXT. The OPTIMIZE TABLE will defragment the data and reclaim any unused space. This also has the effect of resetting any auto incrementing numbers to the highest value in the table.

OPTIMIZE TABLE tbl_name;

You can do this for every table in the database, but to save time MySQL comes with a command line script that will optimize all tables in a database. This program is called mysqlcheck and can be run like this.

mysqlcheck -o database -u user -ppassword

Note that when writing this command you should not put a space in between the -p and your password.

Sequentially Rename All Image Files In A Directory With PHP

The following function will rename all of the image files in a directory to be sequential. The parameters are the path of the directory that the files are in and the name of a function that will be used to sort the array of files through the PHP usort() function.

function sequentialImages($path, $sort=false) {
 $i = 1;
 $files = glob($path."/{*.gif,*.jpg,*.jpeg,*.png}",GLOB_BRACE|GLOB_NOSORT);
 
 if ( $sort !== false ) {
  usort($files, $sort);
 }
 
 $count = count($files);
 foreach ( $files as $file ) {
  $newname = str_pad($i, strlen($count)+1, '0', STR_PAD_LEFT);
  $ext = substr(strrchr($file, '.'), 1);
  $newname = $path.'/'.$newname.'.'.$ext;
  if ( $file != $newname ) {
   rename($file, $newname);  
  }
  $i++;
 }
}

The following function can be used in the second parameter to sort the files by their last modified time.

Convert A Date Into Timestamp In JavaScript

I have previously talked about using PHP to convert from a date to a time, but what about doing the same in JavaScript?

To get the unix timestamp using JavaScript you need to use the getTime() function of the build in Date object. As this returns the number of milliseconds then we must divide the number by 1000 and round it in order to get the timestamp in seconds.

Math.round(new Date().getTime()/1000);

To convert a date into a timestamp we need to use the UTC() function of the Date object. This function takes 3 required parameters and 4 optional parameters. The 3 required parameters are the year, month and day, in that order. The optional 4 parameters are the hours, minutes, seconds and milliseconds of the time, in that order.

Displaying Wordpress Authors

Wordpress has a couple of rarely used functions that allow author information to be displayed for the current post and a list of all of the authors on the blog.

Adding a written by message to your posts is not difficult at all. Just use the the_author_posts_link() function inside the post loop.

<?php the_author_posts_link(); ?>

This function shouldn't be confused with the the_author_link() function that prints out the link in the author's profile.

The second way of printing out author information is by using the wp_list_authors() function. This can take a number of arguments, but the simplest use of it is as follows:

Find Appropriate Colour In PHP

Use the following function if you want to print the text in a particular colour, depending on what the background colour is.

function opposite($color) {
  $white = 0;
  for ($i = 0; $i &lt; 6; $i += 2) {
    $white += base_convert(substr($color,$i,2),16,10) < 127 ? 1 : 0;
  }
  return $white <= 2 ? "000000" : "ffffff";
}

It works by looking at the colour and seeing how much of it is white. If greater than two parts are significantly white then the color returned is black, otherwise white is returned.

Here are some examples to test the function out.

Turn Off Wordpress Revisions

Wordpress has a nice little revisions feature that will allow you to revert to a previous version of a post if you don't like the current edit. However, the drawback of this feature is that it is not always needed and it fills the post table full of stuff you will never need. Fortunately, turning this feature off isn't too much of a pain. All you need to do is add the following line of code to your wp-config file, just below the DB_COLLATE line.

define('WP_POST_REVISIONS', 0);

You can also set the autosave interval here to something greater than the default of 60 seconds. It is possible to do this in the wp-config file since version 2.5.0.

define('AUTOSAVE_INTERVAL', 123);

If you want to get rid off all of the post revisions from your post table then you can use the following SQL query.

Find File Extension In PHP

This simple code example uses a combination of strrchr to find the last occurrence of a string and substr to return part of the string in order to find the file extension for a given filename. This is ideal if you want to quickly find a file extension.

$ext = substr(strrchr($fileName, '.'), 1);

This code can be used in the following way.

$fileName = '\path\to\file\afile.wibble';
$ext = substr(strrchr($fileName, '.'), 1);
echo $ext;

The output here is 'wibble';

Getting Started With Zend_Lucene

Zend_Lucene is an implementation of the Lucene search engine in PHP5 and is included as part of the Zend Framework from version 1.6. Lucene implements all of the standard search engine query syntaxes (eg. boolean and wildcard searches) and stores its index as files so it doesn't need a database server to run. Lucene can be used if you want to add search functionality to a site but don't want to go down the route of building a querying syntax from scratch.

To get started with Lucene you need to create an index. The following code has the effect of creating a directory on your server that Lucene will use to store and retrieve documents.

$index = Zend_Search_Lucene::create('/data/my-index');

To open the index use the following code.

Adding A Sortable List To Wordpress

A sortable list is simply a list of items that can be dragged and dropped to alter the order of those elements.

There are two sortable lists available in Wordpress, one from the JQuery framework and one from the Scriptaculous framework. For a sortable list you will need a list, so here is a simple one.

<ul id="sortcontainer">
	<li class="sortable">Item 1</li>
	<li class="sortable">Item 2</li>
	<li class="sortable">Item 3</li>
	<li class="sortable">Item 4</li>
</ul>

JQuery Sortable

7 Tips On Writing Your CV

Working for a number of years in the web industry means that I get to see quite a few CVs, and after a while you get a feel for what makes a good one. Your CV (or resumé) is quite often the very first thing anyone ever hears about you, so it is important to get it right. To that end I thought I would impart some of the things I have learned after years of writing my own and reading other people's CVs.