Thursday, April 8, 2010

[.Net] execute directory

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
 
Application.ExecutablePath  (windows form)

System.Reflection.Assembly.GetEntryAssembly();
Assembly.GetExecutingAssembly().CodeBase

Appliaction.StartupPath property

Wednesday, March 24, 2010

[PHP] 設定script執行時間限制

set_time_limit(0);

Tuesday, March 23, 2010

[miranda] google talk

google talk實際用的protocol是Jabber

http://miranda.wikia.com/wiki/Google_Talk

http://www.google.com/talk/otherclients.html


Gmail Multiple Notifier (Unicode) 0.4.0.10
http://addons.miranda-im.org/details.php?action=viewfile&id=3677

[AJAX] GET cache problem

避免cache的作法 (利用timestamp傳不同get參數)

 
在url後面加個 '?q=' +new Date().getTime()
 
 
    jQuery(".del").click( function() {
        delPicId = jQuery(this).attr("id").charAt(3);
        jQuery.getJSON("blah/"+delPicId +"?q="+new Date().getTime(),
            function(data){
                jQuery("#pic"+delPicId).attr("src", 'webroot?>img/blah.jpg');
            }
        );
    });

Monday, March 22, 2010

[CakePHP] validation

寫在model的 $validate array中

驗證方式
 1) 自己寫function
 2) 自己寫regex  (cake使用preg_match)
 3) 使用系統的rule

    var $validate = array(
        'nickname'=> array('rule'=>'checkNickname', 'required'=>false, 'message'=>'長度超過限制'),
        'gander'=> array('rule'=>'/^1|0$/', 'required'=>false, 'on'=>'update'),
        'height'=> array('rule'=>'numeric', 'required'=>false, 'allowEmpty'=>false, 'on'=>'update'),
);

    function checkNickname($check) {
        $value = array_values($check);
        $value = $value[0];
        $value = iconv('UTF-8', 'BIG5', $value);
        if ($value && strlen($value)<=16)
            return true;
        else
            return false;
    }

[MySQL]optimization

EXPLAIN (瞭解MySQL如何處理select)
   Ex: EXPLAIN select * from users.users

GROUP BY xxx,預設會對xxx欄位做sort,如果不希望sort要加ORDER BY NULL

Table欄位的順序也會影響到query的效率(primary key, unique index, index, normal columns)

[js] swap image

/*
    img jQuery object of img tag element
    new img suffix
    1/0 suffix or not
*/
function imgSwap(img, suffix, state) {
    //  ext/. -3 -1
    var img_src = img.attr("src");
    var suffix_pos = img_src.length -4 -suffix.length;
    var img_src_suffix = img_src.substr(suffix_pos, suffix.length);
    var img_src_ext = img_src.substr(img_src.length-4);

    if (state == 1 && img_src_suffix != suffix)
    {
        //prefix + suffix + ext
        img.attr("src", img_src.substr(0, img_src.length-4) + suffix + img_src_ext );
    }
    else if (state ==0 && img_src_suffix == suffix)
    {
        //prefix + ext
        img.attr("src", img_src.substr(0, suffix_pos) + img_src_ext );
    }
}
function a_blur() {
    jQuery("a").focus( function() { this.blur(); return false; } );
}


使用,1是over,0是mouse out
    jQuery(".swap").hover(
        function(){ imgSwap(jQuery(this), "-over", 1) },
        function(){ imgSwap(jQuery(this), "-over", 0) }
    );

[CakePHP] use model in component or model

$a = ClassRegistry::init('a');
$options['fields'] = array('a.eventname', 'a.event_pic');
$options['joins'] = array(
    array('table' => 'soul_match_event_join',
        'alias' => 'b',
        'type' => 'INNER',
        'conditions' => array( 'a.id = b.event_id'),
    )
);

$options['conditions'] = array( 'b.uid' => $uid);
$options['order'] = array( 'a.id DESC');

$result2 = $a->find('first', $options);

if ($result2){
    $res[$this->name]['event_name'] = $result2['a']['eventname'];
    $res[$this->name]['event_pic'] = $result2['a']['event_pic'];
}

[vim] .ctp syntax highlight

root@h7dev06:~$ cat ~/.vim/ftdetect/ctp.vim
au BufRead,BufNewFile *.ctp             set filetype=php

Friday, March 19, 2010

[vim] .vimrc

set shiftwidth=4
set tabstop=4
set t_Co=8
set t_Sf=^[[1;3%p1%dm
set t_Sb=^[[1;4%p1%dm
" set autoindent
set fileencoding=utf-8
highlight Comment ctermbg=4 ctermfg=6
syntax on

set ruler
set hlsearch
set showcmd
set backup
set patchmode=.orig
" set nu
" 讓"upper-case"被當成word
set iskeyword+=-
set showmatch
" set softtabstop=4
set expandtab

[SVN] command line

SVNBook
http://svnbook.red-bean.com/nightly/en/index.html

不 使用svn command line的請略過

svn st 檢查local檔案修改狀態

changelist (cl): Associate (or dissociate) changelist CLNAME with the named files.
svn cl
 
ex:
改東西時先加入list
svn cl pay xxx.php
要commit時
svn ci --cl pay

property
svn:ignore  svn會略過的東西

svn ps svn:ignore
 
 
    copy目錄時要小心
 
原目錄的/.svn 也會被一起copy
commit時,會commit進原目錄
要先砍掉目錄內的.svn,再重加進svn
 
 
filelist可以先處理過會比較好
sort -u svn_xxx.txt

[Mysql] db export, import

Dump db to your localhost (windows)
                notice : mysql tool navicat is forbidden because using this tool is prone to delete data by accident. ( This stupid thing happened indeed )
   
    1. Download MySql GUI ( http://dev.mysql.com/downloads/gui-tools/5.0.html )
    2. Open MySql Migration
                                                        ....
Dump MySQL db ( by MySQLdump)
使用--skip-create-options避免使用MySQL table specific option,避開新舊版本相容性
--quick 抓一個row就存一次,避免memory不夠裝滿整個table
--skip-lock-tables 不lock table

倒整個db
mysqldump --skip-create-options --quick --skip-lock-tables -u php -p -C -h 10.15.2.2 -B users soulmatch vhost14168 portrait gdaa arc smartmatrix adsmart > all_db.sql
我用上面的方法,某些table的primary key會沒有autoincrement,會出錯(不是全部,只有部份會沒有autoincrement)
可用手動在MySQL GUI query browser中複製出create table statement,再貼成一個檔案
只倒table data(一次一個db比較ok, 大小不會太大)
mysqldump -u 帳號 -p密碼 -h 10.15.2.21  --skip-opt --no-create-db --no-create-info --disable-keys --extended-insert --set-charset --skip-add-locks --skip-quick --databases users > d:\data_users.sql
    Restore dumped db
mysql -u root -p < all_db.sql
mysql -u root -p dbname < table.sql

SQL query
有使用到中文時,要設定使用utf-8
       mysql --default-character-set=utf8
PS. MySQL query browser 1.2.17不支援中文(或utf-8)

[CakePHP] 元件重複利用

靜態頁面


echo $this->renderElement('blah');


動態內容頁面

requestAction

requestAction(string $url, array $options)
You can use requestAction() to retrieve a fully rendered view by passing 'return' in the options: requestAction($url, array('return'));


array based cake style url

$this->requestAction(array('controller' => 'articles', 'action' => 'featured'), array('return'));

This allows the requestAction call to bypass the usage of Router::url which can increase performance.



測試結果只有 $options有給 return才有效,沒給return都是no return (another cake bug??)

[CakePHP] constant & variable

本機目錄
WWW     (...user2/app/webroot)
IMAGES  (...user2/app/webroot/img)
...etc

給url使用
$this->webroot  (/users2/app/webroot/)

[網站] js, css參考網址

js
http://krook.org/jsdom/

css
http://meiert.com/en/indices/css-properties/

[PHP] debug

倒出變數的function

var_dump($var)
var_export($var)   字串為php code
print_r($var)

[jQuery] Accordion

http://jqueryui.com/demos/accordion/

http://bassistance.de/jquery-plugins/jquery-plugin-accordion/





    jQuery("#menu_block").accordion({ event: 'mouseover', animated: false, autoHeight: false });

a包主選單,div包選取主選單時展開的內容

[jQuery] disable rightclick

  1. $(document).ready(function(){  
  2.     $(document).bind("contextmenu",function(e){  
  3.         return false;  
  4.     });  
  5. }); 
  • W3C has added the method preventDefault() to the event. If you call it the default action is prevented.
  • Microsoft has added the property returnValue to the event. If you set it to false the default action is prevented.

[jQuery] context

jQuery selector在找東西的時候會限制在context裡面找,不會找整個dom,better performance

var right_block = jQuery('#right_block');

jQuery("#close_fill", right_block).click( function() {
    jQuery("#fill_info", right_block).css('display', '
});

[AJAX] file upload

http://valums.com/ajax-upload/