Welcome to Artan's blog! I love raising fishes, programming, travelling and photography, so over the time I will post articles related to those topics and share what I know with everyone... And I really appreciate your feedback!
Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts
Tuesday, June 19, 2012
jQuery Multiple Draggable and Droppable Source Code
Previous Post:
http://williamartan.blogspot.tw/2011/07/jquery-multiple-draggable-and-droppable.html
Demo for jQuery Multiple Draggable and Droppable is here:
http://yy.jangmt.com/willart/testPackaging.php
The source code is here:
https://docs.google.com/open?id=0B52N9Zm_N1hFaGdEdWhwMktZc2s
After clicking the above link, then you can see all of the codes and you can download it as following:
Wednesday, November 23, 2011
How to Convert MySQL Database Character Set from default latin1_swedish_ci to UTF8
Currently I am working with Joomla CMS (Content Management System) which is hosted in a foreign web host and its MySQL database is using latin1_swedish_ci character set. I need to convert it to UTF8 since the content of the website that I'm dealing with is in English, Chinese Traditional and Chinese Simplified.
After searching in the web, I found a php code that can do the thing I want. The solution is pretty easy (at least after I found this code), just copy and paste the code below in a php file, let's call it a converter.php file, and then on the second to fourth line of the codes below change the parameters so that it will fit to your database settings. Here's the code:
Source: http://blog.mashida.info/?p=1353 and http://bingu.net/472/latin1_swedish_ci-to-utf8_general_ci/
Save the converter.php file and then upload to your webhost www folder and execute that php by requesting that file using your browser. E.g. If your webhost domain is www.mywebhost.com, then you can request the file with www.mywebhost.com/converter.php. And boom, everything is done, your database will be converted from latin1_swedish_ci character set to UTF8. And don't forget to delete your converter.php file on your webhost since it will not be used anymore.
After searching in the web, I found a php code that can do the thing I want. The solution is pretty easy (at least after I found this code), just copy and paste the code below in a php file, let's call it a converter.php file, and then on the second to fourth line of the codes below change the parameters so that it will fit to your database settings. Here's the code:
<?php
define('DB_NAME', 'putyourdbnamehere'); // Put your database name here
define('DB_USER', 'usernamehere'); // MySQL Username
define('DB_PASSWORD', 'yourpasswordhere'); // Password
define('DB_HOST', 'localhost'); // Mostly just use the localhost
function UTF8_DB_Converter_DoIt() {
$tables = array();
$tables_with_fields = array();
// Since we cannot use the WordPress Database Abstraction Class (wp-db.php),
// we have to make an a stand-alone/direct connection to the database.
$link_id = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Error establishing a database connection');
mysql_select_db(DB_NAME, $link_id);
// Gathering information about tables and all the text/string fields that can be affected
// during the conversion to UTF-8.
$resource = mysql_query("SHOW TABLES", $link_id);
while ( $result = mysql_fetch_row($resource) )
$tables[] = $result[0];
if ( !empty($tables) ) {
foreach ( (array) $tables as $table ) {
$resource = mysql_query("EXPLAIN $table", $link_id);
while ( $result = mysql_fetch_assoc($resource) ) {
if ( preg_match('/(char)|(text)|(enum)|(set)/', $result['Type']) )
$tables_with_fields[$table][$result['Field']] = $result['Type'] . " " . ( "YES" == $result['Null'] ? "" : "NOT " ) . "NULL " . ( !is_null($result['Default']) ? "DEFAULT '". $result['Default'] ."'" : "" );
}
}
// Change all text/string fields of the tables to their corresponding binary text/string representations.
foreach ( (array) $tables as $table )
mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET binary", $link_id);
// Change database and tables to UTF-8 Character set.
mysql_query("ALTER DATABASE " . DB_NAME . " CHARACTER SET utf8", $link_id);
foreach ( (array) $tables as $table )
mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET utf8", $link_id);
// Return all binary text/string fields previously changed to their original representations.
foreach ( (array) $tables_with_fields as $table => $fields ) {
foreach ( (array) $fields as $field_type => $field_options ) {
mysql_query("ALTER TABLE $table MODIFY $field_type $field_options", $link_id);
}
}
// Optimize tables and finally close the mysql link.
foreach ( (array) $tables as $table )
mysql_query("OPTIMIZE TABLE $table", $link_id);
mysql_close($link_id);
} else {
die('<strong>There are no tables?</strong>');
}
return true;
}
UTF8_DB_Converter_DoIt();
?>Save the converter.php file and then upload to your webhost www folder and execute that php by requesting that file using your browser. E.g. If your webhost domain is www.mywebhost.com, then you can request the file with www.mywebhost.com/converter.php. And boom, everything is done, your database will be converted from latin1_swedish_ci character set to UTF8. And don't forget to delete your converter.php file on your webhost since it will not be used anymore.
Friday, November 11, 2011
Java Programming Thread
什麼是 Thread?
Thread 翻成中文叫做執行緒。一個程式一般來說就是一個 Process, 會執行一個工作就是從 main( ) 或主程式(事實上是一個主執行緒)從第一行到最後一行,而執行緒可以讓程式可以同時執行好幾個工作。更清楚的解釋: http://en.wikipedia.org/wiki/Thread_(computer_science)
| A multithreaded process with two threads executing in time clearly showing that the threads execute separately and execute mutually exclusively in time. [Source: http://en.wikipedia.org/wiki/File:Multithreaded_process.svg] |
什麼時候要用到Thread?
當我們有很多動西要同時一起跑的時候就要用到Thread, 比如說你的程式要刷新畫面,同時也要下載東西,播放音樂,監聽事件(Event Listener 同時要有好幾個 while loop 在等待).
到底要 extends Thread 還是 implements Runnable (extends Thread VS implements Runnable)?
The difference:
1)If you want to extend the Thread class then it will make your class unable to extend other classes as java is having single inheritance feature whereas If you implement runnable interface, you can gain better object-oriented design and consistency and also avoid the single inheritance problems.
2)Extending the thread will give you simple code structure in comparison to Runnable Interface.
3)Using Runnable Interface, you can run the class several times whereas Thread have the start() method that can be called only once.
Thread .setDaemon(true) 的方法(method)
如果設成Daemon則代表這個thread在背後執行
從Main函式開始的是一個非Daemon執行緒,如果您希望某個執行緒在非Daemon執行緒都結束後也跟著終止,那麼您要將它設定為Daemon執行 緒,下面這個程式是個簡單的示範:
- DaemonTest.java
package onlyfun.caterpillar;
public class DaemonTest {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
public void run() {
while(true) {
System.out.print("T");
}
}
});
thread.setDaemon(true);
thread.start();
}
}
Thursday, August 4, 2011
MySQL Query - count age by range
Now I have a condition to query on my MySQL database like following: I have a table, let's called it user_table and in the table I have columns including id, name, email, phone, birthday as the following:
What I want to do is to make a query that will group and count the user by age range. The result that I want, is like following:
Before I started to look for an answer in the internet, I was thinking to achieve this result, may be I could use php and did the looping of MySQL query which contains SELECT, COUNT and GROUP BY on different range of age, but this way is not a direct or a pretty solution. So, I tried to search on the internet of how to do it with pure MySQL query. And after searching on the internet I found out the solution as the following:
From this solution we see that actually it's not necessary to use COUNT AND GROUP BY to count on how many records that fit to certain range or condition. And in fact this solution turns the vertical data into horizontal data, which can't be achieved through COUNT and GROUP BY (as I know, correct me if I'm wrong!).
As a novice, this solution clarified my concept and ideas in MySQL, such as:
1. In FROM we can put another sub-query and what the sub-query return is actually a table that gonna be used to do another query by the main query. Let me give you an instance of what I mean! Usually we do the query like this: SELECT * FROM user_table [WHERE condition], but in another situation we need to select something from a query result and give it as an input or a source table to another query, particularly the main query as you can see in the above solution: SELECT SUM(IF(age < 20,1,0)) as 'Under 20', ... FROM (SELECT YEAR(CURDATE())-YEAR(birthday)) AS age FROM user_table) as derived
2. Another thing is that we can use SUM without having to use GROUP BY, I used to think that whenever I need to use SUM, I should also use GROUP BY.
3. In MySQL we can make a conditional test that return a value with this syntax: IF(condition,value to return when condition is true, value to return when condition is false), e.g. If age is smaller than 20, then return 1, else return 0. The corresponding MySQL is like following: IF(age > 20 , 1 , 0)
4. To get current year in MySQL use: YEAR(CURDATE());
| id | name | tel | birthday | |
|---|---|---|---|---|
| 1 | name1 | email1 | 2144 | 1976-01-01 |
| 2 | name2 | email2 | 5436457 | NULL |
| 3 | name3 | email3 | 0976564765 | 1987-01-01 |
| 4 | name4 | email4 | 1243412 | NULL |
| 5 | name5 | email5 | 87432145 | NULL |
| 6 | name6 | email6 | 5777 | NULL |
| 7 | name7 | email7 | 9875847567 | NULL |
| 8 | name8 | email8 | 9854875463 | 1976-01-01 |
| 9 | name9 | email9 | 27557666 | 1966-01-01 |
| 10 | name10 | email10 | 8765765765 | 1976-01-01 |
| 11 | name11 | email11 | 0912564421 | 1984-01-01 |
| 12 | name12 | email12 | 0955711114 | 1986-01-01 |
| 13 | name13 | email13 | 123 | 1991-01-01 |
| 14 | name14 | email14 | 0955711115 | NULL |
What I want to do is to make a query that will group and count the user by age range. The result that I want, is like following:
| Under 20 | 20 - 29 | 30 - 39 | 40 - 49 | 50 - 59 | 60 - 69 | 70 - 79 | Over 80 | Not Filled In |
|---|---|---|---|---|---|---|---|---|
| 0 | 4 | 3 | 1 | 0 | 0 | 0 | 0 | 6 |
Before I started to look for an answer in the internet, I was thinking to achieve this result, may be I could use php and did the looping of MySQL query which contains SELECT, COUNT and GROUP BY on different range of age, but this way is not a direct or a pretty solution. So, I tried to search on the internet of how to do it with pure MySQL query. And after searching on the internet I found out the solution as the following:
SELECT
SUM(IF(age < 20,1,0)) as 'Under 20',
SUM(IF(age BETWEEN 20 and 29,1,0)) as '20 - 29',
SUM(IF(age BETWEEN 30 and 39,1,0)) as '30 - 39',
SUM(IF(age BETWEEN 40 and 49,1,0)) as '40 - 49',
SUM(IF(age BETWEEN 50 and 59,1,0)) as '50 - 59',
SUM(IF(age BETWEEN 60 and 69,1,0)) as '60 - 69',
SUM(IF(age BETWEEN 70 and 79,1,0)) as '70 - 79',
SUM(IF(age >=80, 1, 0)) as 'Over 80',
SUM(IF(age IS NULL, 1, 0)) as 'Not Filled In (NULL)'
FROM (SELECT YEAR(CURDATE())-YEAR(birthday)) AS age FROM user_table) as derived
SUM(IF(age < 20,1,0)) as 'Under 20',
SUM(IF(age BETWEEN 20 and 29,1,0)) as '20 - 29',
SUM(IF(age BETWEEN 30 and 39,1,0)) as '30 - 39',
SUM(IF(age BETWEEN 40 and 49,1,0)) as '40 - 49',
SUM(IF(age BETWEEN 50 and 59,1,0)) as '50 - 59',
SUM(IF(age BETWEEN 60 and 69,1,0)) as '60 - 69',
SUM(IF(age BETWEEN 70 and 79,1,0)) as '70 - 79',
SUM(IF(age >=80, 1, 0)) as 'Over 80',
SUM(IF(age IS NULL, 1, 0)) as 'Not Filled In (NULL)'
FROM (SELECT YEAR(CURDATE())-YEAR(birthday)) AS age FROM user_table) as derived
From this solution we see that actually it's not necessary to use COUNT AND GROUP BY to count on how many records that fit to certain range or condition. And in fact this solution turns the vertical data into horizontal data, which can't be achieved through COUNT and GROUP BY (as I know, correct me if I'm wrong!).
As a novice, this solution clarified my concept and ideas in MySQL, such as:
1. In FROM we can put another sub-query and what the sub-query return is actually a table that gonna be used to do another query by the main query. Let me give you an instance of what I mean! Usually we do the query like this: SELECT * FROM user_table [WHERE condition], but in another situation we need to select something from a query result and give it as an input or a source table to another query, particularly the main query as you can see in the above solution: SELECT SUM(IF(age < 20,1,0)) as 'Under 20', ... FROM (SELECT YEAR(CURDATE())-YEAR(birthday)) AS age FROM user_table) as derived
2. Another thing is that we can use SUM without having to use GROUP BY, I used to think that whenever I need to use SUM, I should also use GROUP BY.
3. In MySQL we can make a conditional test that return a value with this syntax: IF(condition,value to return when condition is true, value to return when condition is false), e.g. If age is smaller than 20, then return 1, else return 0. The corresponding MySQL is like following: IF(age > 20 , 1 , 0)
4. To get current year in MySQL use: YEAR(CURDATE());
Sunday, July 17, 2011
jQuery Multiple Draggable and Droppable
For people who is starting to use jQuery like me, will find out that jQuery is really really a good javascript library and it is not just a library but also a useful UI (User Interface). You might have seen the full documentation of how to use its UI as well, but did you find something that is missing, something that you might need to achieve the functionality that you want? For me, I found out that there's no documentation nor example of how to achieve multiple drag and drop in the jQuery UI official website, I need this functionality in order to select multiple items, drag and drop them to different droppable area. Specifically, what I need is a better user interface to let a user to "pack" items from an item list to some boxes in an easy and convenient way. The functionality that I want is depicted as the following figure:
Demo: http://yy.jangmt.com/willart/testPackaging.php
People that have the same need as me, which is to select multiple items and then drag and drop them together to the droppable area might have tried to google around to find how to do it. So far, I found this: http://thechriswalker.net/select-drag/, but I haven't seen through its code; there are also some short answers in http://stackoverflow.com of how to do multiple draggable and multiple droppable.
In the next post, I'm gonna explain more about how I made the multiple draggable and droppable, so please keep on following my blog!
![]() |
| Figure 1. jQuery multiple draggable and droppable moving three draggable items from "Item Area" to "Box 1" |
People that have the same need as me, which is to select multiple items and then drag and drop them together to the droppable area might have tried to google around to find how to do it. So far, I found this: http://thechriswalker.net/select-drag/, but I haven't seen through its code; there are also some short answers in http://stackoverflow.com of how to do multiple draggable and multiple droppable.
In the next post, I'm gonna explain more about how I made the multiple draggable and droppable, so please keep on following my blog!
Subscribe to:
Posts (Atom)

