码迷,mamicode.com
首页 > 数据库 > 详细

杂记整理三:php、thinkphhp和sql

时间:2017-07-08 22:58:39      阅读:601      评论:0      收藏:0      [点我收藏+]

标签:return   eve   降维   fse   注意   ini   each   school   use   

php 循环,v为键,c为值

$list = array( "aaa"=>"ttt", "bbb"=>"sss" );

foreach($list as $v => $c){
    echo $v;
    echo $c;
};

 

生成定长的62进制随机码(0-9, a-z, A-Z)

function getRandom($param){
    $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $key = "";

    for($i=0;$i<$param;$i++)
    {
        $key .= $str{mt_rand(0,32)}; //生成php随机数
    }

    return $key;
}

 

sql 递归查询

 SELECT SORT_ID AS ID,SORT_PARENT AS 父ID ,levels AS 父到子之间级数, paths AS 父到子路径 FROM (
     SELECT SORT_ID,SORT_PARENT,
     @le:= IF (SORT_PARENT = 0 ,0,
         IF( LOCATE( CONCAT(|,SORT_PARENT,:),@pathlevel)   > 0  ,
                  SUBSTRING_INDEX( SUBSTRING_INDEX(@pathlevel,CONCAT(|,SORT_PARENT,:),-1),|,1) +1
        ,@le+1) ) levels
     , @pathlevel:= CONCAT(@pathlevel,|,SORT_ID,:, @le ,|) pathlevel
      , @pathnodes:= IF( SORT_PARENT =0,,0,
           CONCAT_WS(,,
           IF( LOCATE( CONCAT(|,SORT_PARENT,:),@pathall) > 0  ,
               SUBSTRING_INDEX( SUBSTRING_INDEX(@pathall,CONCAT(|,SORT_PARENT,:),-1),|,1)
              ,@pathnodes ) ,SORT_PARENT  ) )paths
    ,@pathall:=CONCAT(@pathall,|,SORT_ID,:, @pathnodes ,|) pathall
        FROM  flow_sort,
    (SELECT @le:=0,@pathlevel:=‘‘, @pathall:=‘‘,@pathnodes:=‘‘) vv
    ORDER BY  SORT_PARENT,SORT_ID
    ) src
ORDER BY SORT_ID

 

php 二维数组去掉重复值,保留键值,恢复行索引,自动修改列索引,如果只有小于2行(没有排序的必要),则直接返回原数组。

protected function array_unique_fb($array2D){

    if(count($array2D)>1){
        $keyarr = [];
        foreach($array2D[0] as $k => $v){
            $keyarr[] = $k;
        }
foreach ($array2D as $k=>$v){
            $v=join(‘,‘,$v);  //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
            $temp[$k]=$v;
        }
        $temp=array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
        foreach ($temp as $k => $v){
            $array=explode(‘,‘,$v); //再将拆开的数组重新组装
            //(下面的索引根据自己的情况进行修改即可)
            //自动回复索引

            foreach($keyarr as $sk => $sv){
                $temp2[$k][$sv] =$array[$sk];
            }
        }

        $temp2 = array_values($temp2);

    }else{
        $temp2 = $array2D;
    }

return $temp2;
}

 

对页面进行缓存

function pageCache($siteId,$page = ‘‘,$expire = 7200){
    $siteId = $siteId?:1;
    $fileName = $siteId.‘.html‘;
    $filePath = CACHE_PATH.$fileName;

    if($page){
        file_put_contents($filePath,$page);
    }else{
        if(file_exists($filePath)){
            $editTime =  fileatime($filePath);
            $page = ‘‘;
            if((time() - $editTime) <= $expire){
                $page =  file_get_contents($filePath);
            }
        }
    }
    if($page){
        echo $page;
        exit;
    }
}

 

数组输出json,注意如果有跨域操作(如有api站点)需要加跨域的HTTP头

$arr = [
    ‘haha‘  => 15,
    ‘tata‘  => ‘shiningsun‘,
    ‘lala‘  => [15,16],
];

header("Access-Control-Allow-Origin:*");

$result = json_encode($arr);

 

php 遍历文件名

/***********************
用readdir()函数
 ************************/
function listDir($dir)
{
    if(is_dir($dir))
    {
        if ($dh = opendir($dir))
        {
            $arr = array();
            while (($file = readdir($dh)) !== false)
            {
                if((is_dir($dir."/".$file)) && $file!="." && $file!="..")
                {
                    $arr[$file] = listDir($dir."/".$file."/");
                }
                else
                {
                    if($file!="." && $file!="..")
                    {
                            $arr[$file] = get_extension($file);
                    }

                }
            }
            closedir($dh);

            return $arr;
        }
    }
}

/***********************
获取文件扩展名
 ************************/
function get_extension($file)
{
    return pathinfo($file, PATHINFO_EXTENSION);
}

 

从url中读取页面信息

$url=‘http://school.51cce.dev/klzhhdx/news/‘;
$use_include_path = false;
$context = null;
$offset = -1;
$contents = file_get_contents($url, $use_include_path, $context, $offset);

dump($contents);

 

 

 

thinkphp 5.0 写法总结:
 
接收参数
Request::instance()->post(‘qq‘) && $data[‘QQ‘]                    = Request::instance()->post(‘qq‘);
Request::instance()->post(‘email‘) && $data[‘Email‘]              = Request::instance()->post(‘email‘);
Request::instance()->post(‘mobile‘) && $data[‘ContactsTel‘]       = Request::instance()->post(‘mobile‘);

 

判断是否有post值(get同理)

if(Request::instance()->isPost())

 

 

杂记整理三:php、thinkphhp和sql

标签:return   eve   降维   fse   注意   ini   each   school   use   

原文地址:http://www.cnblogs.com/shining77/p/7138432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!