2010
01.15

Whilst working on our online game Street Crime - Mmorpg Gangster Game I came accross the need to add a wrapper layer to all of our ajax calls thus to make sure players are always logged in before they make the call and if not then re-direct them too our login pages.

Most of you who are familiar with jQuery will also be familiar with the $.ajax() function that is commonly used for making ajax requests.

I decided to use jQuerys plugin functionality to create the wrapper layer.

jQuery.AJAXwrapper=function(options) {
var defaults = {
extensions: '',
theurl: '',
thetype: "POST",
defaultdataType: "xml",
payload: ''
};
var options = $.extend(defaults, options);
$.ajax({
url: options.theurl,
type: options.thetype,
data: options.extensions,
dataType: options.defaultdataType,
beforeSend: function(){
//in here insert your custom function
},
success: function(xml){
if(typeof options.payload==='function'){
options.payload(xml);
}
}
});
};

I call my function above by using a simple bit of code that you can added to any action/event you like.
$(document).ready(function(){
$("#myid").click(function(){
AJAXwrapper({
theurl: "somephp.php",
extensions: "action=refresh",
payload: yourcustomfunction;
});
});
});

And finally I stick an alert in the custom function I have selected to be my on success from the AJAXwrapper function.

function yourcustomfunction(){
alert("we made it to this bit");
}

There you have it now you could make all of your ajax calls using AJAXwrapper({}) and its possible values which I have listed below for clarity.

  • extensions - These are your normal ‘data’ fields in the normal $.ajax() function so something like ‘action=fred’
  • theurl - Quite simply the url you want AJAXwrapper to call
  • thetype - The type of request your making GET,POST default is set to POST
  • defaultdataType - The default data type of the call my function is defaulted to xml, but you could choose JSON,HTML etc
  • payload - Finally the payload, this is your defined function for AJAXwrapper to call when your request comes back successfully.
2009
12.15

When working on your server with ssh access, you tend to get very used to doing everything you need to do via the terminal window. Sometimes you may come accross problems which unless you have the know how you will be puzzled as to why somethings not working.

Today I came across a small problem in that a few commands which would normally work on my local os x machine were not working on my server machine.
Im going to use the ‘zip’ function as my guinea pig. If this function is not installed or enabled in your servers configuration/installation you will see a message like this:

-bash: zip: command not found

This is easy to fix simply by running the following command:

yum install zip

Now if you try the zip command again you will notice its worked or at least should work. You can also if you wish check to see if its installed first by viewing the install list on your server with the following command.

rpm -qa

2009
12.10

This is a nice quick easy way to find duplicate rows in your database based on one field.
So say for instance id like to find all users with duplicate rows in my orders table.

The following query can achieve that.

select userid, count(*) from orders group by userid HAVING count(*) > 1

//this will fetch all the userids with duplicate rows, then you could run a delete query or whatever it is you need to do to get rid of them in a while loop of this.

2009
11.09

Installing wordpress via subversion couldn’t be simpler follow the steps below.

Navigate to the directory on your site in which you wish to create the new installation of wordpress.

//If you need to create the directory run this
$ mkdir blog (or other name of your choice, or you could just install it into the route of your site)

$ cd blog

$ svn co http://core.svn.wordpress.org/trunk/ .

If you already have a copy of wordpress on your server and are looking to just update, run the following code.

$ cd blog

$ svn up

Now you should see all the files flying into your new installation!

2009
11.06

A while back I faced the daunting prospect of learning yet another language, jQuery. At the time I had a little to average knowledge of javascript, I would recommend you to stop reading if you do not have any prior knowledge of javascript or other similar programming languages.

As web sites march forward and us programmers look for ever more elaborate and space saving ways of delightfully cramming our pages with as much content as we can whilst also doing it within a respectable time frame, some life saving javascript libraries have emerged. Elaborate javascript effects have always been possible, but for clients not willing to pay that bit extra for the time it would normally take to build and utilise such features and for programmers not willing to spend the time on some projects, these features have sat very much in the dark corner of the room. Untill now! In relatively simple commands using jQuery you can achieve some actually very complex and cross browser compatible javascript functionality. Which not only looks really cool, but in use is very, very practical!

How to use jQuery

Using jQuery couldn’t be any simpler, head over to the jQuery website and simply download the library. This consists of only one file, which makes everything even more simple!

Upload this file to somewhere on your server and then include it to the head of the page you want to test something on, it should look something like this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>My jQuery Test</title>
<script src="/path/to/your JQUERY filename/on/your/server"></script>
</head><body></body></html>

Im only going to touch on what you can do with jQuery in this topic as I want to focus on the main principles of switching from coding using your basic javascript mark up, to using jQuery. Alot of people refuse to make the move from javascript to jQuery or simply do not see the point. I think in alot of ways thats rather sad, especially when you consider the time it can save you and the simplicity in which otherwise complex javascript can be added to your pages. Yes like many other things it cannot do everything, but whats so great about it is that it can be mixed and matched with regular javascript.

The massive plus point for me using jQuery is its pretty much give or take cross browser compatible. At least the core of it is, this is a hugely important factor when choosing to use jQuery as we all know how javascript is rendered differently in every other browser. So leave the compatibility issues up to someone else and focus on your website, is what I say!

A Simple jQuery Example

Im simply going to show you the very core of any javascript or jQuery application. The selector! In regular javascript were all very used to using the following code to select an element from the dom (or page) by its id.

Add this div to the body of your test page:

<div id="specialdiv"></div>

Then add this code to the head of the page.

<script type="text/javascript">
//normal js selector code
var elem = document.getElementById("specialdiv");
//jquerys selector
var jqelem = $("#specialdiv");
</script>

This is a extremely simple example, but I think it demonstrates nicely what jQuery stands for, “do more and write less!”
One of the most important functions available to you when using jQuery is the following:

$(document).ready(function(){ });
As the name suggests this calls and runs whatever is inside the function when the document is ready ie when the page finishes loading. Try inserting the code below into the head of your test page.

<script type="text/javascript">
$(document).ready(function(){ alert("hi this is my function running"); });
</script>

Another very important thing to learn about jQuery is how it works, differently to normal. For instance say we have a button that we wanted to fire a javascript when it was clicked, jQuery provides ways to bind events to elements. Ie if you click my button I can bind an event to that button on page load and when you click it fire a function of my choice. Where as conventionally before you would have attached the event as an attribute to the element. For example a onclick=”alert(’clicked’)” on an element. Where as I could do the following with jQuery.

First add the button to your page
<input type="button" id="mybutton">

Then add the jQuery below to the head.
<script type="text/javascript">
$(document).ready(function(){
$("#mybutton").click(function(){ alert("You just clicked my button"); });
});
</script>

You will see an alert pop out with the text “You clicked my button”.

Selectors

Selectors are hugely important factor of selecting elements from the dom or the page to a non programmer. jQuery allows you to use numerous ways to select elements. Im only going to touch on a few of the most important ones, have a look at the following examples.

//# selects by the id of the element like so : $("#mybutton");
//. fullstop or a dot select by the class of the element like so : $(".mybutton");
//using a tree or selecting multiples elements by there element type
// selects all divs on the page $("div");
// selects all spans within a div on the page $("div span");

As you can see rather than using regular javascript to do the same its very very quick to code and works really well.

Im going to touch on one last subject in this small tutorial and thats binding of events to the same element. Earlier I showed you how to bind a click function to an element, but what jQuery rather uniquely does is enable you to continuously bind function after function to that element. So in the same function I could lets say add a click function and also a mouseover function with an action in it to change the color of the button. I’ll give a quick example below.

Using your button with the id of “mybutton” that you should have inserted into the body of your test page. Add the following code to the head of your page.
<script type="text/javascript">
//start with our document ready function
$(document).ready(function(){
//select the element by its id
$("#mybutton").click(function(){ alert("you clicked the button"); }).mouseover(function(){ $(this).css("background":"red"); });
});
</script>

In this one line of code I can now tell whether you moused over the button and then perform an action if you do and whether you clicked the button or not. Hopefully your starting the see the scope and speed of this scripting language. Thats it for now, check my jQuery category for other posts about jquery.

2009
08.31

If your anything like me then on a sunday night or all be it any other night when boredom hits  you look for a game or something of the sort to pass some time and hopefully surpass that to “not so much a waste of time” as “genuine fun”. However I use a new generation Imac with all the bells and whistles which is amazing for work purposes, being a web designer its brilliant but not for gaming!

I was thinking about this tonight and realised that it was odd that Apple havent really thought to themselves, okay weve managed to sell our product being the (mac) to a massive world wide audience, but we havent managed to fulfill and persuade all our users that its the best. I mean im not being funny but for most people under the age of 25 the sole purpose of a computer is work and recreation. And I bet in the case of 95% of those people using it for recreation will be the use or the want to use it for gaming.

Now perhaps with this in mind, Apple might have thought to themselves we could make games ourselves for our own computers. I mean microsoft sure as hell do. If they could invest in a large gaming company and make some awesome titles available for the mac they would see there whole image in the face of hardcore internet and game users turned around. I think they HUGELY under-estimate what alot of people that spend alot of time on there computers “out of choice” actually do on them.

I say “out of choice” as these are the users using computers away from work and free of will meaning they want to be there.

Its the one most dissapointing thing about having a mac :( what also strikes me as funny is that id bet my cotton socks that its easier to develop for a mac than a windows system barring the fact its not been as widely done.

Just an idea.

2009
07.31

I always find blogging what i find out is a good idea, we sometimes get conflicts with our subversion repository so here is a list of commands to deal with the conflict in a way that best suits you.

  (e)  edit             - change merged file in an editor
     (df) diff-full        - show all changes made to merged file
     (r)  resolved         - accept merged version of file

     (dc) display-conflict - show all conflicts (ignoring merged version)
     (mc) mine-conflict    - accept my version for all conflicts (same)
     (tc) theirs-conflict  - accept their version for all conflicts (same)

     (mf) mine-full        - accept my version of entire file (even non-conflicts)
     (tf) theirs-full      - accept their version of entire file (same)

     (p)  postpone         - mark the conflict to be resolved later
     (l)  launch           - launch external tool to resolve conflict
     (s)  show all         - show this list
2009
07.06

I flew back last night from cyprus, after being away for 2 weeks on holiday and despite air crafts statistically speaking being the safest modes of transport to date you cannot help but feel that you could be on the last ( however possibly the greatest ride of your life). However it is FACT that in terms of actual deaths by accidents in them, not in terms of the probable chance of death if an accident were to occur (lets get that straight) plane travel is safe. However no matter what the statistics say, we all know in the back of our mind STATISTICALLY speaking if this plane goes down there’ll be a hell of a mess to clean up afterwards.

We all do it, we all get on a plane especially with the 2 recent air crash tragedies and think what if this crashes, what if we never make it, what if terrorists/extremists really want to fly us into a whopping great building.

Especially when you hit those “mild turbulence” patches which feel like you’ve got half the population of the EU trying to pummel you out of the sky with huge potato guns from the ground.

All sorts of odd morbid thoughts run through your head.

Like what would your funeral be like?

Wouldnt it suck never to see my friends, girlfriend, wife, kids or family again.

Wouldnt it be better if we crashed over water not land?

Would I rather crash over england than france?

What would I do if there was a crash would I jump or would I simply, listen and adopt the brace position in the hope that the good for nothing seat belt does anything but aid the death of me?

What would I do if this plane got taken over by terrorists, be a hero? Take one with me? Sit and wimper. Ahhhhh the optionnnnss! Maybe ill just put my ipod back in close my eyes and hope for the best ( which of cause I can now only barely hear due to altitude messing with my ears).

When you land, you just cant help but think to yourself - “hek were going rather fast, what if we dont slow down and plow right into the duty free taking half of Gatwick with us.”

What makes it funny is that we all do it, quietly sitting in our seats with clammy hands and a slightly sweaty back, we all wait for the moment the captain announces, “Welcome to shitty england, the weather is well you guessed……shitty, the current time is - well lets face it who cares. Thank you for flying with We Give You No Leg Room airways, we hope you have a pleasent stay.”

Despite the captains robotic monotone voice, you cant help but feel all of a sudden more secure and happy especially at the fact that you can stand up and stretch those crushed and cramped legs and back of yours out.

Same time next year folks.

2009
06.18

As part of using mac terminal more and more in my own day to day work, I today needed to be able to move all the files and folder from a directory back one directory in the main http folder on my server.

I needed to move all of the files from a file that I unzipped straight to the server, which unfortunately also created another subfolder called PhpEx in my http directory.

So I want to copy of the files and folders from /sites/dunelink.com/http/PhpEx back a directory to /sites/dunelink.com/http

This is how I did it:

mv /sites/dunelink.com/http/PhpEx/* /sites/dunelink.com/http

Notice the * this denotes move everything within the folder but not the actual folder. This will also leave the existing folder untouched exactly where it already is.

If you wish you can then delete the PhpEx folder and all its files with one simple command.

rm -rf /sites/dunelink.com/http/PhpEx

Hope this helps

2009
06.16

As we are now using subversion here at Bytewire I thought it might be useful to just blog a few of the essential commands.

svn status  - Will show you all the files you have changed pending a commit

svn status -u - Will show you all the files that are pending to update on the repository

svn update  - Wil update your files with the files from the repository that have changed.

svn commit -m ‘message’  - Will update the repository and commit your pending files, with a message to describe what you have commited.

svn add ‘filename or foldername’ - cd into the directory of your site and then add ‘http/filename.php’ or http/foldername/ this will update the repository with a new file.

svn rm ‘file or foldername’ - Will remove a file or directory.

svn mv ‘file or filename’ - Used to edit or move a filename so svn mv hello.php hello2.php will move the file hello.php to hello2.php

These are all of the essential commands.

I will be posting more to this list in the coming weeks.