Learn JavaScript With Books, Videos And Meeting Rooms

23 July, 2008 | JavaScript | No comments

Learning JavaScript can be tricky, especially as there are so many things to get wrong in the language if you don’t know what you are doing. It is almost impossible to debug code if you don’t have a very clear understanding of what is going on. The three solutions to this are to read books, watch some videos and to go on a course.

Tuition

If you live in the UK then you can find many JavaScript training courses available, which are held at high quality meeting rooms. Attending a course can help you to get started in the subject and will equip you with the tools you need to continue learning at your own rate. I would really recommend taking this approach with a large and generally abused subject like JavaScript. There are so many ways to go wrong when you set out learning the language, especially because it doesn’t "act" like a lot of other languages. Superficially it might be similar, but if you are intent on writing any serious applications then you need to understand the object model of JavaScript, which many books just miss out.

Books

There are a lot of books available on JavaScript, the majority of which will take a very "look at this, isn’t it pretty" approach to the language. This is unfortunate as this can only lead to people misusing the language to do stupid things in a browser, which is what has happened. However, there are some really good books out there, here are a few that I have read myself and have thought to be very good.

Videos

There are numerous videos available on the Internet, but finding the good ones is also a challenge. Here are some very good videos, most are by Douglas Crockford, but I have managed to find some others.

Here is the first from Douglas Crockford to get you started.

Using !$ To Use Last Parameter

22 July, 2008 | Unix/Linux | No comments

Much like using Alt+. to print out last parameter you can also use !$ to use the last parameter from the previous command. Here is a simple example.

# cd ..
# cd !$

In this example we are moving up a directory and then doing the same action again, the !$ is a short cut to get hold of the ... This is more useful when doing things with longer parameters like directory or file names. For example here we are creating a directory and then moving into that same directory.

# mkdir /www/htdocs/directory/
# cd !$

Raising Skinny Elephants Is Utterly Boring

21 July, 2008 | Unix/Linux | No comments

This might sound odd, but this is a mnemonic that helps you remember a sequence of letters that you can enter when your Linux system is locked. This is a last ditch attempt to get things up and running again and should only be used if all else fails and the only other thing that you can do it pull the plug.

If you have also tried pressing Ctrl+Alt+backspace and this does nothing then you can try using the key sequence Raising Skinny Elephants Is Utterly Boring.

Hold down the left Alt key and the SysRq key (found on the print screen button) and press each letter in turn. Make sure that you give a little time between keystrokes.

  • r
  • s
  • e
  • i
  • u
  • b

Here is a description of what you are doing.

  • The r stands for put keyboard in raw mode
  • The s for sync the disk
  • The e for terminate all processes
  • The i for kill all processes
  • The u for remount all filesystems read only
  • The b for reboot the system

Also , if your filesystem is Ext3 or ReiserFS and on reboot it wants you to do a filesystem check, don’t touch any key when it asks you to press "Y" and let it recover the journal automatically.

Also note that for this to work you need to have the SysRq key enabled in the Linux kernel, also called CONFIG_MAGIC_SYSRQ. You can check if it is enabled by typing:
ls /proc/sys/kernel/sysrq
If this prints out a line with that word then the key is enabled.

Printing Out File And Class Information In PHP

18 July, 2008 | PHP | No comments

If you are debugging a PHP application then you might want more information than the values of some current variables. There are a number of built in magic variables that can be used to print out the file name, line number, class and method that the debug statement is printed out on. Here is an example that prints out some information from a class.

class class_test{
 
function method_test(){
  // the full path to the current file
  print 'File: '.__FILE__.'<br />';
 
  // print the current line
  print 'Line: '.__LINE__.'<br />';
 
  // print the current class name
  print 'class: '.__CLASS__.'<br />';
 
  // print the current method name
  print 'method: '.__METHOD__.'<br />';
 
  // directory separator of the current
  // system (windows = \ and linux = /)
  print 'Directory separator: '.DIRECTORY_SEPARATOR.'<br />';
 }
}
$test = new class_test();
$test->method_test();

This will print out something like the following.

File: C:\Apache Software Foundation\Apache2.2\htdocs\test.php
Line: 10
class: class_test
method: class_test::method_test
Directory separator: \

Optimize A MySQL Table Using PHP

17 July, 2008 | PHP | No comments

In MySQL the OPTIMIZE TABLE can be used if you have made changes or have deleted large parts of the table.

Any deleted rows are kept behind the scenes in the server in order to allow the reuse of these spaces. The OPTIMIZE TABLE command reclaims the unused space and defragments the data file.

For a normal MyISAM table the OPTIMIZE command works in the following way.

  1. If the table has deleted or split rows, repair the table.
  2. If the index pages are not sorted, sort them.
  3. If the table’s statistics are not up to date (and the repair could not be accomplished by sorting the index), update them.

You can do this automatically by using the following PHP code.

// connect to database
$con = mysql_connect("localhost","root","wibble");
// select the correct database
mysql_select_db("database");
// get a list of the tables
$alltables = mysql_query("SHOW TABLES;");
 
// record the output
$output = array();
 
while($table = mysql_fetch_assoc($alltables)){
 foreach($table as $db => $tablename){
  $sql = 'OPTIMIZE TABLE '.$tablename.';';
  $response = mysql_query($sql) or die(mysql_error());
  $output[] = mysql_fetch_assoc($response);
 };
};
// print output
print_r($output);

Here is a sample of what the output array contains

[0] => Array
(
 [Table] => database.table1
 [Op] => optimize
 [Msg_type] => status
 [Msg_text] => Table is already up to date
)
 
[1] => Array
(
 [Table] => database.table2
 [Op] => optimize
 [Msg_type] => status
 [Msg_text] => OK
)

Note that you don’t need to run this command every time you do anything. You should only run this after a major data upheaval, or after a few months or weeks of usage.