广告广告
  加入我的最爱 设为首页 风格修改
首页 首尾
 手机版   订阅   地图  繁体 
您是第 3875 个阅读者
 
发表文章 发表投票 回覆文章
  可列印版   加为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.019103 second(s),query:15 Gzip disabled
本站由 瀛睿律师事务所 担任常年法律顾问 | 免责声明 | 本网站已依台湾网站内容分级规定处理 | 连络我们 | 访客留言