I've been thinking on what to answer here. It seems that you haven't understood the query. I could get the result, but I want you to try harder and research more. If you want to help others as well post your complete solution. If it can be improved, I'll help on that.
Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?
How to post data/code on a forum to get the best help: Option 1 / Option 2