
March 29th, 2007 by

Pete
Here's a nifty one liner in PHP which let's you swap 2 variables values.
Much like the PHP XOR swap there's no real reason this is any better than using a temporary variable, but you might be able to impress chicks with it or something.
/* Pete Graham */
Posted in Tech, php, programming |
4 Comments »

March 29th, 2007 by

Pete
If you want to hide certain Navigational Blocks on your Drupal sites forum(s) here’s how to do it:
1. Go to "Administer » Blocks"
2. Click "configure" on the block you want to hide.
3. On Page specific Visibility Settings click the option for "Show if the following PHP code returns TRUE"
4. Enter the following code in the text area:
PHP:
-
if (arg(0) == 'node')
-
{
-
$node = node_load
(array('nid' => arg
(1)));
-
return $node->type != 'forum';
-
}
-
elseif (arg(0) == 'forum')
-
{
-
return false;
-
}
-
else
-
{
-
return true;
-
}
-
?>
This will hide the navigation on the forum hompage, when viewing lists of topics and when viewing individual posts (nodes).
# Pete Graham
Posted in Tech, Website Development, php, drupal |
14 Comments »

March 26th, 2007 by

Pete
The XOR swap lets you swap the value of integer variables without using a temporary variable, it's also slightly faster.
PHP:
-
$x ^= $y;
-
$y ^= $x;
-
$x ^= $y;
I can't actually think of a good reason Why you'd want to use this in PHP since memory isn't normally a big issue, however it might be good for showing off
.
// Pete Graham xXx
Posted in Uncategorized, Website Development, php, computer science |
2 Comments »

March 26th, 2007 by

Pete
I wrote last week about how I’m trying to improve my JavaScript skills. As a small exercise I have written this function for unobtrusively selecting/deselecting checkboxes in a form.
JAVASCRIPT:
-
function selectAll(box)
-
{
-
// get the elements name
-
var id = box.id;
-
var name = /^([a-z]+)(_select_all)$/i.exec(id);
-
-
name = name[1];
-
-
re = new RegExp('^'+name+'_[0-9]+$');
-
-
// get the elements stauts
-
var state = box.checked;
-
-
// get the form element, search "up"
-
var form = box.parentNode;
-
while(form.nodeName.toLowerCase() != "form")
-
{
-
if(!form.parentNode) break;
-
form = form.parentNode;
-
}
-
-
// search through forms children for email_ checkboxes
-
for (i=0;i<form.elements.length;i++)
-
{
-
if (form.elements[i].type=='checkbox')
-
{
-
// Find checkbox for each member of the group:
-
if (re.test(form.elements[i].name))
-
{
-
form.elements[i].checked = state;
-
}
-
}
-
}
-
}
-
-
-
-
// onload stuff
-
window.onload = function()
-
{
-
var email = document.getElementById('email_select_all');
-
-
email.onclick = function(){
-
return selectAll(email);
-
};
-
}
The Javascript will work on a HTML form like this
HTML:
-
-
-
<!-- Select all check box -->
-
<input id="email_select_all" type="checkbox" />
-
-
<!-- list of check boxes -->
-
<input type="checkbox" name="email_1" value="1" />
-
<input type="checkbox" name="email_2" value="1" />
-
<input type="checkbox" name="email_3" value="1" />
-
<input type="checkbox" name="email_4" value="1" />
-
-
<input type="submit" value="Send Message" />
-
</form>
As long at the select all checkbox has an id of "something_select_all" and the checkboxes have id's of "something_"followed by a number then this will work. No inline Javascript is required all the JS can be put in a separate file.
Note: If any Javascript Gurus are reading that have pointers about how I could improve this function then please put your suggestions in the comments.
# Pete Graham
Posted in Tech, javascript, Website Development, unobtrusive |
2 Comments »

March 22nd, 2007 by

Pete
Here are 3 different ways to perform a DB select when you want to specify two or more values for a field.
1. Use th OR operator
SQL:
-
SELECT * FROM people
-
WHERE (first_name = 'pete'
-
OR first_name = 'paul');
2. Use a Regular Expression
SQL:
-
SELECT * FROM people
-
WHERE first_name ~'^pete|paul$';
3. Use an array
SQL:
-
SELECT * FROM people
-
WHERE first_name = ANY('{pete,paul}');
Note: These should all work in PostgreSQL, I suspect the SQL may need altering for 2 and 3 to work with MySQL.
# Pete Graham
Posted in Tech, regular expressions, database, sql |
No Comments »

March 22nd, 2007 by

Pete
If you need to remove elements from an array that have a particular value(s) here’s a neat way of doing it without any looping:
PHP:
-
// our initial array
-
$arr =
Array("blue",
"green",
"red",
"yellow",
"green",
"orange",
"yellow",
"indigo",
"red");
-
-
-
// remove the elements who's values are yellow or red
-
-
-
-
// optionally you could reindex the array
-
-
This is the output from the code above:
Array ( [0] => blue [1] => green [2] => red [3] => yellow [4] => green [5] => orange [6] => yellow [7] => indigo [8] => red )
Array ( [0] => blue [1] => green [4] => green [5] => orange [7] => indigo )
Array ( [0] => blue [1] => green [2] => green [3] => orange [4] => indigo )
Pete Graham xXx
Posted in Tech, php, arrays, programming |
11 Comments »

March 21st, 2007 by

Pete
There’s an Urban Myth that Bill Gates is so rich that if he was to drop a $1000 bill on the ground, it's just not worth his time to bend over and pick it up. Now it is true that he wouldn’t pick it up, however the real reason for this is he was wedgied multiple times as a child and now has a phobia of picking things of the floor.

- FACT: In the hilarious 1990s comedy Bill and Ted wedgieing a person is referred to as giving someone a Melvin
- FACT: You can escape the Grim Reaper by Melvining him!
The Wikipedia Wedgie page is well worth reading.
# Pete Graham
Posted in Tech, Bill Gates, wedgie |
No Comments »

March 20th, 2007 by

Pete
I started work in web development in 2004 at this point in time JavaScript was hugely unpopular in terms of web trends, this is probably because it was mostly used for annoying pop-ups, animations and whole list of other horrendous Internet crimes. I can recall having a King Herod style culling of all JavaScript from the TCAT website, my first main project.
Recently due to the popularity of AJAX web applications JavaScript’s popularity seems to have exploded again! I’ve had to brush up on my JavaScript skills for some of the User Interface work on phuser.com. Whenever I do JavaScript development I never feel as comfortable as I do with PHP or CSS. It’s definitely the weakest weapon in my web-dev arsenal.
I think it’s time to put my JavaScript prejudices behind me and get my hands dirty with some serious JS development. Now I’m not a JavaScript novice, I’ve got a few DHTML tricks up my sleeve I learnt back in the day. However I rarely write my own JavaScript, if I need a function I’ll look for one someone else has written.
When I’ve needed to learn about Javascript in the past I’ve always find it surprisingly hard to find good quality decent resources (compared to other Web technologies). I shall be posting links to any JavaScript articles/tutorials that I find particularly useful on this blog.
Articles
# Pete Graham
Posted in Tech, javascript, Website Development, usability |
3 Comments »

March 19th, 2007 by

Pete
People seem to love my lists. Here is one of my favourite electrical connections:
- Scart Socket
- USB Port
- 3.5mm Stereo jack
- Double phono
- 2.5mm Stereo jack
- Parallel port
- Ethernet
- PS/2

/* Pete Graham */
Posted in Tech, scart, electrical connections |
2 Comments »

March 19th, 2007 by

Pete
I used to think that it would be a dragons-den-tastic idea to manufacture computer cases with extension plugs built into the back of the case, this would mean you wouldn’t need to buy a separate extension plug (for your printer, scanner, modem, router, etc) when you buy a new PC. Although I still think this is a good idea, many computer peripherals are powered via USB these days so I don’t think it’s quite a marketable an idea as it used to be.

Now turning the idea completely on its head imagine if in the future we do away with plug sockets completely in our houses and just have USB ports instead. These ports would be used primarily for providing electrical power to devices, however in theory they could connect to some sort of domestic computer system that could interface with all your houses devices (fridges, entertainment system, vacuum cleaner, robot pets, etc). Many modern cars have sophisticated computer systems built into them so why not include a few USB ports as well? Not only are USB ports more compact than traditional plugs but it would also prevent you needing adaptors for using electronic devices abroad.

/* Pete Graham */
Posted in Tech, usb, plugs, electricity |
2 Comments »