Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
成功删除0407目录
if(!function_exists('delete_dir_file')){
function delete_dir_file($dir){
$flag = false;
if(is_dir($dir)){
if($handle = opendir($dir)){
while(($file = readdir($handle)) !== false){
// echo $file.'<br>';
if($file !=='.'&&$file !=='..'){
if(is_dir($dir.DIRECTORY_SEPARATOR.$file)){
delete_dir_file($dir.DIRECTORY_SEPARATOR.$file);
}else unlink($dir.DIRECTORY_SEPARATOR.$file);
}
}
}
closedir($handle);
if(rmdir($dir)) $flag = true;
}
return $flag;
}
}
$dir = __DIR__.DIRECTORY_SEPARATOR.'0407';
// echo $dir;
if(delete_dir_file($dir)){
echo json_encode(['msg'=>'删除成功','code'=>0],320);
}else{
echo json_encode(['msg'=>'删除失败','code'=>1],320);
实现产品多级分类并在前端显示
$db = new PDO('mysql:host=localhost;dbname=laravel','root','root');
$RES = $db->query('select `id`,`pid`,`name` from `cates` where `status`=1;')->fetchAll(PDO::FETCH_ASSOC);
printf('<pre>%s</pre>',print_r($RES,true));
function ruleLayer($rule, $pid = 0){
$arr = [];
foreach($rule as $v){
if($v['pid']==$pid){
$v['child'] = ruleLayer($rule, $v['id']);
$arr[] = $v;
}
}
return $arr;
}
$result = ruleLayer($RES);
printf('<pre>%s</pre>',print_r($result,true));
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul class="first">
<?php foreach($result as $v): extract($v)?>
<li>
<?=$name?>
<ul>
<?php if(!empty($child)):
foreach($child as $v1): extract($v1)?>
<li>
<?=$name?>
<ul>
<?php if(!empty($child)):
foreach($child as $v2): extract($v2)?>
<li>
<?=$name?>
<ul>
<?php if(!empty($child)):
foreach($child as $v3): extract($v3)?>
<li> <?=$name?> </li>
<?php endforeach;
endif?>
</ul>
</li>
<?php endforeach;
endif?>
</ul>
</li>
<?php endforeach;
endif?>
</ul>
</li>
<?php endforeach?>
</ul>
</body>
</html>
$arr = ['html', 'css', 'vue'];
$str = implode('|', $arr);
// echo $str;
// 合并,字符串->数组 explode
// echo $_SERVER['REQUEST_URI'];
$pathinfo = (explode("/", $_SERVER['REQUEST_URI']));
echo array_pop($pathinfo); //弹出方法名称
// 字符串的替换 str_replace($被替换内容, $替换内容,检测文档,替换次数)
$search = ['广告','转账'];
$chatMSG = '本公司代理各类广告,可转账。';
$res = str_replace($search, "**",$chatMSG,$COUNT);
echo $res, $COUNT;
if($COUNT == 0){
// ws->sendMessage();
}else{
// 警告功能
}
// 字符串查找、截取
// 随机形成验证码
// echo rand();
$str = md5(rand());
$code = substr($str, 0,4);
$color = '#'.substr(md5(mt_rand()), 0, 6);
echo "<h1 style='color:{$color}'>{$code}</h1>";
echo mb_substr('你吃饭了吗?', 1, 2);
有的电脑是REQUEST_URI,而不是DOCUMENT_URI
http_build_query、urlencode、 urldecode、base64_encode、base64_decode
// url函数
echo urlencode('韩寒');
echo urldecode('%E9%9F%A9%E5%AF%92');
// http_build_query(),生产urlencode之后的请求字符串
$params = [
'city' => '北京',
'key' => '298uejkfl',
];
$paramsString = http_build_query($params);
echo $paramsString;
ob_clean();
// base64_encode()使用mime base64 对二进制字符串进行编码 ,目的是为了使二进制数据可以通过非纯8-bit的传输层传输,例如电子邮件的主体。
// header('content-type:image/jpeg');
$file = base64_encode(file_get_contents("https://img-nos.yiyouliao.com/alph/a21a0c67a67682baddf116e04826ef0d.jpeg?yiyouliao_channel=1536235174142947329_image"));
echo $file;
?>
<body>
<h1>您好!</h1>
<img src="data:image/jpeg;base64,<?=$file?>" alt="">
</body>
递归函数挺有用,用于产品分类也很实用;字符串操作也是基本技能,必须掌握;后面的几个函数之前没接触过,先了解一下吧!