Archive for the ‘Development’ Category

killSkype jQuery plugin

Tuesday, October 12th, 2010


Explanation

Skype is a great program. The internet phone call software has changed the way a lot of businesses work and stay in contact with their employess. However, as good as Skype is, it’s not without it’s small problems. Especially if you’re a web developer.

When a user installs Skype, they will also install, possibly unwittingly, plugins for both Internet Explorer and Firefox. These plugins scan the web page currently being viewed and convers all numbers it finds that it thinks are phone numbers and converts them to links that automatically call the number when clicked. If you have Skype installed, the following number will most likely be turned into a clickable link…

+353870000000

It’s useful functionality, but it can play havoc with a webpage design, especially if the matched number is not actually a phone number. To deal with this problem, I created the killSkype jQuery plugin. This plugin takes the number you don’t want to be “Skyped” and renders it in such a way that Skype will ignore it when it’s doing its thing. i.e.

+353870000000

How to use

Simply download the plugin at the link below, include it in your page and then use the javascript code below to prevent Skype from hijacking your pages layout.

2
   $(selector).killSkype()

e.g.

2
3
4
5
6
7
8
<script type="text/javascript">
$(document).ready(function() {
    $('div.dont_skype').killSkype();
});
</script>
 
<div class="dont_skype">00441214124124</div>

Download plugin (includes readable and packed versions): http://dlx-design.com/plugins/killSkype.0.4.0.zip
jQuery Plugins project page: http://plugins.jquery.com/project/killSkype

Comments and critisms appreciated and encouraged.

Share

Changing div background colour based on mouseover

Thursday, October 29th, 2009

A quick and simple guide to changing the background of a div when the mouse floats over it.

The quickest and easiest way of achieving this effect is to apply a :hover pseudo class to whatever div you want to change the background of in CSS.

e.g.

<div id="my-random-div">
    Place your mouse here :)
</div>
div#my-random-div {
    background-color: #fcf;
    border: 1px solid #c0c0c0;
}
div#my-random-div:hover {
    background-color: #ffc;
}

demonstration:

Place your mouse here :)

Of course, as is always the way, this technique doesn’t work in all browsers (I’m looking at you IE6!) (In IE6′s defence, the :hover psuedo class was never designed to work on anything other than the <a> anchor tag, but I digress.)

In order to achieve this effect in IE6 and other browser, we can employ some simple javascript/jQuery. To implement this we use onmouseover and onmouseout, two event handlers that trigger when the mouse is over the div and when the mouse leaves the div respectively. The markup of the div in the page now looks like this

<div id="my-other-random-div" onmouseover="$(this).addClass('moused-over');" onmouseout="$(this).removeClass('moused-over');">
    Place your mouse here this time and see what happens :)
</div>

The jQuery(this) is jQuery selecting the current element, then either adding or removing the class as necessary. Of course, we need the following CSS also.

div#my-other-random-div {
    background-color: #fcf;
    border: 1px solid #c0c0c0;
}
div#my-other-random-div.moused-over {
    background-color: #cff;
}

All this together gives us…

demonstration:

Place your mouse here this time and see what happens :)

This should work in IE6 and all jQuery supported browsers. I hope this helps someone.

- Stevie

Share

Date your articles people!

Monday, October 12th, 2009

A particular pet peeve of mine is you do a search on Google (or Bing if that’s your bag) and when you click through to one of the results there is no date on it. This means you have no way of figuring out whether the article is brand new, outdated or otherwise time sensitive. This can be especially frustrating when dealing with articles about such a fast moving field as web development.

I’ve found this to be a particular problem with blogs based on WordPress as the default theme doesn’t appear to include the date of an article or blog post hides the date in small writing at the bottom of the post in the permalink version of said content. I want the date up top and visible. You’ll notice that my theme does just that ;) If you want to include the date in your permalink post pages, add the following code to the single post page (single.php) wherever you think makes sense. You can then style it however you want. Trust me, your visitors will thank you for it.

24
  <?php the_time('F jS, Y') ?>
Share

Recursion in MySql and PHP

Friday, October 9th, 2009

Working on a PHP website recently, it became necessary to display the top ten most recently updated articles on the home page. The articles were stored in a MySql database in a structure similar to the following.

(s_article)
---------------------------
 id | title | date_updated
---------------------------

At first it appeared that this would be just a simple case of setting up the MySql query in PHP and getting the last last ten updated articles, e.g.

7
8
9
10
11
12
13
$recentsql = "SELECT id, title FROM s_article ORDER BY date_updated DESC LIMIT 10";
$recentUpdates = mysql_query($recentsql) or die ("Could not query db");	
 
for($i=1;$i<=10;$i++){
	$recentUpdatesRow = mysql_fetch_assoc($recentUpdates);
	/* Do whatever is required here */
}

Unfortunately, as in most cases, things weren’t so easy. It turned out that the articles were divided into categories and sub-categories. These categories had their own table and the relationship between articles and categories was specified in a look-up table.

(s_category)
-----------------------
 id | parent_id | name
-----------------------

(s_article_to_category)
--------------------------
 article_id | category_id
--------------------------

The request came down that articles in some of these categories were not to be shown on the front page as recently updated. This meant we would have to exclude articles that appeared in one of the specified categories. The categories that were to be excluded were all some of the root (top-level) categories. Unfortunately the article_category lookup table only specified what the owning category was, not the top level category. To find the top level category of an article (and thusly to determine whether it should be included in top 10 or not) would require some recursion over the categories table in PHP. To achieve this, I wrote this function

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function find_root_category($child_id)
{
	$findparentsql = "SELECT parent_id FROM s_category WHERE id = ".$child_id;
	$query_result = mysql_query($findparentsql) or die ("Could not query db");
	$result_row = mysql_fetch_assoc($query_result);
	$parent_id = $result_row['parent_id'];
 
	if ($parent_id == 0)
	{
		return $child_id;
	}
	else
	{
		return find_root_category($parent_id);
	}
}

This function took an id for a category ($child_id) and returned the top-level ancestor category id for that category. If the top-level category was one of the excluded categories, then we simply ignored this article and moved on. The final code became

28
29
30
31
32
33
34
35
36
37
38
39
40
41
$recentsql = "SELECT id, title, category_id FROM s_article inner join s_article_to_category on id = article_id ORDER BY date_updated DESC";
$recentUpdates = mysql_query($recentsql) or die ("Could not query db");
 
for($i=1;$i<=10;$i++){
	$recentUpdatesRow = mysql_fetch_assoc($recentUpdates);
	$root_cat = find_root_category($recentUpdatesRow['category_id']);
 
	// Category 101 -> Excluded category. Category 78 -> Unpublished category
	if($root_cat == 101 || $root_cat == 78)	{ $i--;	}
	else
	{
		/* Do whatever is required here */ 
	}
}

I hope this article helps someone out there. Any questions about this approach or problems with the code, let me know.

Share