Some Useful Maths Functions In JavaScript

14th January 2008 - 4 minutes read time

All of the maths functions in JavaScript are kept in a handy object called Math, which contains a number of different functions.

To get the absolute value of a number use the abs() function.

Math.abs(3.14159265) // returns 3.14159265

Rounding a number is done by either the round() function to round to the nearest integer, the ceil() function to round up to the nearest integer and the floor() function to round down to the nearest integer.

Redirecting The Page In PHP

13th January 2008 - 3 minutes read time

To redirect the current page to a different location you use the header() function in the following way:

header("http://www.hashbangcode.com");

You can use this function when you want to point the user to a different page. If you are writing a login script then this function would be useful to show the user a certain page depending on them entering the correct user information.

Highlight Code In PHP

12th January 2008 - 2 minutes read time

When printing off source code there is a handy function that will parse the code and produce nice looking syntax highlighted code. There are actually two functions you can use. The highlight_string() function takes a string as a parameter and will print the highlighted code. The highlight_file() function takes a file name as a parameter, the contents of which are printed off with highlighted syntax. For now I will concentrate on the highlight_string() function, but the output of these two functions is the same.

To use the highlight_string() function just pass it a string. The following code:

Multiple Google Analytics On Same Page

10th January 2008 - 1 minute read time

To set up multiple Google Analytics tags on the same page you need to use the _uff = false; command in between the unchinTracker() calls to reset the tracker for the next account. The urchinTracker() function will send information on the page visit off to Google Analytics.

Getting A Random Row From A PostgreSQL Table

10th January 2008 - 1 minute read time

To get a random row from a PostgreSQL database you need to use the RANDOM() function. This is similar to the MySQL function RAND() and will generate a new random number for each row and order them by that new number. This is used in conjunction with the LIMIT clause to limit the amount of returned rows to one.

SELECT VALUE FROM TABLE ORDER BY RANDOM() LIMIT 1

Check Slash on Database Input

10th January 2008 - 2 minutes read time

If you are querying a database you should get into the habit of sanitising your input, even if it's not coming from users at the moment it might do in the future. SQL injection attacks are all too common and they can be easily prevented.

The addslashes() function takes a string as the input and returns a string with backslashes before all characters that required quoting in database queries. The characters it acts on are quote, double quote, backslash and NUL (or a NULL byte). Magic quotes runs addslashes() on all COOKIE, GET and POST data. The important thing is not to use addslashes() and magic quotes on the same string as everything will be double escaped.

Use the get_magic_quotes_gpc() function to see if magic quotes is enabled. If it isn't then use the PHP function addslashes() to do the same thing. Use this function if you have any user input in order to sanitise anything that you are about to send to a database.

Getting A Random Row From A MS SQL Table

9th January 2008 - 1 minute read time

To get a random row from a Microsoft SQL database you need to use the NEWID() function. This will generate a new random number for each row and order them by that new number. This is used in conjunction with the TOP clause to limit the amount of returned rows to one.

SELECT TOP 1 VALUE FROM TABLE ORDER BY NEWID()

 

PHP Website Function Lookup

8th January 2008 - 2 minutes read time

Those of you who have coded in PHP must have looked at the PHP website at some point, even if it was just to down load the latest version, or to look up a function reference.

What most people don't realise is that there is a really easy way to go straight to the page that you want without having to mess around with the tetchy search engine on the PHP site.

Lets say you wanted to know about the function phpinfo(). This is a debugging function that displays everything you could possible imagine about the PHP environment and is usually used by people who are just starting out with the language as it prints a lot of stuff to the screen.

To look at the reference for the phpinfo() function you could go to the following URL.

PHP Array Mode Function

7th January 2008 - 2 minutes read time

The following mode function will return the most commonly occurring value from an array of values, also called the mode. If just the array is used then only the most commonly occurring value will be returned. The second parameter can be used to return an array containing the mode and the number of times that this value occurs in the array.

Rounding A Number To Nearest The Thousand In PHP

6th January 2008 - 3 minutes read time

I have previously talked about rounding numbers in PHP, but what if you wanted to round the number to the nearest thousand?

It is possible to do this with the native round() function in PHP by using a negative number as the second parameter. The round() function has two parameters, the first is the number to be rounded and the second is the number of places to round the number to, also known as the precision. The default for round() is to round the number to the nearest whole number, by using positive numbers as the second parameter you can set the number of decimal places to round the number to. Giving a negative number as the second parameter will allow you to round the number to the nearest full number. For example, to round a number to the nearest thousand you can put -3 as the precision, this will change the number 12,345 into 12000.