2011年6月30日 星期四

利用eregi_replace擷取URL執行檔案位置

 目的在於擷取當前URL執行檔案的位置,再根據此位置擷取需要的字串
 當然這必須要把自己開發的案子所有檔案的格式做一定的規劃
 底下是在開發過程中所有使用到擷取的方式

$url="http://localhost/test_test_test.php?N=1&B=2";//假設我把當前網址設為底下這形式

PHP中要取得目前網址要用的方式

$_SERVER['HTTP_HOST']; //網址
$_SERVER['REQUEST_URI']; //包含參數(包含目錄)
$_SERVER['PHP_SELF']; //只有檔案名稱(包含目錄)
$_SERVER['QUERY_STRING']; //只取後面參數

假設今天只需擷取檔案名稱(不包含目錄、包含參數)可以用以下的方式

echo eregi_replace(".*/", '', $url);//結果:test_test_test.php?N=1&B=2

假設今天只需擷取檔案名稱中從字元開始到第一次出現底線後結束(不包含底線)可以用以下取代方式
echo substr(eregi_replace(".*/", '', $url), 0, strpos(eregi_replace(".*/", '', $url), '_'));//結果:test

假設今天只需擷取檔案名稱中從字元開始到最後一次出現底線後結束(不包含底線)可以用以下取代方式


echo substr(eregi_replace(".*/", '', $url), 0, strrpos(eregi_replace(".*/", '', $url), '_'));//結果:test_test




[jQuery]縣市連動下拉選單(縣市合併郵遞區號前三碼)

郵遞區號已更正~已打包完成~請於連結下載~如有發現錯誤
請告知~中華郵政目前只看到3+2碼對照~

參考網址:http://www.powerweb.tw/modules/news/V98

由於縣市合併後舊式的下拉選單選擇縣市資料都要要更新,要一筆一筆對照然後更改有些累人(懶)
 就去找了"縣市合併新版 3 碼郵遞區號 MySQL 匯入檔",
參考網址:http://www.powerweb.tw/modules/news/V99

 把資料庫整理了下,在自己利用jQuery寫了底下縣市下拉選單功能帶入郵遞區號,也順便把整理後的MySql資料庫匯出


展示頁程式碼

<?php
include 'Include/open.php';
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                //利用jQuery的ajax把縣市編號(CNo)傳到Town_ajax.php把相對應的區域名稱回傳後印到選擇區域(鄉鎮)下拉選單
                $('#myCity').change(function(){
                    var CNo= $('#myCity').val();
                    $.ajax({
                        type: "POST",
                        url: 'Town_ajax.php',
                        cache: false,
                        data:'CNo='+CNo,
                        error: function(){
                            alert('Ajax request 發生錯誤');
                        },
                        success: function(data){
                            $('#myTown').html(data);
                            $('#myZip').val("");//避免重新選擇縣市後郵遞區號還存在,所以在重新選擇縣市後郵遞區號欄位清空
                        }
                    });
                });
                //根據選擇區域(鄉鎮)的編號傳到Zip_ajax.php把區域對應的郵遞區號印到指定的郵遞區號欄位
                $('#myTown').change(function(){
                    var TNo= $('#myTown').val();
                    $.ajax({
                        type: "POST",
                        url: 'Zip_ajax.php',
                        cache: false,
                        data:'TNo='+TNo,
                        error: function(){
                            alert('Ajax request 發生錯誤');
                        },
                        success: function(data){
                            $('#myZip').val(data);
                        }
                    });
                });
            });
        </script>
    </head>
    <body>
        <table width="100%" border="0" cellspacing="0" cellpadding="0" align=center>    
            <tr> 
                <td width="113" height="28">通訊地址:</td>
                <td height="28" colspan="3">
                    <select name="F_I_CNo" id="myCity">
                        <option value="">選擇縣市</option>
                        <?php
                        $City = "select * from City where State=0";
                        $City_rs = mysql_query($City);
                        while ($City_rows = mysql_fetch_array($City_rs)) {
                            ?>
                            <option value="<?php echo $City_rows["AutoNo"] ?>"><?php echo $City_rows["Name"]; ?></option>
                            <?php
                        }
                        ?>
                    </select>
                    <select name="F_I_TNo" id="myTown">
                        <option value="">選擇鄉鎮</option>
                    </select>
                    郵遞區號:
                    <input id="myZip" type="text" class="field10" Name="F_S_NH_Zip" value="" size="5" readonly="ture"/>
                    地址:
                    <input type="text" class="field10" Name="F_S_NH_Address" value="" style="width:210px;"/>
                </td>
            </tr>
        </table>
    </body>
</html>


Town_ajax.php(回傳區域(鄉鎮)的內容)程式碼

<?php
include 'Include/open.php';
$Town = "select * from Town where CNo='" . $_POST["CNo"] . "'";
$Town_rs = mysql_query($Town);
$Town_num = mysql_num_rows($Town_rs);
if ($Town_num > 0) {//縣市編號帶入後如果有資料存在顯示底下區域內容回傳
    echo "<option value=''>選擇鄉鎮</option>";
    while ($Town_rows = mysql_fetch_array($Town_rs)) {
        echo "<option value='" . $Town_rows["AutoNo"] . "'>" . $Town_rows["Name"] . "</option>";
    }
} else {//縣市編號帶入後如果有資料存在顯示底下內容回傳
    echo "<option value=''>選擇鄉鎮</option>";
}
?>

Zip_ajax.php(回傳郵遞區號資料)程式碼

<?php
include("Include/open.php");
$Zip = "select * from Town where AutoNo='" . $_POST["TNo"] . "'";
$Zip_rs = mysql_query($Zip);
$Zip_num = mysql_num_rows($Zip_rs);
if ($Zip_num > 0) {//區域(鄉鎮)編號帶入後如果有資料存在顯示底下郵遞區號內容後回傳
    $Town_rows = mysql_fetch_array($Zip_rs);
    echo $Town_rows["Post"];
} else {
    echo "無資料";
}
?>


如果有錯誤的地方請告知^.^

檔案打包下載(包含資料庫)

window.open跟window.showModalDialog取得父視窗的控制項

最近在使用開啟新視窗的一個糗事
原本是使用window.showModalDialog開啟新視窗,但卻使用window.opener取得父視窗的控制項

然後一直沒辦法控制(這問題找了半個小時),然後問了位學長,
他只回我.......
window.opener是用在window.open >"<
=>要取得window.open父視窗的控制項,請使用window.opener;
=>要取得window.showModalDialog父視窗的控制項,請使用    window.dialogArguments;

FCKeditorAPI的使用方法

此篇在記錄著在使用FCKeditor編輯器時所使用的FCKeditorAPI 

1. 取得編輯器裡頭的內容
   var oEditor=FCKeditorAPI.GetInstance('Contents');
   var   Contents   =   oEditor.GetXHTML();
   其中('Contents');為內容欄位名稱 



2.設置編輯器裡頭的內容
     var oEditor=FCKeditorAPI.GetInstance('欄位名稱');
     oEditor.SetHTML(內容); 



3.子視窗設置父視窗編輯器裡頭的內容
     var oEditor=window.opener.FCKeditorAPI.GetInstance('欄位名稱');
     oEditor.SetHTML(內容);



2011年6月13日 星期一

PHP Function:計算(日、月、年)數差距

藉由給定的時間算出時間的差距

例子一:給定起始時間,算出差幾天
例子二:給定起始時間,算出差幾個月
例子三:給定起始時間,算出差幾年



//例子一
function dateDiff($startDate, $endDate) {
    $startArry = getdate(strtotime($startDate));
    $endArry = getdate(strtotime($endDate));
    $start_date = gregoriantojd($startArry["mon"], $startArry["mday"], $startArry["year"]);
    $end_date = gregoriantojd($endArry["mon"], $endArry["mday"], $endArry["year"]);
    return round(($end_date - $start_date), 0);
}

//例子二
function monDiff($startDate, $endDate) {
    $startyear = date("Y", strtotime($startDate));
    $endyear = date("Y", strtotime($endDate));
    $startArry = date("m", strtotime($startDate));
    $endArry = date("m", strtotime($endDate));
    return round(($endyear - $startyear), 0) * 12 + round(($endArry - $startArry), 0);
}

//例子三
function yearDiff($startDate, $endDate) {
    $startyear = date("Y", strtotime($startDate));
    $endyear = date("Y", strtotime($endDate));
    return round(($endyear - $startyear), 0);
}
//時間帶入function
echo "2011-05-03到2011-05-10 差" . dateDiff('2011-05-03', '2011-05-10') . '天<BR>';
echo "2011-1-03到2011-05-10 差" . monDiff('2011-1-03', '2011-05-10') . '月<BR>';
echo "2009-1-03到2011-05-10 差" . yearDiff('2009-1-03', '2011-05-10') . '年<BR>';