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

51 篇文章 2 订阅

定义方法部分:

//定义数组  
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"); 



数据库表中字段存储:


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值