2012年9月19日 星期三

PHP 取得DB欄位資訊

//$table:表格名稱 return欄位名稱陣列
function getFieldName($table)
{
 $result = mysql_query("SELECT * FROM $table");

 for($i = 0; $i < mysql_num_fields($result); $i++)
 {
  $meta = mysql_fetch_field($result);

  $fieldNameArray[] = $meta->name;

  //name:欄位名稱 
  //table:欄位所屬的資料表名稱
  //max_length:欄位的最大長度
  //not_null:欄位是否不可是null 是1否0
  //primary_key:欄位是否為主鍵 是1否0
  //numeric:欄位是否為數值 是1否0
  //type:欄位的資料型態
 }
  
 return $fieldNameArray;
}

PHP 取得DB資料使用JSON回給client

PHP 取得DB資料使用JSON回給client
並且讓數字不加引號
$sth = mysql_query("SELECT ...");

$rows = array();

while($r = mysql_fetch_assoc($sth))
{
    while($elm=each($r))
    {
        if(is_numeric($r[$elm["key"]])){
                    $r[$elm["key"]]=intval($r[$elm["key"]]);
        }
    }
    $rows[] = $r;
}  

2012年9月3日 星期一

PHP 取得隨機數

//取隨機數
//第一步:初始化種子 
$seedarray = microtime(); 
$seedstr = split(" ",$seedarray,5); 
$seed = $seedstr[0]*10000;

//第二步:使用種子初始化隨機數發生器 
srand($seed);

//第三步:生成指定範圍內的隨機數 
$random = rand(1,10);

2012年8月14日 星期二

PHP 確保每次進入頁面都會reload的小技巧

經常有頁面流程A->B->A的情況,最後A的頁面不會更新,提供一個從國外論壇找到的小技巧:
在A頁面的開頭加入PHP程式碼

// 確保每次網頁進來都會reload
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

2012年8月7日 星期二

2012年7月31日 星期二

PHP 訪客計數器(不使用exec與DB)

因客戶需求所以google一下訪客計數器,結果找到Wilson Peng大大的code。
//---------------------------
// 訪客計數器函數 MyCounter()
// Author: Wilson Peng
//        Copyright (C) 1999
//---------------------------
function MyCounter() {
  $counterFile="/tmp".$GLOBALS["PHP_SELF"];
  if (!file_exists($counterFile)) {
    if (!file_exists(dirname($counterFile))) {
      mkdir(dirname($counterFile), 0700);
    }
    exec("echo 0 > $counterFile");
  }
  $fp = fopen($counterFile,"rw");
  $num = fgets($fp,5);
  $num += 1;
  print "$num";
  echo $counterFile;
  exec("rm -rf $counterFile");
  exec("echo $num > $counterFile");
}

不過遇到兩個問題,一是exec權限不足,一是$GLOBALS["PHP_SELF"]無法取得路徑。
一個一個處理吧:
exec是用來新增與刪除檔案,所以用fwrite跟unlink取代
  exec("rm -rf $counterFile");
  exec("echo $num > $counterFile");
改成
  //刪舊檔
  unlink($counterFile);
  //開新檔存新值
  $fh = fopen($counterFile, "w");
  fwrite($fh, $num);
  fclose($fh);


$GLOBALS["PHP_SELF"]是為了針對不同頁面產生不同檔名的counter文件,
這邊用getenv('REQUEST_URI')配合str_replace()來取得目前網頁的檔名,並加上副檔名。
所以將
$counterFile="/tmp".$GLOBALS["PHP_SELF"];
改成
  //用網頁檔名加.count當計數文件檔名
  $counterFile = "./counter/".str_replace("/harland/NewZealand/", "", getenv('REQUEST_URI')).".counter";


最後整個function調整後的成果,不使用exec與DB的訪客計數器如下:
//---------------------------
// 訪客計數器函數 MyCounter()
// Author: Wilson Peng
// Revise: Harland Chou (2012)
//        Copyright (C) 1999
//---------------------------
function MyCounter() 
{
  //用網頁檔名加.count當計數文件檔名
  $counterFile = "./counter/".str_replace("/harland/NewZealand/", "", getenv('REQUEST_URI')).".counter";

  if (!file_exists($counterFile)) 
  {

    if (!file_exists(dirname($counterFile))) 
    {

      mkdir(dirname($counterFile), 0700);
    }
    
      //開新檔存新值
    $fh = fopen($counterFile, "w");
    fwrite($fh, "0");
    fclose($fh);

  }
  
  $fp = fopen($counterFile,"rw");
  $num = fgets($fp,5);
  $num += 1;
  print "$num";
  //echo $counterFile;

  //刪舊檔
  unlink($counterFile);       //刪原檔
  //開新檔存新值
  $fh = fopen($counterFile, "w");
  fwrite($fh, $num);
  fclose($fh);

}

PHP 隱藏function Warning

 //檢查檔案時間
 $filetime = filemtime($filename);

此函式會回傳檔案建立時間,但是檔案不存在時(也許尚未被建立)會印出Warning如下
Warning: filemtime() [function.filemtime]: stat failed for ./cache/Route.aspxColumnproviderId1100DataFormatjson.html in

若不想讓使用者看到則可在function名稱前加@來隱藏Warning訊息
 //檢查檔案時間
 $filetime = @filemtime($filename);