Welcome to the Boolean blog

Here you will find details of news, projects, releases and other bits and pieces that we are working on at Boolean. We hope that you find something of interest amongst the archives.

Code snippets, methodologies and useful links will also be mentioned here.

Where there is more to logic than TRUE or FALSE

Archive for 'Development'

Currency Conversion using Yahoo Finance API

Just a quick and dirty PHP function to retrieve information from the Yahoo Finance API to perform a currency conversion

function ConvertCurrency($From,$To,$Amount){
$URL = ‘http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=’. $From . $To .’=X’;
$Handle = @fopen($URL, ‘r’);

if ($Handle) {
$Result = fgets($Handle, 4096);
fclose($Handle);
}
$allData = explode(‘,’,$Result);
$Value = $allData[1];

return $Value * $Amount;
}

Usage:

$Amount = ConvertCurrency(“NZD”,”USD”,25);

Using mod_rewrite to remove variables from a URL

The Apache mod_rewrite module offers an unparalleled toolset for manipulating URLs, – however configuration can be a tricky beast at times. While most people are comfortable enough removing file extensions like .php we recently had a user who wanted to hide all the variables in the address string.

For example index.php?id=id would end up being index/id

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^about/(.*)$ index.php?id=$1 [QSA]

The magic here is the QSA or Query String Append. If you just wanted to remove the .php you would use

RewriteCond %{REQUEST_URI} !.*\.php$
RewriteRule ^/(.*)$ $1.php [QSA]

Find the size of all databases on a server

A quick way to create a view to show file size statistics across databases on a server.

DROP VIEW IF EXISTS alldb;
CREATE VIEW AllDatabases AS
SELECT
s.schema_name AS ‘Schema’,
SUM (t.data_length) AS Data,
SUM ( t.index_length ) AS Indexes,
SUM (t.data_length) + SUM (t.index_length) AS ‘Mb Used’,
IF (SUM(t.data_free)=0,”,SUM (t.data_free)) As ‘Mb Free’,
IF (SUM(t.data_free)=0,”, 100 * (SUM (t.data_length) + SUM (t.index_length)) / ((SUM (t.data_length)+SUM (t.index_length) + SUM (IFNULL(t.data_free,0))) ) ) AS ‘Pct Used’, COUNT (table_name) AS Tables
FROM information_schema.schemata s
LEFT JOIN information_schema.tables t ON s.schema_name = t.table_schema
GROUP BY s.schema_name
WITH ROLLUP

Making a CSS div behave like a link

The requirements were that Javascript shouldn’t be necessary (bad SEO and accessibility) and that the HTML had to validate. For some examples have a look at Facebook advertisements. Here is how it was done:

  • Create the div as normal using HTML and CSS. In the div you will need to place the link you are wanting to be accessible.
  • Inside that link you will need an empty span tag.
  • Make sure that the div is styled in CSS with position:relative
  • Apply the following style to the empty span tag:
{ position:absolute;
width:100%;
height:100%;
top:0;
left: 0;
z-index: 1 }

Any other links you may have inside the div will require position:relative and a suitable z-index (greater than 1)

Where is my Firebug icon?

The icon of the wonderful plugin Firebug recently moved from its familiar location at the bottom right corner of Firefox. This was due to a UI change in the status bar of Firebox from version 4 onwards. The Firebug blog does a good explanation of why this was necessary.

What I did to fix however was to type about:config in my Firefox address bar.
I accepted the “I’ll be careful, promise!” warning.
To narrow down the (many) configuration options I applied a filter “firebug.show”
Finally I changed “extensions.firebug.showStatusIcon” to true.

29/01/2012 From the number of pageviews this post receives I see this is common problem. I hope it helps people solve the issue.

IE blocking iFrame Cookies

Facebook recently moving their applications inside an iFrame has turned web development on its head once again. After being deprecated in XHTML and barely hanging on in HTML iFrames looked like they had all but become extinct. Not so!

So, although other browsers do not block and intercept your cookie within an iFrame (and hence Facebook application) Internet Explorer will do. The problem lies with the W3C standard called Platform for Privacy Preferences (P3P) and you can read about it here if you wish.

In PHP all you will need to ensure Internet Explorer accepts third party cookies is to send the below header:

header(‘P3P:CP=”IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT”‘);

Note: by using such a P3P statement you are making a legal statement about how your application handles cookies and if you are later found in violation of the statement you make, you may find yourself in criminal or civil hot water.

PHP Script Execution Timer Class

Often for testing the efficieny of some PHP code we use script execution calculations such as the microtime function. What is infinitely more useful however is the ability to time the execution of all, some, or multiple sections of code or series of lines.
Unfortunately I cant remember the origins of this timer class – but this code allows you to stop, start, pause and resume timing of specific sections of your php code. Thank you, whoever you may be.

Download the class here

Available methods:

start() – start/resume the timer
stop() – stop/pause the timer
reset() – reset the timer
get([$format]) Which can defaults to Timer::SECONDS but can also be Timer::MILLISECONDS or Timer::MICROSECONDS

Usage example:

$timer1 = new Timer();
$timer2 = new Timer();

$timer1->start();
// do some code

// calculate the time it takes to run a function
$timer2->start();
functionX();
$timer2->stop();

$timer1->stop();

print $timer1->get();
print $timer2->get();

Image Placeholders

placekitten

A quick and simple service for obtaining pictures of kittens for use as placeholders in your designs or code.

Simply place the required image size after their url (for example http://placekitten/200/300) and you will be rewarded with a cute placeholder image. Great for quick real code mockups…

Check it out here