2011年11月27日

[PHP] 程式小技巧

[迴圈的使用]

//PHP3 or old
reset($attributes);
while (list($key, $value) = each($attributes)) {
    //do something
}
//PHP4
foreach ($attributes as $key => $value){
   //do something
}



[簡查重覆的資料]
SELECT username,COUNT(*)/*重複出現的次數*/ FROM member GROUP BY username HAVING COUNT(*) > 1 /*列出重複出現一次以上的資料*/

2011年11月24日

[Android] 取得檔案位置

package tw.Goocue;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity {
   @Override 
   public void onCreate(Bundle icicle)
   {
     // TODO Auto-generated method stub 
     super.onCreate(icicle);
        setContentView(R.layout.main);
        
        Button b = (Button)this.findViewById(R.id.b1);
        
        b.setOnClickListener( new OnClickListener(){
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                
                // 建立 "選擇檔案 Action" 的 Intent
                Intent intent = new Intent( Intent.ACTION_GET_CONTENT );
                
                // 過濾檔案格式
                intent.setType( "*/*" );
                
                // 建立 "檔案選擇器" 的 Intent  (第二個參數: 選擇器的標題)
                Intent destIntent = Intent.createChooser( intent, "選擇檔案" );
                
                // 切換到檔案選擇器 (它的處理結果, 會觸發 onActivityResult 事件)
                startActivityForResult( destIntent, 0 );
            }
        });
   }
   
   @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         
         // TODO Auto-generated method stub
         super.onActivityResult(requestCode, resultCode, data);
         
         // 有選擇檔案
         if ( resultCode == RESULT_OK )
         {
             // 取得檔案的 Uri
             Uri uri = data.getData();
             if( uri != null )
             {
               Cursor cursor = this.getContentResolver().query(uri, null, null, null, null);
                  cursor.moveToFirst();
                  
                  for (int i = 0; i < cursor.getColumnCount(); i++) {
                   
                   
                   setTitle( i+"-"+cursor.getString(1));
                   }
                 // 利用 Uri 顯示 ImageView 圖片
                // setTitle( uri.toString() );
             }
             else
             {
                 setTitle("無效的檔案路徑 !!");
             }
         }
         else
         {
             setTitle("取消選擇檔案 !!");
         }
     }

}

[PHP] 清除網頁快取



//變成Function
function nocache_headers() {
@ header('Expires: Thu, 01 Jan 1970 00:00:01 GMT');
@ header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
@ header('Cache-Control: no-cache, must-revalidate, max-age=0');
@ header('Pragma: no-cache');
}
nocache_headers();