//$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; }
2012年9月19日 星期三
PHP 取得DB欄位資訊
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程式碼
在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日 星期二
PHP HTML character encod
直接貼範例
$mystring = mb_convert_encoding($mystring, 'HTML-ENTITIES', 'UTF-8');HTML Character Codes List
2012年7月31日 星期二
PHP 訪客計數器(不使用exec與DB)
因客戶需求所以google一下訪客計數器,結果找到Wilson Peng大大的code。
不過遇到兩個問題,一是exec權限不足,一是$GLOBALS["PHP_SELF"]無法取得路徑。
一個一個處理吧:
exec是用來新增與刪除檔案,所以用fwrite跟unlink取代
$GLOBALS["PHP_SELF"]是為了針對不同頁面產生不同檔名的counter文件,
這邊用getenv('REQUEST_URI')配合str_replace()來取得目前網頁的檔名,並加上副檔名。
所以將
最後整個function調整後的成果,不使用exec與DB的訪客計數器如下:
//--------------------------- // 訪客計數器函數 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);
PHP Delete all files in folder
一次清空目錄下所有檔案
//////////////////////////////////////////////////////////// //$dir要刪除的目錄,$DeleteMe是否連目錄本身也要刪掉 /* $log = '/home/logs/2011'; SureRemoveDir($log , true); // 第二個參數: true 連 2011 目錄也刪除 */ //////////////////////////////////////////////////////////// function SureRemoveDir($dir, $DeleteMe) { if(!$dh = @opendir($dir)) return; while(false !== ($obj = readdir($dh))) { if($obj=='.' || $obj=='..') continue; $temp = $dir.$obj; echo "刪除 ".$temp."
"; $conut++; if(!@unlink($dir.'/'.$obj)) SureRemoveDir($dir.'/'.$obj, true); } echo "已刪除".$conut."個檔案!"; if ($DeleteMe) { closedir($dh); @rmdir($dir); echo "已刪除目錄!"; } }
2012年7月30日 星期一
PHP 去除字串中特殊字元或符號
有時需要對字串作特殊處理,像是在處理檔名時去要去除特殊符號。
提供一簡單的function來方處理。
提供一簡單的function來方處理。
//////////////////////////////////////////////////////////// //$targetString要處理的字串,要移除的符號陣列$symbolArray //使用範例 /* $temp = "adn?cklmc?=kkal;m!c"; $sArray = array("?", "!", "="); replaceSepcialSymbol($temp, $sArray); echo $temp; */ //////////////////////////////////////////////////////////// function replaceSepcialSymbol(&$targetString, $symbolArray) { foreach ($symbolArray as $value) $targetString = str_replace($value, "", $targetString); }
PHP GET URL截斷問題處理
範例
http://api.taipeibus.kasper.com.tw/?url=http://imp.5284.com.tw:3621/TaipeiBusService/Route.aspx?Column=providerId,1100&DataFormat=json
原本期待可以使用$_GET["url"]取得
http://imp.5284.com.tw:3621/TaipeiBusService/Route.aspx?Column=providerId,1100&DataFormat=json
但其實只能得到
http://imp.5284.com.tw:3621/TaipeiBusService/Route.aspx?Column=providerId,1100
在"&"之後會被截斷
可以改用
http://api.taipeibus.kasper.com.tw/?url=http://imp.5284.com.tw:3621/TaipeiBusService/Route.aspx?Column=providerId,1100&DataFormat=json
原本期待可以使用$_GET["url"]取得
http://imp.5284.com.tw:3621/TaipeiBusService/Route.aspx?Column=providerId,1100&DataFormat=json
但其實只能得到
http://imp.5284.com.tw:3621/TaipeiBusService/Route.aspx?Column=providerId,1100
在"&"之後會被截斷
可以改用
$query_url = str_replace("url=", "", $_SERVER['QUERY_STRING'];
2012年7月27日 星期五
PHP echo 中文亂碼處理
用php直接echo "中文"; 在瀏覽器上會看到亂碼,在開頭加入header即可解決。
<?php header("Content-Type:text/html; charset=utf-8"); ?>
2012年7月26日 星期四
PHP cache demo
1.收到要求
2.檢查要求檔案是否過期
3.重新要新的或將cache給client
2.檢查要求檔案是否過期
3.重新要新的或將cache給client
<?php //////////////////////////// //將從http(或其他)讀來的檔案 //使用範例 // http://api.taipeibus.kasper.com.tw/?url=http://tw.yahoo.com/ // http://api.taipeibus.kasper.com.tw/?url=http://imp.5284.com.tw:3621/TaipeiBusService/Route.aspx?DataFormat=json //////////////////////////// function outputHttpResult($url)//您想抓取的網址 { //檔案名稱 $filename = "./cache/".str_replace("http://imp.5284.com.tw:3621/TaipeiBusService/", "", $url).".html"; //檢查檔案時間 $filetime = filemtime($filename); if($filetime == 0) { //無此檔案 要新的 getApiResultAndSave($url); } else { //檔案是否過期 if((mktime() - $filetime) >= 30) { //超過30秒 要新的 getApiResultAndSave($url); } else { //還沒超過30秒 給cache的檔案 readCacheFileAndEcho($filename); } } } //取得http資源存檔並回給client function getApiResultAndSave($url) { //檔案名稱 $filename = "./cache/".str_replace("http://imp.5284.com.tw:3621/TaipeiBusService/", "", $url).".html"; $buffer = file($url); //將網址讀入buffer變數 for($i = 0; $i < sizeof($buffer); $i++) //將每段文字讀出來,以換行為單位,sizeof會傳回共有幾筆 { echo $buffer[$i]; //全部印出來 } // 要寫入的內容 for($i = 0; $i < sizeof($buffer); $i++) //將每段文字讀出來,以換行為單位,sizeof會傳回共有幾筆 { $word .= $buffer[$i]; //全部接起來 } //$word = $buffer; $fh = fopen($filename, "w"); fwrite($fh, $word); fclose($fh); } //讀出舊的檔案並回給client function readCacheFileAndEcho($filename) { $fh = @fopen($filename,"r") or die("READ FILE ERROR!"); if($fh) { while(!feof($fh)) { echo fgets($fh); } } fclose($fh); } ?>
2012年7月24日 星期二
PHP include html demo
this is used to manager website's main menu.
or some repeated html code.
Here is an example.
share_main_menu.html for reusing.
when you went to use...
or some repeated html code.
Here is an example.
share_main_menu.html for reusing.
<a href="admin_news.php">管理最新消息</a> | <a href="admin_case.php">管理成功案例</a> | <a href="admin_contact.php">管理聯絡我們</a> | <a href="admin_quest.php">管理案件</a> | <a href="admin_reserve.php">管理備料</a> | <a href="?logout=true">登出系統</a> <hr size="1" />
when you went to use...
2012年7月23日 星期一
PHP get monthly serial number
PHP版本的自動生成有規則的訂單號(或編號)
生成的格式是:200908010001 前面幾位為當前的日期,後面五位為系統自增長類型的編號原理:
1.獲取當前日期格式化值
2.讀取文件,上次編號的值+1最為當前此次編號的值(記錄以文件的形式存儲,下月會接著這個編號)
注意:網站目錄下需有EveryDaySerialNumber.dat否則會發生錯誤
使用範例(一)
使用範例(二)
生成的格式是:200908010001 前面幾位為當前的日期,後面五位為系統自增長類型的編號原理:
1.獲取當前日期格式化值
2.讀取文件,上次編號的值+1最為當前此次編號的值(記錄以文件的形式存儲,下月會接著這個編號)
<?php class FileEveryDaySerialNumber { private $filename; //檔名 private $separate; //系統分隔符號 private $width; //自動增長部分的個數 public function __construct($width, $filename, $separate) { $this->width = $width; $this->filename = $filename; $this->separate = $separate; } public function getOrUpdateNumber($current, $start) { $record = IOUtil::read_content($this->filename); $arr = explode($this->separate, $record); if($current == $arr[0]) { //如果是同一天,則繼續增長 $arr[1]++; IOUtil::write_content("$arr[0],$arr[1]", $this->filename); //將新值存入文件中 return "$arr[0]".str_pad($arr[1],$this->width,0,STR_PAD_LEFT); } else { //如果兩個日期不一樣則重新從起始值開始 $arr[0] = $current; $arr[1] = $start; IOUtil::write_content("$arr[0],$arr[1]", $this->filename); //將新值存入文件中 return "$arr[0]".str_pad($arr[1],$this->width,0,STR_PAD_LEFT); } } } class IOUtil { public static function read_content($filename) { $handle = fopen($filename,"r"); $content = fread($handle,filesize($filename)); return $content; } public static function write_content($content, $filename) { $handle = fopen($filename,"w"); fseek($handle,0); fwrite($handle, $content); return $content; } } ?>參數含義分別是日期後自增長數的位數, 存儲的文件名稱, 日期與自增長數的分割數
注意:網站目錄下需有EveryDaySerialNumber.dat否則會發生錯誤
使用範例(一)
$obj = new FileEveryDaySerialNumber(4,"EveryDaySerialNumber.dat",","); $current_date = date("Ymd"); echo $obj->getOrUpdateNumber($current_date,1);輸出結果: 201207230001
使用範例(二)
$obj = new FileEveryDaySerialNumber(3,"EveryDaySerialNumber.dat",","); $current_date = date("ym"); echo $obj->getOrUpdateNumber($current_date,1);輸出結果: 1207001
2012年7月20日 星期五
PHP get system date
Demo html
out put is 2012-07-20 15:16:20
All formate key word list :
Y - 幾年,以4位數表示,例如:" 1999"
y - 幾年,以2位數表示,例如:"99"
a - "am" 或 "pm"
A - "AM" 或 "PM"
B - 網際網路時間樣本
d - 幾日,例如:" 01" 到 " 31"
D - 幾日,以3個英文字表示,例如:" Fri "
F - 幾月,以英文全名表示,例如:" January "
g - 小時,12小時制不足2位數不補0,例如:" 1" 到 " 12 "
G - 小時,24小時制不足2位數不補0,例如:" 0 " 到 " 23 "
h - 小時,12小時制,例如:" 01" 到 " 12 "
H - 小時,24小時制,例如:" 00 " 到 " 23 "
i - 幾分,例如:" 00 " 到 " 59 "
I (大寫的 i) - "1" if Daylight Savings Time, "0" otherwise.
j - 幾日,不足2位數不補0,例如:" 1" 到 " 31"
l (小寫的 'L') - 幾日,以英文全名表示,例如:"Friday"
L - 布林值,判斷是否為閏年,例如:" 0" 或 " 1"
m - 幾月,例如:" 01" 到 " 12"
M - 幾月,以3個英文字表示,例如:"Jan"
n - 幾月,不足2位數不補0,例如:" 1" 到 "12"
s - 幾秒,例如:" 01" 到 " 59"
S - 以英文後2個字表示,例如:"th","nd"
t - 當月的天數,例如:" 28" 到 " 31"
T - 這個機器的時間區域設定,例如 :"MDT"
U - 總秒數
w - 以數字表示星期幾,例如:" 0" 到 " 6"
z - 一年中的第幾天,例如:" 0" 到 " 365"
Z - 在短時間內時間區域補償(timezone offset) ,例如:"-43200" to "43200"
$ts = mktime(); echo date("Y-m-d H:i:s",$ts);
out put is 2012-07-20 15:16:20
All formate key word list :
Y - 幾年,以4位數表示,例如:" 1999"
y - 幾年,以2位數表示,例如:"99"
a - "am" 或 "pm"
A - "AM" 或 "PM"
B - 網際網路時間樣本
d - 幾日,例如:" 01" 到 " 31"
D - 幾日,以3個英文字表示,例如:" Fri "
F - 幾月,以英文全名表示,例如:" January "
g - 小時,12小時制不足2位數不補0,例如:" 1" 到 " 12 "
G - 小時,24小時制不足2位數不補0,例如:" 0 " 到 " 23 "
h - 小時,12小時制,例如:" 01" 到 " 12 "
H - 小時,24小時制,例如:" 00 " 到 " 23 "
i - 幾分,例如:" 00 " 到 " 59 "
I (大寫的 i) - "1" if Daylight Savings Time, "0" otherwise.
j - 幾日,不足2位數不補0,例如:" 1" 到 " 31"
l (小寫的 'L') - 幾日,以英文全名表示,例如:"Friday"
L - 布林值,判斷是否為閏年,例如:" 0" 或 " 1"
m - 幾月,例如:" 01" 到 " 12"
M - 幾月,以3個英文字表示,例如:"Jan"
n - 幾月,不足2位數不補0,例如:" 1" 到 "12"
s - 幾秒,例如:" 01" 到 " 59"
S - 以英文後2個字表示,例如:"th","nd"
t - 當月的天數,例如:" 28" 到 " 31"
T - 這個機器的時間區域設定,例如 :"MDT"
U - 總秒數
w - 以數字表示星期幾,例如:" 0" 到 " 6"
z - 一年中的第幾天,例如:" 0" 到 " 365"
Z - 在短時間內時間區域補償(timezone offset) ,例如:"-43200" to "43200"
2012年6月28日 星期四
JavaScript攔鍵盤訊息
步驟一、body中加入onkeydow要call的function name
<body onkeydown="enterkey()">步驟二、function中做你想做的事
function enterkey() { e = event.keyCode; if (e==13||e==32) //加入想要攔的id { //do something //... event.returnValue= false; //不做接下來的事 } }
HTTP GET and POST request in Javascript?
Javascript 直接發出 POST request
function post_to_url(path, params, method) { method = method || "post"; // Set method to post by default, if not specified. // The rest of this code assumes you are not using a library. // It can be made less wordy if you use one. var form = document.createElement("form"); form.setAttribute("method", method); form.setAttribute("action", path); for(var key in params) { var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", key); hiddenField.setAttribute("value", params[key]); form.appendChild(hiddenField); } document.body.appendChild(form); // Not entirely sure if this is necessary form.submit(); }使用方式
post_to_url('http://http://www.touch-idea.net/harland/sky/admin_quest.php', {'action':'search'}, 'post');Javascript 直接發出 GET request
function httpGet(theUrl) { var xmlHttp = null; xmlHttp = new XMLHttpRequest(); xmlHttp.open( "GET", theUrl, false ); xmlHttp.send( null ); return xmlHttp.responseText; }
訂閱:
文章 (Atom)