(1)普通方式调用
- {pc:get sql="SELECT setting FROM v9_poster WHERE spaceid = 12 AND type='images' AND disabled=0 ORDER BY listorder ASC" num="5"}
- {loop $data $r}
- <?php
- $narry = string2array($r['setting']);
- ?>
- <a href="{$narry[1][linkurl]}" target="_blank"><img src="{$narry[1][imageurl]}" alt="{$narry[1][alt]}" width="210" height="199"/></a>
- {/loop}
- {/pc}
(2)封装方式
找到phpcmsmodulescontentclassescontent_tag.class.php文件,找到list方法下新增ad方法
- /**
- * 广告列表标签
- * @param $data
- */
- public function ad($data) {
- $poster = pc_base::load_model('poster_model');
- $space = pc_base::load_model('poster_space_model');
- $spaceid = intval($data['spaceid']);
- $siteid = get_siteid();
- if(empty($spaceid)) return false;
- $r = $space->get_one(array('siteid'=>$siteid,'spaceid'=>$spaceid));
- if(!$r) exit();
- if(isset($data['where'])) {
- $sql = $data['where'];
- } else {
- $sql = "`disabled` = 0 AND `spaceid` = '$spaceid' ";
- if (isset($data['type'])){
- $type = $data['type'];
- $sql .= " AND `type` = '$type'";
- }
- $now = SYS_TIME;
- if(isset($data['time']) && $data['time'] == 1){//开启时间限制
- $sql .= " AND `startdate`<='".$now."' AND (`enddate`>='".$now."' OR `enddate`=0)";
- }
- }
- $order = $data['order'];
- $return = $poster->select($sql, '*', $data['limit'], $order, '', 'id');
- foreach ($return as $k => $v) {
- $return[$k]['setting'] = string2array($return[$k]['setting']);
- $return[$k]['linkurl'] = $return[$k]['setting'][1]['linkurl'];
- $return[$k]['imageurl'] = $return[$k]['setting'][1]['imageurl'];
- $return[$k]['alt'] = $return[$k]['setting'][1]['alt'];
- }
- return $return;
- }
前端调用方式:
- {pc:content action="ad" spaceid="11" num="10" order="listorder DESC"}
- {loop $data $r}
- <a href="{$r[linkurl]}" target="_blank" alt="{$r[alt]}">
- <div class="imgBox" style="background-image: url({$r[imageurl]})"></div>
- </a>
- {/loop}
- {/pc}
还可以添加其他参数比如:
time="1"表示开启时间限制,不满足时间段的将不显示
type="images"调用类型
where="id>10":直接where条件(where条件下其他条件将失效)
改造之后,再也不用担心记不住代码了,一劳永逸