* 注:此方法查询时关联三个表term_relationships,posts,users;在指定查询字段(field),排序(order),指定查询条件(where)最好指定一下表名 * @param string $tag
查询标签,以字符串方式传入 * 例:"cid:1,2;field:posts.post_title,posts.post_content;limit:0,8;order:post_date desc,listorder desc;where:id>0;" * ids:文章id,可以指定一个或多个文章id,以英文逗号分隔,如1或1,2,3 * cid:文章所在分类,可指定一个或多个分类id,以英文逗号分隔,如1或1,2,3 默认值为全部 * field:调用指定的字段 * 如只调用posts表里的id和post_title字段可以是field:posts.id,posts.post_title; 默认全部, * 此方法查询时关联三个表term_relationships,posts,users; * 所以最好指定一下表名,以防字段冲突 * limit:数据条数,默认值为10,可以指定从第几条开始,如3,8(表示共调用8条,从第3条开始) * order:排序方式,如按posts表里的post_date字段倒序排列:posts.post_date desc * where:查询条件,字符串形式,和sql语句一样,请在事先做好安全过滤,最好使用第二个参数$where的数组形式进行过滤,此方法查询时关联多个表,所以最好指定一下表名,以防字段冲突* @param array $where 查询条件(只支持数组),格式和thinkphp的where方法一样,此方法查询时关联多个表,所以最好指定一下表名,以防字段冲突; * @param int $pagesize 每页条数,为0,false表示不分页 * @param string $pagetpl 以字符串方式传入,例:"{first}{prev}{liststart}{list}{listend}{next}{last}" * @return array 包括分页的文章列表
* 格式: * array( * "posts"=>array(),//文章列表,array * "page"=>"",//生成的分页html,不分页则没有此项 * "count"=>100, //符合条件的文章总数,不分页则没有此项 * "total_pages"=>5 // 总页数 * )*/ public static function posts($tag,$where=array(),$pagesize=0,$pagetpl=''){ $where=is_array($where)?$where:array(); $tag=sp_param_lable($tag); $field = !empty($tag['field']) ? $tag['field'] : '*'; $limit = !empty($tag['limit']) ? $tag['limit'] : '0,10'; $order = !empty($tag['order']) ? $tag['order'] : 'post_date DESC'; //根据参数生成查询条件 $where['term_relationships.status'] = array('eq',1); $where['posts.post_status'] = array('eq',1); if (isset($tag['cid'])) { $tag['cid']=explode(',', $tag['cid']); $tag['cid']=array_map('intval', $tag['cid']); $where['term_relationships.term_id'] = array('in',$tag['cid']); } if (isset($tag['ids'])) { $tag['ids']=explode(',', $tag['ids']); $tag['ids']=array_map('intval', $tag['ids']); $where['term_relationships.object_id'] = array('in',$tag['ids']); } if (isset($tag['where'])) { $where['_string'] = $tag['where']; } $join = '__POSTS__ as posts on term_relationships.object_id = posts.id'; $join2= '__USERS__ as users on posts.post_author = users.id'; $term_relationships_model= M("TermRelationships"); $content=array(); if (empty($pagesize)) { $posts=$term_relationships_model ->alias("term_relationships") ->join($join) ->join($join2) ->field($field) ->where($where) ->order($order) ->limit($limit) ->select(); }else{ $pagetpl = empty($pagetpl) ? '{first}{prev}{liststart}{list}{listend}{next}{last}' : $pagetpl; $totalsize=$term_relationships_model ->alias("term_relationships") ->join($join) ->join($join2) ->field($field) ->where($where) ->count(); $pagesize = intval($pagesize); $page_param = C("VAR_PAGE"); $page = new \Page($totalsize,$pagesize); $page->setLinkWraper("li"); $page->__set("PageParam", $page_param); if(sp_is_mobile()){ $pagesetting= array("listlong" => "2", "prev" => "上一页", "next" => "下一页", "list" => "*", "disabledclass" => ""); }else{ $pagesetting=array("listlong" => "4", "first" => "首页", "last" => "尾页", "prev" => "上一页", "next" => "下一页", "list" => "*", "disabledclass" => ""); } $page->SetPager('default', $pagetpl,$pagesetting); $posts=$term_relationships_model ->alias("term_relationships") ->join($join) ->join($join2) ->field($field) ->where($where) ->order($order) ->limit($page->firstRow, $page->listRows) ->select(); $content['page']=$page->show('default'); $content['total_pages']=$page->getTotalPages(); // 总页数 $content['count']=$totalsize; } $content['posts']=$posts; return $content; } /** * 查询文章列表,不做分页 * 注:此方法查询时关联三个表term_relationships,posts,users;在指定查询字段(field),排序(order),指定查询条件(where)最好指定一下表名 * @param string $tag
查询标签,以字符串方式传入 * 例:"cid:1,2;field:posts.post_title,posts.post_content;limit:0,8;order:post_date desc,listorder desc;where:id>0;" * ids:文章id,可以指定一个或多个文章id,以英文逗号分隔,如1或1,2,3 * cid:文章所在分类,可指定一个或多个分类id,以英文逗号分隔,如1或1,2,3 默认值为全部 * field:调用指定的字段 * 如只调用posts表里的id和post_title字段可以是field:posts.id,posts.post_title; 默认全部, * 此方法查询时关联三个表term_relationships,posts,users; * 所以最好指定一下表名,以防字段冲突 * limit:数据条数,默认值为10,可以指定从第几条开始,如3,8(表示共调用8条,从第3条开始) * order:排序方式,如按posts表里的post_date字段倒序排列:posts.post_date desc * where:查询条件,字符串形式,和sql语句一样,请在事先做好安全过滤,最好使用第二个参数$where的数组形式进行过滤,此方法查询时关联多个表,所以最好指定一下表名,以防字段冲突* @param array $where 查询条件(只支持数组),格式和thinkphp的where方法一样,此方法查询时关联多个表,所以最好指定一下表名,以防字段冲突; * */ public static function postsNotPaged($tag,$where=array()){ $content=self::posts($tag,$where); return $content['posts']; } /** * 功能:根据分类文章分类ID 获取该分类下所有文章(包含子分类中文章) * 注:此方法查询时关联三个表term_relationships,posts,users;在指定查询字段(field),排序(order),指定查询条件(where)最好指定一下表名 * @author labulaka 2014-11-09 14:30:49 * @param int $term_id 文章分类ID. * @param string $tag
查询标签,以字符串方式传入 * 例:"cid:1,2;field:posts.post_title,posts.post_content;limit:0,8;order:post_date desc,listorder desc;where:id>0;" * ids:文章id,可以指定一个或多个文章id,以英文逗号分隔,如1或1,2,3 * cid:文章所在分类,可指定一个或多个分类id,以英文逗号分隔,如1或1,2,3 默认值为全部 * field:调用指定的字段 * 如只调用posts表里的id和post_title字段可以是field:posts.id,posts.post_title; 默认全部, * 此方法查询时关联三个表term_relationships,posts,users; * 所以最好指定一下表名,以防字段冲突 * limit:数据条数,默认值为10,可以指定从第几条开始,如3,8(表示共调用8条,从第3条开始) * order:排序方式,如按posts表里的post_date字段倒序排列:posts.post_date desc * where:查询条件,字符串形式,和sql语句一样,请在事先做好安全过滤,最好使用第二个参数$where的数组形式进行过滤,此方法查询时关联多个表,所以最好指定一下表名,以防字段冲突* @param array $where 查询条件(只支持数组),格式和thinkphp的where方法一样,此方法查询时关联多个表,所以最好指定一下表名,以防字段冲突; */ public static function postsByTermId($term_id,$tag,$where=array()){ $term_id=intval($term_id); if(!is_array($where)){ $where=array(); } $term_ids=array(); $term_ids=M("Terms")->where("status=1 and ( term_id=$term_id OR path like '%-$term_id-%' )")->order('term_id asc')->getField('term_id',true); if(!empty($term_ids)){ $where['term_relationships.term_id']=array('in',$term_ids); } $content=self::posts($tag,$where); return $content['posts']; } /** * 功能:查询文章列表,支持分页;
查询标签,以字符串方式传入 * 例:"cid:1,2;field:posts.post_title,posts.post_content;limit:0,8;order:post_date desc,listorder desc;where:id>0;" * ids:文章id,可以指定一个或多个文章id,以英文逗号分隔,如1或1,2,3 * cid:文章所在分类,可指定一个或多个分类id,以英文逗号分隔,如1或1,2,3 默认值为全部 * field:调用指定的字段 * 如只调用posts表里的id和post_title字段可以是field:posts.id,posts.post_title; 默认全部, * 此方法查询时关联三个表term_relationships,posts,users; * 所以最好指定一下表名,以防字段冲突 * limit:数据条数,默认值为10,可以指定从第几条开始,如3,8(表示共调用8条,从第3条开始) * order:排序方式,如按posts表里的post_date字段倒序排列:posts.post_date desc * where:查询条件,字符串形式,和sql语句一样,请在事先做好安全过滤,最好使用第二个参数$where的数组形式进行过滤,此方法查询时关联多个表,所以最好指定一下表名,以防字段冲突* @param int $pagesize 每页条数,为0,false表示不分页 * @param string $pagetpl 以字符串方式传入,例:"{first}{prev}{liststart}{list}{listend}{next}{last}" * @return array 包括分页的文章列表
* 格式: * array( * "posts"=>array(),//文章列表,array * "page"=>""//生成的分页html,不分页则没有此项 * "count"=>100 //符合条件的文章总数,不分页则没有此项 * )*/ public static function postsPaged($tag,$pagesize=20,$pagetpl=''){ return self::posts($tag,array(),$pagesize,$pagetpl); } /** * 根据分类文章分类ID 获取该分类下所有文章(包含子分类中文章),已经分页 * 注:此方法查询时关联三个表term_relationships,posts,users;在指定查询字段(field),排序(order),指定查询条件(where)最好指定一下表名 * @author labulaka 2014-11-09 14:30:49 * @param int $cid 文章分类ID. * @param string $tag
查询标签,以字符串方式传入 * 例:"cid:1,2;field:posts.post_title,posts.post_content;limit:0,8;order:post_date desc,listorder desc;where:id>0;" * ids:文章id,可以指定一个或多个文章id,以英文逗号分隔,如1或1,2,3 * cid:文章所在分类,可指定一个或多个分类id,以英文逗号分隔,如1或1,2,3 默认值为全部 * field:调用指定的字段 * 如只调用posts表里的id和post_title字段可以是field:posts.id,posts.post_title; 默认全部, * 此方法查询时关联三个表term_relationships,posts,users; * 所以最好指定一下表名,以防字段冲突 * limit:数据条数,默认值为10,可以指定从第几条开始,如3,8(表示共调用8条,从第3条开始) * order:排序方式,如按posts表里的post_date字段倒序排列:posts.post_date desc * where:查询条件,字符串形式,和sql语句一样,请在事先做好安全过滤,最好使用第二个参数$where的数组形式进行过滤,此方法查询时关联多个表,所以最好指定一下表名,以防字段冲突* @param int $pagesize 每页条数. * @param string $pagetpl 以字符串方式传入,例:"{first}{prev}{liststart}{list}{listend}{next}{last}" */ public static function postsPagedByTermId($term_id,$tag,$pagesize=20,$pagetpl=''){ $term_id=intval($term_id); $term_ids=array(); $where=array(); $term_ids=M("Terms")->field("term_id")->where("status=1 and ( term_id=$term_id OR path like '%-$term_id-%' )")->order('term_id asc')->getField('term_id',true); if(!empty($term_ids)){ $where['term_relationships.term_id']=array('in',$term_ids); } $content=self::posts($tag,$where,$pagesize,$pagetpl); return $content; } /** * 功能:根据关键字 搜索文章(包含子分类中文章),已经分页,调用方式同sp_sql_posts_paged
查询标签,以字符串方式传入 * 例:"cid:1,2;field:posts.post_title,posts.post_content;limit:0,8;order:post_date desc,listorder desc;where:id>0;" * ids:文章id,可以指定一个或多个文章id,以英文逗号分隔,如1或1,2,3 * cid:文章所在分类,可指定一个或多个分类id,以英文逗号分隔,如1或1,2,3 默认值为全部 * field:调用指定的字段 * 如只调用posts表里的id和post_title字段可以是field:posts.id,posts.post_title; 默认全部, * 此方法查询时关联三个表term_relationships,posts,users; * 所以最好指定一下表名,以防字段冲突 * limit:数据条数,默认值为10,可以指定从第几条开始,如3,8(表示共调用8条,从第3条开始) * order:排序方式,如按posts表里的post_date字段倒序排列:posts.post_date desc * where:查询条件,字符串形式,和sql语句一样,请在事先做好安全过滤,最好使用第二个参数$where的数组形式进行过滤,此方法查询时关联多个表,所以最好指定一下表名,以防字段冲突* @param array $where 查询条件(只支持数组),格式和thinkphp的where方法一样,此方法查询时关联多个表,所以最好指定一下表名,以防字段冲突; * @param int $pagesize 每页条数. * @param string $pagetpl 以字符串方式传入,例:"{first}{prev}{liststart}{list}{listend}{next}{last}" */ public static function postsPagedByKeyword($keyword,$tag,$pagesize=20,$pagetpl=''){ $where=array(); $where['posts.post_title'] = array('like',"%$keyword%"); $content=self::posts($tag,$where,$pagesize,$pagetpl); return $content; } /** * 获取指定id的文章 * @param int $post_id posts表下的id. * @param string $tag 查询标签,以字符串方式传入,例:"field:post_title,post_content;"