廣告廣告
  加入我的最愛 設為首頁 風格修改
首頁 首尾
 手機版   訂閱   地圖  簡體 
您是第 3856 個閱讀者
 
發表文章 發表投票 回覆文章
  可列印版   加為IE收藏   收藏主題   上一主題 | 下一主題   
月光 手機
個人頭像
個人文章 個人相簿 個人日記 個人地圖
社區建設獎
頭銜:看我代替月亮懲罰你!看我代替月亮懲罰你!
版主
級別: 版主 該用戶目前不上站
版區: Unix-like, 網站架設
推文 x42 鮮花 x565
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片
推文 x0
[PHP][原碼] 分析資料輸入欄位,並轉成 SQL 查詢字句
原文
http://blog.roodo.com/rocksay...1149103.html


在表單中,可讓使用者查詢指定的門市代號。門市代號全為數字,以空白或逗點分隔各門市代號,並可使用連字符號 (-) 表示從哪個門市到哪個門市。例如輸入 000-010 表示查詢從 000到010 的門市。又如輸入 000,005, 010-020 ,則表示查詢 000 、005 以及從 010 到 020 的門市。解析輸入字串,轉成 SQL 查詢語法的一部份。


複製程式
function parse_shop_string($shops) {   
    if(emptyempty($shops))   
        return "";   
/*  
分離 單店 與 區間 表示。  
s1 = 單店  
s2 = 區間  
 
ex: shops = "000,003-005, 010 020, 230-240 301"  
 
s1 = Array ( [0] => 000 [1] => 010 [2] => 020 [3] => 301 )  
s2 = Array ( [0] => BW_003_005_END [1] => BW_230_240_END [2] => )  
 
query_string = "(_TABLE.shop = 願' or _TABLE.shop = 顢' or _TABLE.shop = 顬' or _TABLE.shop = 馅' or _TABLE.shop between 顛' and 顝' or _TABLE.shop between 餾' and 饈')";  
 
*/  
    $s = preg_replace("/[a-z']/i", "", $s);   
     /*移除非預期字元,即英文字母及單引號(single quote)*/  
  
    $s1 = preg_split("/(\s|,)+/",preg_replace("/(\d+)-(\d+)/", "", $shops));   
    $s2 = preg_split("/\s+/",preg_replace("/(\d+(\s|,)+)|(\d+$)|(,+)/" ,"",   
         preg_replace("/(\d+)-(\d+)/", "BW_\$1_\$2_END ", $shops)));   
  
    $query_string_s1 = "";   
    $shop = current($s1);   
    do {   
        if(!emptyempty($shop)) {   
            if(emptyempty($query_string_s1))   
                $query_string_s1 = "_TABLE.shop = '". $shop ."'";   
            else  
                $query_string_s1 = $query_string_s1 . " or _TABLE.shop = '{$shop}'";   
        }   
    } while($shop = next($s1));   
  
    $query_string_s2 = "";   
    $shop = current($s2);   
    do {   
        if(!emptyempty($shop)) {   
            if(emptyempty($query_string_s2))   
                $query_string_s2 = "_TABLE.shop ".   
                    preg_replace("/BW_(\d+)_(\d+)_END/","between '\$1' and '\$2' ",$shop);   
            else  
                $query_string_s2 = $query_string_s2 . " or _TABLE.shop ".   
                    preg_replace("/BW_(\d+)_(\d+)_END/","between '\$1' and '\$2' ",$shop);   
        }   
    } while($shop = next($s2));   
  
    if(emptyempty($query_string_s1))   
        $query_string = $query_string_s2;   
    elseif(emptyempty($query_string_s2))   
        $query_string = $query_string_s1;   
    else  
        $query_string = $query_string_s1 ." or " . $query_string_s2;   
  
    return ("(". $query_string .")");   
}   
  
  
$grade_date = "2006-02-20";   
$args['shop_query_string'] = parse_shop_string($_POST['shops']);  
 
$products_stock_query_string = " and ". preg_replace("/_TABLE.shop /", "product_stock.shop ", $args['shop_query_string']);  
 
$sql_query_string = "select product_stock.* from product_stock where grade_date='{$grade_date}' {$products_stock_query_string}";   
  
function parse_shop_string($shops) {
       if(empty($shops))
              return "";
/*
分離 單店 與 區間 表示。
s1 = 單店
s2 = 區間

ex: shops = "000,003-005, 010 020, 230-240 301"

s1 = Array ( [0] => 000 [1] => 010 [2] => 020 [3] => 301 )
s2 = Array ( [0] => BW_003_005_END [1] => BW_230_240_END [2] => )

query_string = "(_TABLE.shop = 願' or _TABLE.shop = 顢' or _TABLE.shop = 顬' or _TABLE.shop = 馅' or _TABLE.shop between 顛' and 顝' or _TABLE.shop between 餾' and 饈')";

*/
       $s = preg_replace("/[a-z']/i", "", $s);
        /*移除非預期字元,即英文字母及單引號(single quote)*/

       $s1 = preg_split("/(\s|,)+/",preg_replace("/(\d+)-(\d+)/", "", $shops));
       $s2 = preg_split("/\s+/",preg_replace("/(\d+(\s|,)+)|(\d+$)|(,+)/" ,"",
               preg_replace("/(\d+)-(\d+)/", "BW_\$1_\$2_END ", $shops)));

       $query_string_s1 = "";
       $shop = current($s1);
       do {
              if(!empty($shop)) {
                     if(empty($query_string_s1))
                            $query_string_s1 = "_TABLE.shop = '". $shop ."'";
                     else
                            $query_string_s1 = $query_string_s1 . " or _TABLE.shop = '{$shop}'";
              }
       } while($shop = next($s1));

       $query_string_s2 = "";
       $shop = current($s2);
       do {
              if(!empty($shop)) {
                     if(empty($query_string_s2))
                            $query_string_s2 = "_TABLE.shop ".
                                   preg_replace("/BW_(\d+)_(\d+)_END/","between '\$1' and '\$2' ",$shop);
                     else
                            $query_string_s2 = $query_string_s2 . " or _TABLE.shop ".
                                   preg_replace("/BW_(\d+)_(\d+)_END/","between '\$1' and '\$2' ",$shop);
              }
       } while($shop = next($s2));

       if(empty($query_string_s1))
              $query_string = $query_string_s2;
       elseif(empty($query_string_s2))
              $query_string = $query_string_s1;
       else
              $query_string = $query_string_s1 ." or " . $query_string_s2;

       return ("(". $query_string .")");
}


$grade_date = "2006-02-20";
$args['shop_query_string'] = parse_shop_string($_POST['shops']);

$products_stock_query_string = " and ". preg_replace("/_TABLE.shop /", "product_stock.shop ", $args['shop_query_string']);

$sql_query_string = "select product_stock.* from product_stock where grade_date='{$grade_date}' {$products_stock_query_string}";




月光論壇
http://bbs.dj...com/


===================================
贊助本站 -- 刊登廣告 -- 物超所值虛擬主機租用
獻花 x0 回到頂端 [樓 主] From:臺灣中華電信HINET | Posted:2007-05-22 09:28 |

首頁  發表文章 發表投票 回覆文章
Powered by PHPWind v1.3.6
Copyright © 2003-04 PHPWind
Processed in 0.017508 second(s),query:15 Gzip disabled
本站由 瀛睿律師事務所 擔任常年法律顧問 | 免責聲明 | 本網站已依台灣網站內容分級規定處理 | 連絡我們 | 訪客留言