Order MySQL results according to how you want it

Posted on November 16th, 2008 in Database, MySql | No Comments »

Here is a mysql function that is very useful when you want to order results according to how you want it, ofcourse when used with full query result its kinda useless, so i only suggest you to use this on a portion of a query. For example you have the following data:

Id | Name
1 | Tom
2 | Jerry
3 | Donald
4 | Roland
5 | Willie

And you want to order this so that Donald comes first followed by Willie, then randomized the remaining names.

In MySQL you could achieve this by using the function find_in_set. The answer to the query question above is:

SELECT id,IF(FIND_IN_SET(id, '3,5') > 0,FIND_IN_SET(id, '3,5'),3)
AS sort_column FROM table_names ORDER BY sort_column,RAND()

find_in_query would find 3 and 5 (3,5) in the field id and return a chronological order from 1 to n, the reason why we use IF is that for the remainding name the function would return 0 coz it didnt found the other id in the find_in_query arguments, so to have an ascending order and have Donald and Willie goes first, we must assign 3 (thats 3,5 which is 2 numbers plus 1) to the sort_column.

Hope you guys understand this. :)

How to change frequency on CakePHP’s describe query

Posted on November 15th, 2008 in CakePHP, Php, Programming | No Comments »

By default if you set the debug to 0 in your core.php the frequency of executing a describe query is 999 days, but if your developing your application the duration is 10 sections, meaning every 10 sections it would ask your database to describe the tables that you need in your controller. To change the 10 sections to a higher value you could do this.

open configure.php in cake/libs/
locate the function __loadBootstrap
inside this function locate the following
if (Configure::read() >= 1) {
$duration = '+10 seconds';
} else {
$duration = '+999 days';
}

Change the ‘+10 seconds’ according to your requirement, say you want to describe a query every 1 minute, you would change it to
if (Configure::read() >= 1) {
$duration = '+1 minute';
} else {
$duration = '+999 days';
}

Debian OS in Android

Posted on November 9th, 2008 in android | No Comments »

Last week or so, it was Gameboy emulator on andriod, now its a Debian, a linux operating system. Jay Freeman has written a post on how to install it on your gphone http://www.saurik.com/id/10. And btw i had saw a g1 here on Hong Kong for 570USD, and i still have no plans to buy it since its too expensive.

Make iso from folders, files in Ubuntu

Posted on November 8th, 2008 in Ubuntu | No Comments »

Making iso in ubuntu is fairy easily task to do, here is how i did it.

* Open brasero
* Choose Data project
* Find your files and just press the Add (plus button at the top left corner)
note: you cannot drag and drop
* Then select Burn

If you insert a cd/dvd it will as you for either burning it to your cd/dvd or to an Image File, choose the Image File
If you didn’t insert a cd/dvd, then there is only one option and that is the Image File

* Click burn and it would be at your home folder (Alt+F2 then nautilus)

Then you can save or distribute your iso files.

Limit your internet connection in Windows XP

Posted on November 7th, 2008 in Microsoft, Technology | 1 Comment »

I barely talk about windows stuffs but this time i have to share to you a wonderful application for developing web sites or flash or silverlight site. The application is call netlimiter, its not a free software so you have to buy the real thing or try it out for a few days. The cool thing about it is that it can limit individual application, this is handy especially in flash where you have to see the loading/progress bar if your loading an external file. Its good to prank your friends within the trial too, hahaha. But anyway go ahead and try it, its really a good software for web developers.

If anyone knows any freeware that do work, please comment below thanks.

Get Total count from datastore in app engine

Posted on November 3rd, 2008 in App Engine, Google, Programming | No Comments »

Before you continue to read the post, i have to warn you that this is not the best way of getting the total count and there would be a possible inaccuracy and may not be scalable (http://groups.google.com/group/google-appengine/browse_thread/thread/3abb2868ab5fc304?pli=1), please use shared counter as what david suggested.

Here is a way on how to get the total count from a datastore in app engine, this uses memcache to cache the count for one minute (you can change this by passing an argument), i’m not sure but this seems to be better coz it doesn’t need you to do some writing on datastore.

from google.appengine.api import memcache
def get_total_count(self,model,cacheTime = 60,maxNumber = 10):
cacheName = model.kind() + “_totalCount”
total = memcache.get(cacheName)
if total == None:
index = 0
currentTotal = total = len(model.all().fetch(maxNumber,0 ))
while currentTotal == maxNumber:
index += 1
currentTotal = len(model.all().fetch(maxNumber,maxNumber * index))
total += currentTotal
memcache.add(cacheName, total, cacheTime)
return total

To call it on your get function:
self.response.out.write(str(get_total_count(self,<your model>))
or
self.response.out.write(str(get_total_count(self,<your model>,3600,1000))

* Remember that the maxNumber argument can only support upto 1000 and i encourage you to set it to 1000 if your datastore is big

Explanation to the codes:
cacheName = model.kind() + “_totalCount”
Create a variable and name it as our model’s name with an underscore totalCount (you can change this one)

total = memcache.get(cacheName)
if total == None:
index = 0

Change if there is already a cached value, if not set a index variable to 0 and ….

currentTotal = total = len(model.all().fetch(maxNumber,0 ))
Set the currentTotal and total variable to the len of a fetched row limit by maxNumber starting with the row 0

while currentTotal == maxNumber:
index += 1
currentTotal = len(model.all().fetch(maxNumber,maxNumber * index))
total += currentTotal

Loop until the currentTotal is not equal to the maxNumber and add the currentTotal to the total variable and add one to the index. Why do a while loop on currentTotal == maxNumber? Since we set a upper limit on the maximum number of rows we could fetch, it logically means that if we loop with the upper limit it would come to a point where it would never be equal to that limit.

memcache.add(cacheName, total, cacheTime)
Cache our total

Hope i’m right and it helps :)

Installing phpmyadmin in ubuntu

Posted on November 2nd, 2008 in Ubuntu | No Comments »

Here is a simple way of installing phpmyadmin on ubuntu

sudo apt-get install phpmyadmin
cd /var/www
sudo ln -s /usr/share/phpmyadmin/ phpmyadmin