Make a dynamic year dropdown using PHP

Ever wanted to have a dropdown that automatically showed the current year and the few years before it? This is a quick and easy way to do exactly that! 1 2 3 4 5 6 7 8 <select name="year"> <?php for($i=date("Y")-5;$i<=date("Y");$i++) { $sel = ($i == date('Y')) ? 'selected' : ''; echo "<option value=".$i." ".$sel."-->".date("Y", mktime(0,0,0,0,1,$i)).""; } ?> </select>

Pad a string with zeros using PHP

Recently I had to create a code/username maker to fit into a certain type of pattern so that all “broker codes” (as they were called) followed the same path. An example one looked like so: 1 HJTH0001 The number needed to increment and I wanted to use the auto-increment feature in the mysql database table that was driving this data to make it. This was easy but I ended up with a code that looked like this:

ImportError: No module named MySQLdb (Python)

Trying to use MySQL with Python and getting an ImportError? Traceback (most recent call last): File "some_file.py", line 4, in import MySQLdb as mdb ImportError: No module named MySQLdb This is likely because you have not installed it to the system. You can do this on Ubuntu Linux by running the following code: sudo apt-get install python-mysqldb Also, remember that there is no MySQLdb for python3.x and above. There is however, a fork of it on GitHub at: https://github.

Search for “arabic” is url request and change codepage – ASP

If you are using Classic ASP (yuck) to create an arabic section of a website you can search for the arabic string in your Request URI and then change the session’s codepage as follows: 1 2 3 if instr(1, request.ServerVariables("PATH_INFO"), "arabic") then session.codepage = 1252 end if

Windows 7 is better than Ubuntu 12.04 on desktop!

Today we have finalised for ourselves that Windows 7 is definitely a “better” operating system than Ubuntu 12.04 when it comes to desktop environments. Now, I say “better” in quotes because the word is quite hard to use as is and “convince” everybody while doing so. We started our journey on Windows 7 and after desperately needing a reinstall we decided to go for the latest version (at the time) of Ubuntu, which happened to be the brand spanking new Ubuntu 12.

Function split() is deprecated in PHP

You heard it right! split() is officially a deprecated function. That means that you can still use it if you are really brave and it will work correctly, but don’t expect to see it in later versions of PHP when they come out. Somewhere along the line it WILL not work at all. It has been marked as “deprecated” due to the fact that the explode() function has the exact same output!

Disable Cache in jQuery

I usually run into this problem when dealing with good ol’ Internet Explorer (..any version of IE actually). The problem is that IE tries to be smart and not tell you the new output of a file it fetches more than one time with the same filename, instead it shows you what it saw the first time it loaded that file. You can imagine this would be insanely dumb if you were using a realtime application data source where a script output a different resultset each time, e.

Where is Technology heading, perhaps we should focus more on Software?

I often wonder to myself where the technology industry is heading as I see new things come out almost daily. You look around and something you just bought is all of a sudden outdated. This can be quite irritating for the techno-geeks out there that always want the latest release of every gadget, as it makes it somewhat impossible to keep up with what’s in stores. The really nice thing about software over hardware is that one can just perform a localised update instead of having to purchase new hardware everytime something changes or is improved.

How to break out of an iframe

So someone’s trying to make their site better by opening a page on your site inside of their site using an iframe? They’re doing something like this: 1 <iframe src="http://www.example.com/your_page.html" width="100%" height="100%"></iframe> Well how about you stop that from happening by pasting the following line in your website’s header! 1 2 3 4 5 <script type="text/javascript"> if (top.location != self.location) { top.location = self.location.href; } </script> The above code basically does a check to see if the site is the same as what is said in the address bar, if not then it sets the parent’s frame (the other site) to change to your site’s location!

Edit hosts file on Windows 7

The hosts file is used to manually alter the hostname or IP address in place of it being served by a Nameserver, also known as via DNS. The hosts file is located here: 1 C:\windows\system32\drivers\etc\ Or an easiler way to get to it incase of whatever is like this: 1 %systemroot%\system32\drivers\etc\ This file cannot be editted by “normal users” and requires all changes to be done via an Administrator. This is really easy if you do it in the following way:

(EAI 2)Name or service not known: Could not resolve host name *.80 — ignoring!

You are no doubt trying to add a vhost and you get the following error when attempting to affect changes by restarting the http daemon (httpd): (EAI 2)Name or service not known: Could not resolve host name *.80 — ignoring! Not to worry! Add the following text to httpd.conf at the bottom (That’s at /etc/httpd/conf/httpd.conf): 1 2 3 4 5 6 7 NameVirtualHost 12.34.56.78:80 ServerAdmin [email protected] DocumentRoot /var/www/html/the_path/ ServerName the_path.example.com ErrorLog /logs/the_path.

Virgin Media blocks torrent sites!

Virgin Media have done it again! They have gone and started a “downloaders war”, I’m sure of it. Basically what happened is that Virgin Media are claiming that they have had an order from the Courts to prevent access to a whole bunch of sites which lead to illegal downloading or copyright infringement downloading. Not only is it Virgin Media but all 5 major ISP’s in the UK that have received this Court order, that’s Sky, Everything Everywhere, TalkTalk, O2 and Virgin Media.

How to Comment out a line in a Crontab on Linux

Firstly let’s just note that crontabs are read in the following way: 1 2 3 4 5 6 7 8 * * * * * command to be executed - - - - - | | | | | | | | | +----- day of week (0 - 6) (Sunday=0) | | | +------- month (1 - 12) | | +--------- day of month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59) Your current crontabs can be viewed on your linux setup by typing in the following command:

DateTime conversion function using PHP

It’s really very simple to convert times in different timezones using the following function. 1 2 3 4 5 6 function dateTimeConversion($datetime, $timezone="Europe/London") { $date = new DateTime($datetime, new DateTimeZone("UTC")); $date->setTimezone($timezone); return $date->format("Y-m-d H:i:s"); } As you can see, it takes 2 arguments, $datetime which is a time string and a $timezone which is a timezone to convert to. A usage example would be: 1 echo dateTimeConversion("2012-05-01 17:09:58");

Convert seconds to days, hours, minutes, seconds in PHP

With the following function you can easily convert an integer containing seconds to a nice days, hours, minutes, seconds string or array. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 function secondsToTime($seconds, $return_type="string") { // extract days $days = floor($seconds / 3600 / 24); // extract hours $hours = floor($seconds / 3600) - $days*24; // extract minutes $divisor_for_minutes = $seconds % 3600; $minutes = floor($divisor_for_minutes / 60); // extract the remaining seconds $divisor_for_seconds = $divisor_for_minutes % 60; $seconds = ceil($divisor_for_seconds); // return the final array $obj = array( "d" => (int) $days, "h" => (int) $hours, "m" => (int) $minutes, "s" => (int) $seconds ); $str = ""; if ($obj["d"]!

Remove dotted outline around anchors

This has become quite a common question asked by many website owners. “How do I remove that dotted outline around an anchor?” Lucky for you all, it’s a really easy one to resolve as well. You just have to take a look at CSS for a second and add one line as follows: 1 a { outline:0; } This will disable the outlines around clicked links all around your site.

Get amount of hours between 2 hours

If you would like to get the amount of hours between 10:00 and 12:00 then use this! 1 2 3 4 5 6 7 8 function timeDiff($firstTime,$lastTime) { $firstTime=strtotime($firstTime); $lastTime=strtotime($lastTime); $timeDiff=$lastTime-$firstTime; return $timeDiff; } echo (timeDiff("10:00","12:00")/60)/60;

How to embed HTML5 video with fallback

A common question with the rise of HTML5 becoming more and more common and the whole iGeneration (iPhones/iPads/iEtc) is “how can I embed a video on my site without using a flash player?“. An option that really does work quite well is to use the JWPlayer and set it up to use HTML5. The benefit of going this route is you can also tell it to have a fallback to flash if the browser is not HTML5 compatible, or even fall all the way back to downloading the video if all else fails.

How to backup all mysql databases

In order to backup all mysql databases, you can run the following command in your linux command line: The example below is configured with username “theuser” and password “thepass”. 1 mysqldump -utheuser -pthepass ?all-databases > all_dbs.sql

Invalid command 'RewriteEngine'

If you receive the following error message if means that mod_rewrite is not enabled in Apache: Invalid command ‘RewriteEngine’, perhaps misspelled or defined by a module not included in the server configuration. 1 a2enmod rewrite Bear in mind this is done like this on Linux Ubuntu, not entirely sure if this works for other flavours too.