Blog

Random thoughts and musings, often some code examples and experimentations.

Time Since

Recently I have been transfixed on the time since functions offered by Natalie Downe (credit where credit is due). I discovered her code when developing a web application for my previous job at the Chafford Hundred Campus.

At the time I only altered some of the code to make it display how I wanted it too, (just one item, rather than two). But I did not spend too much time thinking about how to add extra comments about the time that has passed.

So with another job comes another project, and I wanted to use the same style of time since comments and tasks were left. Having seen other blogs and sites using similar functions where by they say "left a moment ago", I took another look at the script to see what I could do.

The solution is simple, for the developers out there, you have probably worked this out a long time ago, but for those starting out, here you go.

The whole script works out the time since your timestamp, so the solution to adding the comments about a moment ago is working out the math for the amount of seconds, minutes, hours, weeks etc you want you comment to be associated with. For example if you wanted to say your message was left a moment ago, if it was left 15 seconds or under you would add the following line in the array of "time period chunks":

array(15, ‘a moment ago’),

The possibiliies are endless. For a couple of weeks ago:

array(60 * 60 * 24 * 14, ‘a couple of weeks ago’),

I have left the code in its altered state below. If you do find it useful, please tell me so, and tell me where you have used it too.

/*
 —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  — 
Time Ago Stamp
============================================================
*/
/* Works out the time since the entry post, takes a an argument in unix time (seconds) */
 function time_since($original) {
// array of time period chunks
$chunks = array( array(60 * 60 * 24 * 365 , ‘year’),
array(60 * 60 * 24 * 30 , ‘month’),
array(60 * 60 * 24 * 7 , ‘week’),
array(60 * 60 * 24 , ‘day’),
array(60 * 60 , ‘hour’),
array(60 , ‘minute’),
array(30 , ‘less than a minute’),  // Just add in math like this. array(1 , ‘second’), );
$today = time();
/* Current unix time */
$since = $today - $original;
// $j saves performing the count function each time around the loop
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
 $seconds = $chunks[$i][0];
$name = $chunks[$i][1];
// finding the biggest chunk (if the chunk fits, break)
if (($count = floor($since / $seconds)) != 0) {
// DEBUG print "\n";
break;
}
 }
$print = ($count == 1) ? ‘1 ‘.$name : "$count {$name}s";
if ($i + 1 < $j) {
// now getting the second item
$seconds2 = $chunks[$i + 1][0];
 $name2 = $chunks[$i + 1][1];
}
 return $print; }

 

April 17, 2008 Retweet

New Beginnings

Isn’t it strange how fast life seems to throw new events and beginning’s at you? Last year in February I was panicing at the thought of not being able to afford my mortgage only three months after purchasing a new flat and moving in. The sudden unemployment took me by surprise, but not nearly as much as this year has.

A week after losing my job in Colchester, thank the Lord I accepted a job in Chafford Hundred working as a web application designer and developer within a secondary school. Not having had much experience with application building for the web at that time, I was thrown in at the deep end. Fortunately there was a lot of work already started, and being the fast learner I am, picked up very quickly what I had to do. A year on, I have created many a useful web application for the school, all still in use today.

At that same time last year, things started to move into a serious direction with me and my then girlfriend Rachael. I knew when I first met her back in October 2006 that she was the one for me, but I still was not married to her. In April 2007 I proposed, and we started planning for our wedding. Working in Chafford Hundred has been hard this last year or so, but I have learned a lot from my experience there. This year, 2008 everything sped up.

After Christmas, with the price of petrol going up, and my car giving up, forcing me to purchase (or rather Rachael) a new one, to take me the 69 miles from my flat to work, then back to my parents for mid week stop overs, bed and breakfast accommodation, I was fed up of not living in my home, lower wages and increased cost of living. I started applying for jobs.

There were plenty around, and I had a few interviews. Not before I lost heart at having interviews, and not getting any results, a job became available in Hadleigh, near Ipswich. 10 mins door to door, it had the beginnings of a great job. Whilst advertised as a developer role, I was optimistic and looking forward to a positive result. I was offered the job late February. Exactly a year I after getting made redundant.

I started at my new place of work last week, three days after getting pelted with paint balls at my stag day, a week before I am due to get married. probably three months before I start to look for a new, bigger home with my wife, and not too long after that we may be looking to start a family of our own.

So 2008 seems like it will be a year full of change. New job, new home, wife, and family. New beginnings.

I cannot say enough how excited I am to be getting married in less than seven days time.

March 22, 2008 Retweet

Internet Explorer 8

Well, the time is here when Microsoft finally get their act together and start listening to the web development community. Here comes IE8. CSS3 compatibility? W3C standards approach? I am yet to see.

The sole reason I have failed to take a look at my existing web sites in the eyes of IE8 is because I have spent the first hour after downloading the software trying to get rid of the default homepage.

After spending this hour searching for ways I finally found a statement from Microsoft;

Internet Explorer 8 Beta 1 will add a new page to the home page set. This page will be automatically removed from the home page set two weeks after you install Internet Explorer Beta 1.

How frustrating. Two weeks i have to wait to get rid of this unsightly home page. When all I want to see is my work intranet (I only use IE whilst at work, at home I’m a MAC!). You cannot even change the order, Microsoft have made IE8 to simply insert their homepage everytime it is not found at the start of the homepage list!

Maybe once I have done some work today, ill take a look further into the face of IE8 and discover just how standardized it really is.

March 7, 2008 Retweet

Javascript IE Stumbles!

I thought I may post this little hint at the woes of Internet Explorer. Whilst creating an application laden with Javascript, testing with Firefox, and the wonder that is Firebug, all seems well.
But then comes the dreaded IE game. For some reason my Javascript failed to do what it was supposed to do. That some reason ended up being a COMMA. yes my friends, a ,! trailing at the end of a function IE gives up when it thinks the script carries on and starts looking for the rest of the code, when it cant find it, IE will give up on Javascript all together.

So next time you are pulling your hair out, just check for those commas with nothing proceeding it. That will be what Internet Explorer is moaning about, pretty much guaranteed.

January 24, 2008 Retweet

JQuery AutoSuggest

During the process of developing an application for work the other day, I decided that a normal Javascript enabled interactive menu wasn’t enough for what I wanted. The hunt for an attractive auto suggestion, quick search tool was on.

After many searches and deliberations my focus was set on the JQuery AutoSuggest script from BrandSpankingNew. The first issue I discovered was the PHP and ability to generate the correct array for the autosuggest to read from. The vast amount of questions on the website all point to one thing, no-one knows, or no-one wanted to share how to correctly read and loop from MySQL and create the array in PHP for autosuggest.

Well I am pleased to say, I did it. As comments are closed on BSN I would like to leave this on my web site for everyone searching.
Here goes.

Firstly with test.php (how autosuggest ships) add your connection details to MySQL database. Simple stuff if your used to PHP, if not try this:

$dbhost = ‘host’;
$dbuser = ‘user’;
$dbpassword = ‘password’;
$mysqlLink = mysql_connect($dbhost,$dbuser,$dbpassword);
global $mysqlLink;
mysql_select_db(‘yourDatabase’,$mysqlLink) or die(header("Location: message.php" ));
$query = "SELECT * FROM `table` LIMIT 5 LIKE %’entry’";
$result = mysql_query($query);

Next you need to blank your ARRAY, default it to 0 if you like, this is how you would do it.

$aUsers = Array();
$aInfo = Array();
$aId = Array();
$aSid = Array();

You will see I have added another ID called SID, this could be anything else you wanted to pull back from the database and use later within the results.

Next create your ARRAY, here we need to loop the results and build the array in the format autosuggest is looking for.

$row = "";
while($row = mysql_fetch_array($result))
{
        $aId[] = $row["id"];
        $aSid[] = $row["subjectYear"];
        $aUsers[] = $row["subjectName"];
        $aInfo[] = $row[‘courseLevel’];
}

As you can see we again we have to reset $row to 0, created a while loop with our MySQL connection results, created earlier, and built an array associated with our variables that autosuggest requires.
Thats all you need to do to fetch variable database driven data for autosuggest to suggest from!
But wait, what if you wanted to pass more information to the generated HTML, or form that you are using, so that when you click submit it passes information along the query string to go to a variable page or other use. Well, you will need to edit the line controlled via the variable $aResults.

$aResults[] = array(
     "id"=>($aId[$i]),
    "sid"=>($aSid[$i]) ,
    "value"=>htmlspecialchars($aUsers[$i]),
    "info"=>($aInfo[$i]) );

As you can see, I have added my extra id $aSid into the equation. This will build the array string for JSON with all the information I need to created a query sting!
Next you need to make sure that this information is inserted via the javascript into the correct position, to do this, edit the autosuggest.js around line 315 and simply add you variables:

this.aSug.push(  { ‘id’:jsondata.results[i].id, ‘sid’:jsondata.results[i].sid, ‘value’:jsondata.results[i].value, ‘info’:jsondata.results[i].info }  );

Do you see my SID? Next the callback: function within the options script held on the HTML page, just add in your variable data, I wanted to add in the SID to a hidden field on my form, so that the query string would pass two id’s along the URL to open a dynamically generated page.

callback: function (obj) { document.getElementById(‘pid’).value = obj.id, document.getElementById(‘sid’).value = obj.sid, document.qSearch.submit();}

I also added this line: document.qSearch.submit(); when the user clicks on one of the results, the form gets submitted and jumps to the correct page, much like the quick search on Facebook.

Lesson over. Hope that you found this useful? Let me know if you have any other suggestions, and thanks would be kindly accepted on this blog!

If you liked this you might want to head over to the Facelist jQuery plugin I have created.

January 16, 2008 Retweet

BT Home HUB and Mac’s

Upon receiving my Home Hub from BT I was very impressed with the glossy white cover, matching almost perfectly with my Apple iBook G4 (It would have been perfect if it had the Apple Logo on it).

But unfortunately I had great trouble trying to get the darn thing connected wirelessly, and even more trouble with my Airport Station!

Thankfully though I found a resolution. After some time now, I figured I should share my revelations for anyone stumbling across this Blog.

To setup your BT Home Hub to work with Apple Macs, and Apple Airport Express follow these simple instructions.

Firstly when entering your WEP pass code into your Mac, be sure to select WEP 40/128-bit Hex which is the second option on the list. Do this also when joining your Airport Express to your home network.

Done.

September 7, 2007 Retweet

JQuery Against Anything Else.

I have fallen in love with a new Javascript Library. Gone are the days of prototype, I have found JQuery, which I have quickly learnt and begun to write my own custom variables.

OK so Prototype has the experience, the big guns behind one of the most popular Javascript Libraries around, but JQuery is so simple, you don’t have to borrow someone else’s good looking code, you can make your own. There is a growing library on the web too, with many good developers sharing their skills, like those over at Malsup with clever examples and the briefest but most helpful of code, they have enabled me to play with JQuery and make it work for me. My version Three of Media By will be heavy on JQuery, making use of the graphical interface rather than the technology, all the while making the site look GOOD.

The fear though; ‘is javascript a wise choice for web sites?’ My answer is why not? So many browsers have got their act together with regards to security, and more and more people are becoming aware of web 2.0, you just have to put CSS in your browser to see the abundant list of web 2.0 galleries, and standard compliant sites. Javascript libraries are for the future. They are compatible with everything, they render well even in IE, and they are accessible.

So spread the word, turn on your javascript, the future is now. Lets make the web the future, and make it look good.

April 15, 2007 Retweet

Engagement

Well Rachael and I are happy to announce that we are engaged. Wednesday 4th April 2007. I managed to really embarrace Rachael, and surprise her!

I got down on one knee in a lil restuarant in Ipswich Marina, We both had a huge smile on our faces! Oh and she did say ‘YES’!

If you want more details you will have to ask, leave a comment and i’ll soon get in touch!

Hopefully will speak to you all soon! And you can share my excitement!

April 5, 2007 Retweet

Government Doesn't Care

I am writing this piece in the hope that you will read it and perhaps make a noise about the state in which our government treats its own.
I am British through and through, but the hardships I am put through whilst living in this country, like so many other people makes me think that this Government (Labour 14th Feb 2007) really does not care for its own. Please read on…

I am employed by a small computer company which has been trading since the middle of last year. The Sister company is a phone trader.

Due to the extended verifications being done by HMRC we are now in great danger of losing our livelihoods and all the companies associated will go as well which total six.

They all depend on our phone company for financial upkeep We are innocent of any fraudulent trading but are being penalised. Nine people have already lost their jobs; we have downsized cut costs and done everything humanly possible in order to keep going until HMRC decide to pay our money. We are taking them to Tribunal but as expected they are delaying and delaying. Perhaps in the hope that we will go out of business and there will be no bank account to pay the money to?

We have been presumed guilty until we can prove ourselves innocent; so much for the government encouraging business in the UK. This is a disgrace to all small businesses; it is a step back in the world of Democracy and Fair Trade.

Because HMRC cannot get their act together and have allowed fraudulent traders to get away with funds the innocent have to pay. Will they care when my new flat is reprocessed and others put through hardships which are none of their doing? We have assisted HMRC to the letter and been penalised, perhaps crime does pay, because honesty certainly doesn’t.

February 15, 2007 Retweet

Greatest Band?

Ok, so the fact that I know the Bass player is a little bit bias, but they are a cool band.

Electralyte, since adding their album to iTunes spent many weeks on the main page of the ROCK pages, a feat for a Christian Rock band alone.

There style is alternative, but very easy to listen to, and they are not out to preach to you either. They simply play good music, yes their songs are about Jesus, and the love of God in their lives, but has not every song got a hidden message in it somewhere? Robbie Williams disgracful last album (track 7 inparticular I believe) talks about getting drugs from the doctor in order to get high. So what is the harm in listening about some truth for a change? Whether you beleive in God or not, this band plays good music.

so if your only going to spend 79pence today, let it be on Breakout, the title track from Electralyte Album!
To make things easy i have included a link for all you lucky people with iTunes. Enjoy
Electralyte - Breakout - Breakout

February 3, 2007 Retweet

Ads Via Varied Media

Latest Work

Latest Projects