RSS
formats

Get the total size of all the files in a directory

Published on January 15, 2014 by in General

It’s amazing how many times I’ve need to find out the total size of all the files in a directory on one of our servers.

I have grown to love the simplicity of the `du` command.

du -sh

du = Display disk Usage statistics

 
Tags: , , ,
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
formats

Validate Email Address in Javascript

Published on November 5, 2013 by in General

Below is a nice little Javascript RegularExpression to validate email addresses.

/^\w.+@[azAZ_.]+?\.[azAZ.]{2,3}$/

..and to use it..

if (/^\w.+@[a-zA-Z_.]+?\.[a-zA-Z.]{2,3}$/.test(email)) alert("Success");
else alert("Fail");
 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
formats

Remove hash from window.location in Javascript

Published on August 9, 2013 by in General

E.g. URL:
http://example.com/?option=1&task=2&listing_id=36&layout=4&table=5#some_hash_value_here

So how would you get the current URL using Javascript?

That is really easy, you can use window.location.href or just simply window.location.

But how do you replace the #hash at the end if it exists?

That too is quite easy, you can just do something like this:

window.location.href.split("#")[0]

or

window.location.href.substr(0, window.location.href.indexOf("#"))

So now that we have the value of the current URL how do we refresh the page with it?

window.location = window.location.href.split("#")[0];

or

window.location = window.location.href.substr(0, window.location.href.indexOf("#"));
 
Tags:
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
formats

URL GET vars to PHP Array

Published on May 14, 2013 by in General

Sometimes you will need to retrieve the GET variables passed into the current page URI or you will have a URL string to work from which contains certain GET variables, the below method helps a lot to convert them into an array which you can easily manipulate later.

$url = $_SERVER["REQUEST_URI"];
parse_str(parse_url($url, PHP_URL_QUERY), $array);

`$array` is now an array of all the GET variables in the URL.

Alternatively you can pass a URI string in place of the $_SERVER[“REQUEST_URI”] by replacing the `$url` variable with something else.

No need for an example on that one, as I’m sure if you’re already reading this post then you will know what to do.

If all else fails then leave a comment below..

 
Tags: ,
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
formats

python "AttributeError: ZipFile instance has no attribute '__exit__"

Published on May 8, 2013 by in General

This is actually a very easy error to fix, eventhough off the bat it seems a lot more involved.

You probably have syntax something like this:

with zipfile.ZipFile(wr_zip) as zipfd:
    extract(zipfd, wr_csv, wr_csv)

So instead we will change it to this:

zipfd = zipfile.ZipFile(wr_zip)
extract(zipfd, wr_csv, wr_csv)

The reason this fixes it is because at the moment (Python 2.6/2.7 I believe) the zipfile.ZipFile class has no __exit__ attribute, so it does not work with a `with statement` as other file objects do.

 
Tags: ,
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
formats

python “AttributeError: ZipFile instance has no attribute ‘__exit__”

Published on May 8, 2013 by in Python

This is actually a very easy error to fix, eventhough off the bat it seems a lot more involved.

You probably have syntax something like this:

with zipfile.ZipFile(wr_zip) as zipfd:
    extract(zipfd, wr_csv, wr_csv)

So instead we will change it to this:

zipfd = zipfile.ZipFile(wr_zip)
extract(zipfd, wr_csv, wr_csv)

The reason this fixes it is because at the moment (Python 2.6/2.7 I believe) the zipfile.ZipFile class has no __exit__ attribute, so it does not work with a `with statement` as other file objects do.

 
Tags: ,
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
formats

Remove specific HTML tags using PHP

Published on March 21, 2013 by in php

There are times when you want to remove a specific HTML tag from an HTML block of text.

This could be an anchor(<a/>>) or an image(<img/>) perhaps.

You can use preg_replace to do this quite quickly and efficiently.

Remove an anchor:

$content = "Sample text <a href=’#’>Our anchor</a>. Etc etc";
$content = preg_replace(‘/<\/?a[^>]*>/’,,$content);
//$content is now -> "Sample text. Etc etc";

Remove an image:

$content = "Sample text <img src=’our_image.jpg’ />. Etc etc";
$content = preg_replace(‘/<\/?a[^>]*>/’,,$content);
//$content is now -> "Sample text. Etc etc";
 
Tags: ,
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
formats

Keep Google Map v3 centered when browser is resized

Published on March 8, 2013 by in General

Using Google Maps V3 Javascript API you can keep the map centered to the browser’s window by using the following trick when resizing the window.

var mapOptions = {
    zoom: 2,
    center: new google.maps.LatLng(0,0), mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

// this is our gem
google.maps.event.addDomListener(window, "resize", function() {
    var center = map.getCenter();
    google.maps.event.trigger(map, "resize");
    map.setCenter(center);
});

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
20 Comments  comments 
formats

Buy a Google Nexus 10 in the UK!

Published on December 29, 2012 by in General


If you are looking to get a Google Nexus 10 inch tablet and are based in the UK you are in for a shocker!

The only place to buy it is at the Google Play store and since about the middle of November 2012 they have been sold out.
Some people say it’s due to delayed deliveries and orders and some say it’s just bad management on Google’s behalf. I think the latter is probably the thing that actually happened as no other tablet providers seem to ever have this problem.

For now we will wait around and see what transpires in the next few weeks.

11MARCH 2013 – UPDATE: It appears as though they have gotten themselves sorted out and the Nexus 10 is now available with larger stock quantities.

 
Tags: , ,
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
formats

Let Joomla and MySQL interact!

Published on December 21, 2012 by in General

I often need a quick and easy few lines to retrieve some data from MySQL using Joomla without all the MVC nonsense that usually goes about this topic.

So here is how I do it!

$dbSEL = JFactory::getDbo();
$dbSEL->setQuery("SELECT MAX(`start_price`) FROM #__some_table");
$dbSEL->query();
echo $dbSEL->fetchRow();

As you can see above, we get the current database object and then run a query on it.
You may notice the hash (#) in the SQL statement of this example, Joomla replaces that (as well as the underscores (__) with the database prefix which is configured, so on shared nodes you don’t have to type all that extra stuff that will need to be changed later.

 
Tags: , ,
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
formats

Perform a Mysql Query using Joomla!

Published on November 23, 2012 by in General

This is really actually meant for future reference for myself as lately I’ve been doing quite a lot of Joomla! mysql coding and have used the following code just short of 3million times.

$db = JFactory::getDbo();
$querySelectId = "SELECT `id` FROM #__some_table WHERE `user_id`=’$user_id‘";
$db->setQuery($querySelectId);
$db->query();
$data = $db->loadAssoc();

What we do in the above code snippet is select a user_id from some_table (#__) is short for whatever your database table’s prefix is (Joomla! replaces this itself) and then we perform the query and return the row’s `id` into the $data variable.

 
Tags: , ,
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
formats

Refresh User Data in Joomla

Published on October 21, 2012 by in Joomla, php

I was busy with a custom component in Joomla, and it stored it’s own user_details based off of the main users table, but if the details were changed then I needed the system to update the session to reflect the changes.

This is what I did:

$user = JFactory::getUser();
$session = JFactory::getSession();
$session->set("user", new JUser($user->id));
$user = JFactory::getUser();
 
Tags: , ,
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
© Copyright © 2016 · Web Development London UK · All rights reserved
By using the site and/or making use of any of our services you agree to our Terms and Conditions