Script.aculo.us - Web 2.0 Javascript

11th May 2008 - 2 minutes read time

Script.aculo.us is a JavaScript framework that allows you to add effects and advanced JavaScript features to any web page. Although Script.aculo.us can be a little slower than Mootools on occasion it makes up for this by having more stuff built into the library components. For example, the slider control is available of both frameworks, but the Script.aculo.us version comes with some handy features (like filling one part of the slider) where the Mootools version doesn't.

PHP Logo

Convert Time Into Timestamp Or Timestamp Into Time In PHP

10th May 2008 - 3 minutes read time

The easiest (and most reliable) way to store the time in a database table is with a timestamp. It is also the most convenient way of working out time scales as you don't have to do calculations in base 60. In MySQL this is accomplished by the UNIXTIME() function, which can be reversed by using another MySQL function called FROM_UNIXTIME().

However, you can sometimes be left with timestamps in your code and the task of trying to figure out what to do with them.

The first problem is trying to convert a timestamp into a date. So here is a PHP function that does this.

Linux Logo

Delete File By inode Reference

9th May 2008 - 3 minutes read time

If you want to delete a file that you can't type in the name of either because the name is long and complicated, or because it is difficult to type in without causing a syntax error then here is the solution.

You first need to find the inode reference of the file. This can be done by using the command ls -li. The start of each line has a number that is specific to that file. You could use the command ls -i , but the output is a little confusing.

To delete the file use the find command with the flag -inum, followed by a pipe into the rm (remove file) command like the following.

find . -inum 916618 | xargs rm

The xargs bit is used to pass a list of the files found from the find command to the rm command.

JavaScript Logo

Linked HTML Drop Down Menus With JavaScript

8th May 2008 - 3 minutes read time

A good usability feature of web forms is to fill the contents of one drop down menu, depending on what has been selected in a previous menu. This can be done quite easily with JavaScript.

Take the following form, in this instance I have used some server variables used in mod_rewrite, but the idea is valid.

WordPress Logo

Fixing Wordpress Scheduled Posts

7th May 2008 - 5 minutes read time

Wordpress has a neat little feature that allows you to write a post and then schedule it to display at some point in the future. This seems good, but it invariably doesn't work on some server platforms and rather than publishing a post Wordpress just counts the amount of time passed since it was supposed to go live. The basic solution to this is to go into the post and click on publish, which can be a pain if you are taking a couple of days off from blogging and want to leave it running.

The problem lies with the functions that convert a scheduled post into a live post which are kept in the file wp-cron.php in the root Wordpress directory. For some reason the Wordpress developers decided to call the scheduling functions using the fsockopen() function available in PHP. This essentially opens a browser session to the wp-cron.php file, just as you would if you browsed to the location using your web browser.

JavaScript Logo

Check Or Uncheck All Items In A Checklist With JavaScript

6th May 2008 - 3 minutes read time

If you have lots of check boxes in a row a handy little usability trick is to allow a user to click on a button and check all of the checkboxes at once. The following function will either check or uncheck all of the check boxes in your form.


function check_uncheck(truefalse){
 var boxes = document.forms[0].chkboxarray.length;
 var form = document.getElementById('checkForm');
 for(var i=0;i < boxes;i++){
  if (truefalse) {
   form.chkboxarray[i].checked=true;
  } else {
   form.chkboxarray[i].checked=false;
  }
 }
}

All you need to do is to add two buttons to your form that will allow users to either check or uncheck the boxes on the form. The function takes a single parameter, which is what to set the value of the check boxes to, so one button is needed to set them to checked and one to unchecked.

PHP Logo

Banner Advert Rotation With PHP

5th May 2008 - 3 minutes read time

A normal procedure with banner adverts is to rotate them randomly, but also to display some more than others. Lets say that you had 3 banner adds and that you wanted to display them at different rates. To do this you can use the rand() function to generate a random number that can then be used to see what banner advert will be displayed.

First we generate the random number, in our case it is between 1 and 100.


$number = rand(1, 100);

You can then use this number to figure out what banner ad to display. Because the random number is between 1 and 100 it is basically a percentage. So you can create a simple if statement that will determine what banner should be displayed.

PHP Logo

Obfusticating PHP Code

4th May 2008 - 3 minutes read time

You will sometimes want to make sure that your code is a better hidden from the end user. For example, you might want to make sure that your database password files are completely hidden from prying eyes so that even if your web server is hacked your database server isn't also compromised.

Take the following code, which prints out "Hello world".

echo "Hello world";

You can encode this into meaningless text by using the base64_encode() function.

$code = base64_encode('echo "Hello world";');

This turns the $code variable into the following.

ZWNobyAiSGVsbG8gd29ybGQiOw==

Which can be run again by using the bade64_decode() function in conjunction with the eval() function.

Firefox JavaScript Debugging And Development With Firebug

3rd May 2008 - 3 minutes read time

Firebug is by far the best JavaScript debugging plug-in available for Firefox. I have been stuck on a few problems in the past and Firebug has usually provided me with a reason as to why things are going wrong. In fact when developing for other browsers (like Internet Explorer) I can feel a little blind as there are no debugging tools with the power and features of Firebug. It can allow you to stop JavaScript execution at any time using breakpoints.

Firefox JavaScript Debugging And Development With Firebug

Firebug is important if you are creating AJAX applications as it will tell you about every client/server communication, what headers where sent and what the response was.

#! code Logo

Some Common Timestamp Intervals

2nd May 2008 - 2 minutes read time

Using timestamps is quite a common practice, but converting them into "real life" times can be a little hard. So here are some common time intervals that you might need.