thinkphp5项目--个人博客(七)

thinkphp5项目--个人博客(七)

项目地址

fry404006308/personalBlog: personalBlog
https://github.com/fry404006308/personalBlog

一、标签部分(thinkphp框架中html中可以直接写原生的php)

视图

 1 这是另一个位置相似的代码,加了span标签便于理解html和php的关系
 2 
 3                         <span class="writor">标签:
 4                             <?php 
 5                                 $arr=explode(',',$articleres['keywords']);
 6                                 foreach($arr as $k => $v){
 7                                     echo "<a href='#'>$v</a>";
 8                                 }
 9                             ?>
10                         </span>

二、两个常用的string函数str_replace和explode

替换文章关键词中的中文逗号为英文逗号

'keywords'=>str_replace(',', ',', input('keywords')),

以英文逗号来截取文章

$arr=explode(',',$articleres['keywords']);

三、ThinkPHP where的使用

参考手册

 1 普通查询
 2 最简单的数组查询方式如下:
 3 
 4 $map['name'] = 'thinkphp';
 5 $map['status'] = 1;
 6 // 把查询条件传入查询方法
 7 Db::table('think_user')->where($map)->select();
 8 // 助手函数
 9 db('user')->where($map)->select();
10 最后生成的SQL语句是
11 SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1

项目中用处

 1     //相关文章
 2     public function ralate($keywords,$id){
 3         $arr=explode(',',$keywords);
 4         static $ralateres= array();
 5         foreach ($arr as $k => $v) {
 6             //找相似关键词的文章
 7             $map['keywords']=['like','%'.$v.'%'];
 8             //自己相同的文章不能显示在这里,id不能相同
 9             $map['id']=['neq',$id];
10             $tmp=db('article')->where($map)->order('id desc')->limit(8)->select();
11             $ralateres=array_merge($ralateres,$tmp);
12         }
13         //给$ralateres去重,因为不同关键词匹配的时候可能匹配到重复文章
14         // array_unique只能做一维的,所以我们用自己写的函数
15         // $ralateres=arr_unique($ralateres);
16 
17         return $ralateres;
18     }

四、同类文章推荐

找两篇文章的多个标签中能否有一个相同

 1     //相关文章
 2     public function ralate($keywords,$id){
 3         $arr=explode(',',$keywords);
 4         static $ralateres= array();
 5         foreach ($arr as $k => $v) {
 6             //找相似关键词的文章
 7             $map['keywords']=['like','%'.$v.'%'];
 8             //自己相同的文章不能显示在这里,id不能相同
 9             $map['id']=['neq',$id];
10             $tmp=db('article')->where($map)->order('id desc')->limit(8)->select();
11             $ralateres=array_merge($ralateres,$tmp);
12         }
13         //给$ralateres去重,因为不同关键词匹配的时候可能匹配到重复文章
14         // array_unique只能做一维的,所以我们用自己写的函数
15         // $ralateres=arr_unique($ralateres);
16 
17         return $ralateres;
18     }

五、使用函数文件

原文地址:https://www.cnblogs.com/Renyi-Fan/p/8816484.html