在Phpcms V9常规的数据调用中有按列表调用(action="lists")、按推荐位调用(action="position")、按浏览排行调用(action="hits")、按相关文章调用(action="relation")(相关文章:Phpcms V9在新建模型中添加相关文章功能)等,今天CMSYOU在这里与大家分享最近在实际项目中自定义的一种调用数据的方法:自定义PC标签按模型调用数据。
首先来分析下常规的按栏目调用的PC语句:
{pc:content action="lists" catid="$catid" num="25" order="id DESC" page="$page"}<ul class="list lh24 f14">{loop $data $r}<li><span class="rt">{date('Y-m-d H:i:s',$r[inputtime])}</span>路<a href="{$r[url]}" target="_blank"{title_style($r[style])}>{$r[title]}</a></li>{if $n%5==0}<li class="bk20 hr"></li>{/if}{/loop}</ul><div id="pages" class="text-c">{$pages}</div>{/pc}
以上是调用栏目id为$catid栏目的数据,25篇为一页。
这里使用的action="lists"实际上是phpcms\modules\content\classes\content_tag.class.php这个文件里边的调用方法控制的:
/*** 列表页标签* @param $data*/public function lists($data) {$catid = intval($data['catid']);if(!$this->set_modelid($catid)) return false;if(isset($data['where'])) {$sql = $data['where'];} else {$thumb = intval($data['thumb']) ? " AND thumb != ''" : '';if($this->category[$catid]['child']) {$catids_str = $this->category[$catid]['arrchildid'];$pos = strpos($catids_str,',')+1;$catids_str = substr($catids_str, $pos);$sql = "status=99 AND catid IN ($catids_str)".$thumb;} else {$sql = "status=99 AND catid='$catid'".$thumb;}}$order = $data['order'];$return = $this->db->select($sql, '*', $data['limit'], $order, '', 'id');//调用副表的数据if (isset($data['moreinfo']) && intval($data['moreinfo']) == 1) {$ids = array();foreach ($return as $v) {if (isset($v['id']) && !empty($v['id'])) {$ids[] = $v['id'];} else {continue;}}if (!empty($ids)) {$this->db->table_name = $this->db->table_name.'_data';$ids = implode('\',\'', $ids);$r = $this->db->select("`id` IN ('$ids')", '*', '', '', '', 'id');if (!empty($r)) {foreach ($r as $k=>$v) {if (isset($return[$k])) $return[$k] = array_merge($v, $return[$k]);}}}}return $return;}
由此方法延伸,自定义出来按模型调用数据的方法:
/*** 根据频道调用列表页标签 cmsyou* @param $data*/public function lists_by_modelid($data) {$modelid = intval($data['modelid']);$siteids = getcache('category_content','commons');if(!$modelid) return false;$this->modelid = $modelid;$this->db->set_model($this->modelid);$this->tablename = $this->db->table_name;if(isset($data['where'])) {$sql = $data['where'];} else {$thumb = intval($data['thumb']) ? " AND thumb != ''" : '';$sql = "status=99".$thumb;}$order = $data['order'];$return = $this->db->select($sql, '*', $data['limit'], $order, '', 'id');//调用副表的数据if (isset($data['moreinfo']) && intval($data['moreinfo']) == 1) {$ids = array();foreach ($return as $v) {if (isset($v['id']) && !empty($v['id'])) {$ids[] = $v['id'];} else {continue;}}if (!empty($ids)) {$this->db->table_name = $this->db->table_name.'_data';$ids = implode('\',\'', $ids);$r = $this->db->select("`id` IN ('$ids')", '*', '', '', '', 'id');if (!empty($r)) {foreach ($r as $k=>$v) {if (isset($return[$k])) $return[$k] = array_merge($v, $return[$k]);}}}}return $return;}
那么具体在前台模板调用的语句则是:
{pc:content action="lists_by_modelid" modelid="1" num="5" thumb="1" order="rand()" return="hitspic"}{loop $hitspic $r}<li class="related hasimage"><a class="img" href="{$r[url]}" title="{$r[title]}" target="_blank"><img src="{thumb($r[thumb],210,147)}" width="100" height="70"></a><a class="text" href="{$r[url]}" title="{$r[title]}" target="_blank">{$r[title]}</a></li>{/loop}{/pc}
上面的语句是调用模型id为1的模型数据,每5篇为一页,必须缩略图,排序为随机。
最后,欢迎大家收听CMSYOU官方微博,看完CMSYOU思优团队的文章有疑问,那就在下面留言探讨吧!
点击加载更多