入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

PHP方法字符串拼接转化mysql进行数据查询

创建时间:2017-06-30 投稿人: 浏览次数:157

定义方法部分:

//定义数组  
private $_getjoinstr = array();  
  
/** 
    字段中包含 
*/  
public function dbinstr($fiekd, $str, $spl1=",", $spl2=",")  
{  
    return "instr(concat("$spl1", $fiekd, "$spl2"), "".$spl1.$str.$spl2."")>0";  
}  
  
//字符串拼接转化mysql方法  
public function getjoinstr($fids, $us, $lx=0, $slx=0)  
{  
    $s      = "";  
    //判断是否是数字  
    if(is_numeric($us)){  
        $key= "a".$fids."".$us."_".$lx."";  
        if(isset($this->_getjoinstr[$key]))return $this->_getjoinstr[$key];  
        //admin表中查询id、name、deptid、deptath  
        $us = $this->getone($us,"id,`name`,`deptid`,`deptpath`");  
    }  
    //如果没有数据,返回空  
    if(!$us)return "";  
    //有数据执行查询  
    $uid    = $us["id"];  
    $key    = "a".$fids."".$uid."_".$lx."";  
    if(isset($this->_getjoinstr[$key]))return $this->_getjoinstr[$key];  
    //判断是否为空 为空也执行查询   
    if($slx==0)$tj[]    = "ifnull($fids,"")=""";  
    //判断是否包含 all  
    $tj[]   = $this->dbinstr($fids, "all");  
    //判断包含的u开头的人员信息  
    $tj[]   = $this->dbinstr($fids, "u".$uid);  
    //根据当前用户信息去查询当前用户所在组  
    if($us){  
        $dep = explode(",", $us["deptpath"]);  
        foreach($dep as $deps){  
            $_deps  = str_replace(array("[","]"), array("",""), $deps);  
            $tj[]   = $this->dbinstr($fids, "d".$_deps);  
        }  
    }  
    //用or链接  
    $s  = join(" or ", $tj);  
    $this->_getjoinstr[$key] = $s;  
    return $s;  
}  

调用部分:

$uid="1";//当前传入用户的ID  
$where_userid = m("admin")->getjoinstr("userid", $uid);  
if($where_userid) $s.= $where_userid;  
//获取所有的步骤  
$rows = $this->db->getall("SELECT * FROM [Q]flow_course WHERE " .$s. " ORDER BY sort,id ASC"); 



数据库表中字段存储:


声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。