WordPress Logo

Procedure For Changing The Directory Of A Wordpress Blog

1st February 2008 - 3 minutes read time

For many sites the blog is only part of the site, not the main reason for the site existing. In these cases the blog is kept in the directory /blog or similar. Occasionally (for what ever reason) it might be necessary to change the directory. To that end here is a small walk through of the steps you need to take in order to do this.

PHP Logo

Using PHP To Generate CSS

31st January 2008 - 3 minutes read time

Generating CSS with PHP has several benefits. For example, you can keep all of your colour declarations as PHP variables so if you need to change any colours it only takes a small edit and not a find/replace operation.

Getting PHP to generate CSS requires just two steps. The first thing to do is to open your CSS file and insert the following line at the top. This tells the browser that the file is CSS.

JavaScript Logo

JavaScript Scrolling Box Marquee Replacement

29th January 2008 - 14 minutes read time

Having a Marquee on a web page is a nightmare from an XHTML validation point of view, so here is a neat function that will produce the same effect as a vertical scrolling marquee, that passes XHTML validation, and built entirely from JavaScript and CSS.

Vertical Scrolling

Take the following HTML code. Don't worry about reading it. It is just 7 p tags (2 of which act as spacers) contained within a div tag.

MySQL Logo

Logging Onto A MySQL Database

28th January 2008 - 4 minutes read time

If you have command line access to your MySQL database server you will need to use certain parameters to log in. Most web hosts will not allow you to do this, so you might want to install MySQL into a local computer and give it a go.

To log into mysql you must run the program called mysql with certain parameters. Here is an example.

./mysql -u username

One thing you must realise is that all usernames are associated with a host so if the user you specified can't access the server from this host then you won't get far. To specify the host location enter the -h flag.

./mysql -h hostaddress -u username

If your user is able to access this server then you will be asked for a password. You can set the password in the string using the -p flag. If this doesn't work then leave out the space between the -p and the password.

Flashing JavaScript Text

27th January 2008 - 2 minutes read time

Here is a simple function that makes text in a tag fade into one colour slowly before quickly fading back into the original colour. If the background is the same colour as the text then the text will appear to fade in and out.

PHP Logo

PHP5 Error Reporting

26th January 2008 - 7 minutes read time

PHP has some very nice error reporting features, which can tell you many things about the code that you are trying to execute. This error reporting is always nice to have available when debugging code as it helps you solve many of the common mistakes that occur when creating dynamic web pages.

However, this error reporting is almost always turned off on production servers as it can reveal information about the server that you wouldn’t want everyone to see. For example, the errors can reveal information about server file structure, database fields in queries, database usernames, $_GET and $_POST commands and so on.

WordPress Logo

Enable Custom Field Search In Wordpress

25th January 2008 - 10 minutes read time

When you write a post in Wordpress you can set certain custom fields. The default search behaviour of Wordpress is to search only the title and main text of the posts, which makes these custom fields not all that useful. With a little bit of tinkering you can get Wordpress to search any custom fields that you have set, so if you store things like "Author" you can allow people to view all posts by that author by clicking on a link or doing a search. To see more information about Wordpress custom fields see this Wordpress codex site article. Wordpress stores these custom fields in a table called postmeta where each custom field name (called meta_key) is associated with a custom field value (called meta_value).

PHP Logo

Shuffle An Array In PHP

24th January 2008 - 4 minutes read time

To randomise an array in PHP use the shuffle() function like this.


$array = range(1, 5);
shuffle($array); // randomise array

There are two limitations to this function. The first is that it completely removes any key association that you may have set up. So the following array.


$array = array('one'=>1,'two'=>2);

Would be turned into the following after using shuffle().


Array
(
  [0] => 2
  [1] => 1
)

A way around this is to use the following function, this uses the PHP function array_rand() to extract the all of the keys in the array in a random order before ensuring that the key associations are in place.

PHP Logo

Downloading Alexa Data With PHP

23rd January 2008 - 4 minutes read time

It is widely known that the data that Alexa offers on visitor numbers is far from accurate, but it is possible to obtain an XML feed from Alexa that allows you to find out all of the data that Alexa offers, which is more than just their visitor numbers. Passing the correct parameters to this feed you can find out related links, contact and domain information, the Alexa rank, associated keywords and Dmoz listings.

As an example here is a feed URL for getting information about the bbc.co.uk page.


http://xml.alexa.com/data?cli=10&dat=nsa&ver=quirk-searchstatus&uid=19700101000000&userip=127.0.0.1&url=www.bbc.co.uk

So to get information about any site all you have to do is pass the correct URL to this address.

To get this information in a usable form with PHP you can use the curl functions. To download the Alexa feed into PHP use the following code:

PHP Logo

Extend The str_word_count Function In PHP

22nd January 2008 - 6 minutes read time

The str_word_count() function in PHP does exactly what is says it does. The default of this function is to simply count the number of words present. Take the following string.

$str = "This is a 'string' containing m0re than one word. This is a 'string' containing m0re than one word.";

If we pass this to the str_word_count() function with no other parameters we get the number of words.

echo str_word_count($str); // prints 20

The second parameter is the type of value returned from the function. The default value is 0, but 1 and 2 are also available. Using 1 as the second parameters returns an array containing all the words found inside the string. Using 2 returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself. Here are the results from setting the second parameter to 1.