yes i love technology

PHP: Swap Variables One Liner

March 29th, 2007 by Pete

Here's a nifty one liner in PHP which let's you swap 2 variables values.

PHP:
  1. list($a,$b) = array($b,$a);

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 »

Drupal: Hide Navigation on Forum

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:
  1. if (arg(0) == 'node')
  2. {
  3. $node = node_load(array('nid' => arg(1)));
  4. return $node->type != 'forum';
  5. }
  6. elseif (arg(0) == 'forum')
  7. {
  8. return false;
  9. }
  10. else
  11. {
  12. return true;
  13. }
  14. ?>

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 »

PHP: XOR swap

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:
  1. $x ^= $y;
  2. $y ^= $x;
  3. $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 »

JS: Unobtrusive Select All Checkboxes

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:
  1. function selectAll(box)
  2. {
  3. // get the elements name
  4. var id = box.id;
  5. var name = /^([a-z]+)(_select_all)$/i.exec(id);
  6.  
  7. name = name[1];
  8.  
  9. re = new RegExp('^'+name+'_[0-9]+$');
  10.  
  11. // get the elements stauts
  12. var state = box.checked;
  13.  
  14. // get the form element, search "up"
  15. var form = box.parentNode;
  16. while(form.nodeName.toLowerCase() != "form")
  17. {
  18. if(!form.parentNode) break;
  19. form = form.parentNode;
  20. }
  21.  
  22. // search through forms children for email_ checkboxes
  23. for (i=0;i<form.elements.length;i++)
  24. {
  25. if (form.elements[i].type=='checkbox')
  26. {
  27. // Find checkbox for each member of the group:
  28. if (re.test(form.elements[i].name))
  29. {
  30. form.elements[i].checked = state;
  31. }
  32. }
  33. }
  34. }
  35.  
  36.  
  37.  
  38. // onload stuff
  39. window.onload = function()
  40. {
  41. var email = document.getElementById('email_select_all');
  42.  
  43. email.onclick = function(){
  44. return selectAll(email);
  45. };
  46. }

The Javascript will work on a HTML form like this

HTML:
  1. <form method="post">
  2.  
  3. <!-- Select all check box -->
  4. <input id="email_select_all" type="checkbox" />
  5.  
  6. <!-- list of check boxes -->
  7. <input type="checkbox" name="email_1" value="1" />
  8. <input type="checkbox" name="email_2" value="1" />
  9. <input type="checkbox" name="email_3" value="1"  />
  10. <input type="checkbox" name="email_4" value="1"  />
  11.  
  12. <input type="submit" value="Send Message" />
  13. </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 »

SQL: Selecting Specifying Multiple Field Values

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:
  1. SELECT * FROM people
  2. WHERE (first_name = 'pete'
  3. OR first_name = 'paul');

2. Use a Regular Expression

SQL:
  1. SELECT * FROM people
  2. WHERE first_name ~'^pete|paul$';

3. Use an array

SQL:
  1. SELECT * FROM people
  2. 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 »

PHP: Remove Values from Array

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:
  1. // our initial array
  2. $arr = Array("blue", "green", "red", "yellow", "green", "orange", "yellow", "indigo", "red");
  3. print_r($arr);
  4.  
  5. // remove the elements who's values are yellow or red
  6. $arr = array_diff($arr, array("yellow", "red"));
  7. print_r($arr);
  8.  
  9. // optionally you could reindex the array
  10. $arr = array_values($arr);
  11. print_r($arr);

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 »

Bill Gates Urban Myth

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.

Bill Gates Sitting at his desk

  • 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 »

My volatile relationship with Javascript

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 »

My Favourite Electrical Connections

March 19th, 2007 by Pete

People seem to love my lists. Here is one of my favourite electrical connections:

  1. Scart Socket
  2. USB Port
  3. 3.5mm Stereo jack
  4. Double phono
  5. 2.5mm Stereo jack
  6. Parallel port
  7. Ethernet
  8. PS/2

sweet scart leeds with technical information
/* Pete Graham */

Posted in Tech, scart, electrical connections | 2 Comments »

USB ports everywhere, yeah!

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.

UK electrical plug

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.

USB plug and port

/* Pete Graham */

Posted in Tech, usb, plugs, electricity | 2 Comments »

« Previous Entries