MySQL Logo

Manage MySQL Databases With phpMyAdmin

7th August 2008 - 2 minutes read time

phpMyAdmin is a tool, written in PHP, that allows you to handle the administration of a MySQL database server. You could always download the MySQL GUI tools, but the problem there is that you need to give external access to a user account, which isn't always possible to do. This is where phpMyAdmin steps in.

Manage MySQL Databases With phpMyAdmin

The tool is easy to use and I have done some tricky stuff with it in the past. It can do everything that you need it to do with MySQL.

PHP Logo

Remove Duplicate Entries In A PHP Array

6th August 2008 - 2 minutes read time

Use the following function to remove all duplicate values in an array.

function remove_duplicated_values($array){
 $newArray = array();
 foreach($array as $key=>$val){
  $newArray[$val] = 1;
 return array_keys($newArray);

The way this function works is by looping through the array and assigning each value of the array to be a key of a new array and setting the value as 1. As the values of the array are added to the new array any new values will lengthen the array and any duplicate values will reset to be 1.

The keys of the new array are then returned as an array of values using the array_keys() PHP function.

Here is an example of the function in action.

PHP Logo

Hiding HTML In Command Line PHP

5th August 2008 - 3 minutes read time

Printing out stuff with PHP is basically essential to any program, but problems can arise if you want to use the same script for command line scripting and web site scripting because of the need to print out HTML. The $_SERVER super global array contains a variable called SERVER_PROTOCOL, which contains the protocol that the client is using to access the script. If the client access through the web then the protocol will contain something like "HTTP 1.1". If the script is being run from the command line then this super global variable SERVER_PROTOCOL would exist.

It would therefore be possible to print out HTML or new line characters depending on if the protocol variable exists. The following function can be used instead of the print() function to print out an HTML <br /> tag when the SERVER_PROTOCOL variable exists and new lines when it is not present.

PHP Logo

Tidy Up A URL With PHP

4th August 2008 - 4 minutes read time

Lots of applications require a user to input a URL and lots of problems occur as a result. I was recently looking for something that would take a URL as an input and allow me to make sure that is was formatted properly. There wasn't anything that did this so I decided to write it myself.

The following function takes in a URL as a string and tries to clean it up. It essentially does this by splitting is apart and then putting it back together again using the parse_url() function. In order to make sure that this function works you need to put a schema in front of the URL, so the first thing the function does (after trimming the string) is to check that a schema exists. If it doesn't then the function adds this onto the end.

PHP Logo

Clean Up A Comma Separated List In PHP

1st August 2008 - 3 minutes read time

Getting users to enter a list of items is a normal practice, but getting users to do it properly is sometimes a challenge in itself. Some users will put spaces in between the commas, others will not, some will even put a trailing comma at the end of the list.

Here is some code that can be used to clear up a comma separated list using some simple regular expressions. It works using the preg_replace() function, and by passing this function an array of options patterns and an array of replacements.

$list= trim($list);
$patterns[0] = '/\s*,\s*/';
$patterns[1] = '/,{2,}/';
$patterns[2] = '/,+$/';
$patterns[3] = '/^,+/';
$replacements[0] = ',';
$replacements[1] = ',';
$replacements[2] = '';
$replacements[3] = '';
$list= preg_replace($patterns, $replacements, $list);

Take the following string, entered by a user at 2 o'clock in the morning, whilst surfing the net drunk after coming home from the pub.

JavaScript Logo

Format Numbers With Commas In JavaScript

31st July 2008 - 5 minutes read time

I found a good function that details how to format numbers with commas in JavaScript and thought I would reproduce it here. It basically takes any number and turns it into formatted string with the thousands separated by commas. The number_format() function is available in PHP and I have been relying on PHP to sort out the formatting before returning it via an AJAX call. I can now include this function into my JavaScript library and do this on the client side.

The function works by using regular expressions to first split the string into whole number and decimal, before splitting the number by groups of three digits.

The regular expression used is (\d+)(\d{3}), which looks for a sequence of three numbers preceded by any numbers. This is a little trick that causes the regular expression engine to work from right to left, instead of left to right.

WordPress Logo

Custom Search Form With Wordpress Search Widget

30th July 2008 - 4 minutes read time

When adding a search form to your Wordpress blog you will want to have control over what sort of form is displayed. It is possible to override the search form created by the widget function without having to go into the /wp-includes/widget.php file and editing the wp_widget_search function. Here is the function that is present in Wordpress 2.6.

WordPress Logo

Getting Started With Wordpress Templates

29th July 2008 - 8 minutes read time

If you are setting up a Wordpress blog the chances are that you will be looking into modifying the default theme to be something a little more customised to your site. Theme development can be as complicated or as simple as you want, or are capable of doing.

Wordpress themes are stored in the folder wp-content/themes/, each theme being stored in it's own directory.

The basic theme must contain two basic files, the main control is done from a file called index.php and a file called styles.css, which is also needed to allow you to display the theme within the admin section of Wordpress. If you don't want to use the styles.css file then this is fine, but it should be present and contain the following lines.

JavaScript Logo

Adding Numbers In JavaScript

28th July 2008 - 3 minutes read time

You think I'm joking right? Well, due to a silly mistake (in my opinion) when creating the language the concatenation character is the same as the plus symbol. This means that sometimes JavaScript will add them together and sometimes it will concatenate them.

This occurs if JavaScript encounters any part of the calculation to be a string. If it is then it will concatenate the whole expression. For example.

alert("1"+2+3);// prints out "123" rather than 6

To stop this you need to add the parseInt() function to the part of the addition that might be mistaken as a string. Or the whole thing just to make sure. The following is a bit of an overkill but ensures that the values will be added, NOT concatenated.

PHP Logo

Practical PHP Programming An Online PHP Resource

25th July 2008 - 2 minutes read time
Practical PHP Programming is a wiki site that has been set up to provide a central area for PHP developers to contribute to learning and understanding PHP. This site was recently called Hudzilla and was a pretty good online PHP book.Practical PHP ProgrammingPractical PHP Programming was a pretty good site when it was just static pages, people were always linking to it from the site in order to explain something that wasn't clear in the documentation. I think that the decision to turn it into a Wiki can only lead to it becoming a better resource.