🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 内置函数和定义函数 获取某篇文章信息 `GetOneArchive($id)` ``` <pre class="calibre14">``` <span class="token2">[</span>typeid<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">6</span> <span class="token2">[</span>click<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">219</span> <span class="token2">[</span>title<span class="token2">]</span> <span class="token">=</span><span class="token">></span> KBS <span class="token3">2</span>TV电视剧<span class="token2">[</span>适合<span class="token2">]</span> <span class="token2">[</span>writer<span class="token2">]</span> <span class="token">=</span><span class="token">></span> 管理员 <span class="token2">[</span>litpic<span class="token2">]</span> <span class="token">=</span><span class="token">></span> public<span class="token">/</span>images<span class="token">/</span><span class="token3">222.</span>jpg <span class="token2">[</span>pubdate<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">1530861461</span> <span class="token2">[</span>arcurl<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token">/</span>html<span class="token">/</span>gszx<span class="token">/</span><span class="token3">1.</span>html 静态地址 ``` ``` ip `GetIP()` 字符截取 `cn_substr($str, $slen, $startdd=0)`字符截取 格式化时间`MyDate('Y-m-d H:i:s',@me)` 转换时间戳 GetMkTime(@me) 友好时间 FloorTime(@me) {field:description function='Html2Text(@me)'/} {field:body function=Text2Html(@me)'/} GetEditor('content','默认值') 编辑器 显示信息 跳转地址 ShowMsg($msg, $gourl) GetTopTypename($typeid) 顶级栏目名 GetChannelArcpic($typeid)顶级栏目图片 自定义函数 ``` <pre class="calibre14">``` <span class="token5">if</span> <span class="token2">(</span><span class="token">!</span><span class="token1">function_exists</span><span class="token2">(</span><span class="token4">'dump'</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">function</span> <span class="token1">dump</span><span class="token2">(</span>$arr<span class="token2">)</span><span class="token2">{</span> echo <span class="token4">''</span><span class="token2">.</span><span class="token1">print_r</span><span class="token2">(</span>$arr<span class="token2">,</span>TRUE<span class="token2">)</span><span class="token2">.</span><span class="token4">''</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token6">//读取文章内容,传入id,</span> <span class="token5">function</span> <span class="token1">get_body</span><span class="token2">(</span>$id<span class="token2">,</span>$len<span class="token">=</span><span class="token3">0</span><span class="token2">)</span><span class="token2">{</span> global $db<span class="token2">;</span> $rs <span class="token">=</span> $db<span class="token">-</span><span class="token">></span><span class="token1">get</span><span class="token2">(</span><span class="token4">'addonarticle'</span><span class="token2">,</span><span class="token1">array</span><span class="token2">(</span><span class="token4">'aid'</span><span class="token">=</span><span class="token">></span>$id<span class="token2">)</span><span class="token2">,</span><span class="token1">array</span><span class="token2">(</span><span class="token4">'body'</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">;</span> <span class="token5">if</span><span class="token2">(</span>$len<span class="token">==</span><span class="token3">0</span><span class="token2">)</span> <span class="token2">{</span> $rs <span class="token">=</span> <span class="token1">empty</span><span class="token2">(</span>$rs<span class="token2">[</span><span class="token4">'body'</span><span class="token2">]</span><span class="token2">)</span> <span class="token">?</span><span class="token4">""</span><span class="token2">:</span> <span class="token1">html2text</span><span class="token2">(</span><span class="token1">trim</span><span class="token2">(</span>$rs<span class="token2">[</span><span class="token4">'body'</span><span class="token2">]</span><span class="token2">)</span> <span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span><span class="token2">{</span> $rs <span class="token">=</span> <span class="token1">empty</span><span class="token2">(</span>$rs<span class="token2">[</span><span class="token4">'body'</span><span class="token2">]</span><span class="token2">)</span> <span class="token">?</span><span class="token4">""</span><span class="token2">:</span><span class="token1">cn_substr</span><span class="token2">(</span> <span class="token1">html2text</span><span class="token2">(</span><span class="token1">trim</span><span class="token2">(</span>$rs<span class="token2">[</span><span class="token4">'body'</span><span class="token2">]</span> <span class="token2">)</span><span class="token2">)</span><span class="token2">,</span>$len<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">return</span> $rs<span class="token2">;</span> <span class="token2">}</span> <span class="token6">//字符截取</span> <span class="token5">if</span> <span class="token2">(</span> <span class="token">!</span><span class="token1">function_exists</span><span class="token2">(</span><span class="token4">'cutstr'</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">function</span> <span class="token1">cutstr</span><span class="token2">(</span>$str<span class="token2">,</span> $slen<span class="token2">,</span> $sq<span class="token">=</span><span class="token4">"..."</span><span class="token2">,</span>$startdd<span class="token">=</span><span class="token3">0</span><span class="token2">)</span> <span class="token2">{</span> $sq <span class="token">=</span> <span class="token1">strlen</span><span class="token2">(</span>$str<span class="token2">)</span><span class="token">></span>$slen <span class="token">?</span> <span class="token4">"..."</span> <span class="token2">:</span> <span class="token4">""</span><span class="token2">;</span> $text <span class="token">=</span> <span class="token1">cn_substr_utf8</span><span class="token2">(</span>$str<span class="token2">,</span>$slen<span class="token2">,</span>$startdd<span class="token2">)</span><span class="token2">.</span>$sq<span class="token2">;</span> <span class="token5">return</span> $text<span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token6">//是否有子栏目</span> <span class="token5">function</span> <span class="token1">is_subnav</span><span class="token2">(</span>$typeid<span class="token2">,</span>$echo1<span class="token">=</span><span class="token4">''</span><span class="token2">,</span>$echo2<span class="token">=</span><span class="token4">''</span><span class="token2">)</span><span class="token2">{</span> global $dsql<span class="token2">;</span> $sql <span class="token">=</span> <span class="token4">"SELECT id From `#@__arctype` WHERE reid=$typeid And ishidden<>1 order by sortrank asc limit 0, 10"</span><span class="token2">;</span> $row <span class="token">=</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">GetOne</span><span class="token2">(</span>$sql<span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> <span class="token1">is_array</span><span class="token2">(</span>$row<span class="token2">)</span><span class="token">?</span>$echo1<span class="token2">:</span>$echo2<span class="token2">;</span> <span class="token2">}</span> <span class="token6">/** * 分页时候计算记录总条数 * @param $table 表名称,不要带前缀 * @param string $where 查询条件 需要带 where * $psize 每页显示数据 * {dede:field.typeid function="page_count('zhaopin','where typeid=16 ')"/} */</span> <span class="token5">function</span> <span class="token1">page_count</span><span class="token2">(</span>$table<span class="token2">,</span>$where<span class="token">=</span><span class="token4">' '</span><span class="token2">,</span>$psize<span class="token">=</span><span class="token3">6</span><span class="token2">)</span><span class="token2">{</span> global $db<span class="token2">;</span> $where <span class="token">=</span> <span class="token2">(</span><span class="token1">empty</span><span class="token2">(</span>$where<span class="token2">)</span><span class="token2">)</span><span class="token">?</span> <span class="token4">" 1=1"</span><span class="token2">:</span><span class="token4">" typeid=$where"</span><span class="token2">;</span> $row <span class="token">=</span>$db<span class="token">-</span><span class="token">></span><span class="token1">fetchcolumn</span><span class="token2">(</span><span class="token4">"select count(*) from "</span><span class="token2">.</span>$db<span class="token">-</span><span class="token">></span><span class="token1">tablename</span><span class="token2">(</span>$table<span class="token2">)</span><span class="token2">.</span><span class="token4">" where $where"</span><span class="token2">)</span><span class="token2">;</span> $maxpage <span class="token">=</span> <span class="token1">ceil</span><span class="token2">(</span>$row<span class="token">/</span>$psize<span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> $maxpage<span class="token2">;</span> <span class="token2">}</span> <span class="token6">/** * @param $aid 图片集id * num 显示数量 * @return string */</span> <span class="token5">function</span> <span class="token1">get_imgs</span><span class="token2">(</span>$aid<span class="token2">,</span>$num <span class="token">=</span> <span class="token3">0</span><span class="token2">)</span><span class="token2">{</span> global $dsql<span class="token2">;</span> $imgurls <span class="token">=</span> <span class="token4">''</span><span class="token2">;</span> $row <span class="token">=</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">GetOne</span><span class="token2">(</span><span class="token4">"Select imgurls,typeid From #@__addonimages where aid=$aid order by aid desc"</span><span class="token2">)</span><span class="token2">;</span> $imgurls <span class="token">=</span> $row<span class="token2">[</span><span class="token4">'imgurls'</span><span class="token2">]</span><span class="token2">;</span> <span class="token1">preg_match_all</span><span class="token2">(</span><span class="token4">"/{dede:img (.*)}(.*){\/dede:img/isU"</span><span class="token2">,</span> $imgurls<span class="token2">,</span> $wordcount<span class="token2">)</span><span class="token2">;</span> <span class="token1">preg_match_all</span><span class="token2">(</span><span class="token4">"/text='(.*?)' width=/is"</span><span class="token2">,</span> $imgurls<span class="token2">,</span> $wordcount1<span class="token2">)</span><span class="token2">;</span><span class="token6">//$wordcount1[1][$i] 图片描述</span> $typename <span class="token">=</span> <span class="token1">get_chanel_name</span><span class="token2">(</span>$row<span class="token2">[</span><span class="token4">'typeid'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $typelink <span class="token">=</span> <span class="token1">get_chanel_link</span><span class="token2">(</span>$row<span class="token2">[</span><span class="token4">'typeid'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $title<span class="token">=</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">GetOne</span><span class="token2">(</span><span class="token4">"Select title From #@__archives where id=$aid"</span><span class="token2">)</span><span class="token2">;</span> $count <span class="token">=</span> <span class="token1">count</span><span class="token2">(</span>$wordcount<span class="token2">[</span><span class="token3">2</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $num <span class="token">=</span> <span class="token2">(</span>$num<span class="token">==</span><span class="token3">0</span> <span class="token">||</span> $num<span class="token">></span>$count<span class="token2">)</span><span class="token">?</span>$count<span class="token2">:</span>$num<span class="token2">;</span> $imglist<span class="token">=</span><span class="token4">''</span><span class="token2">;</span> <span class="token5">for</span><span class="token2">(</span>$i <span class="token">=</span> <span class="token3">0</span><span class="token2">;</span>$i <span class="token"><</span> $num<span class="token2">;</span>$i<span class="token">++</span><span class="token2">)</span><span class="token2">{</span> $imglist<span class="token2">.</span><span class="token">=</span>" <span class="token"><</span>li class<span class="token">=</span><span class="token4">'is-visible'</span><span class="token">></span> <span class="token"><</span>div class<span class="token">=</span><span class="token4">'cd-half-block image'</span> style<span class="token">=</span><span class="token4">'background-image:url(".$wordcount[2][$i].")'</span><span class="token">></span><span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span>div class<span class="token">=</span><span class="token4">'cd-half-block content'</span><span class="token">></span> <span class="token"><</span>div<span class="token">></span> <span class="token"><</span>h2<span class="token">></span><span class="token"><</span>a href<span class="token">=</span><span class="token4">'".$typelink."'</span><span class="token">></span><span class="token4">".$typename."</span><span class="token"><</span><span class="token">/</span>a<span class="token">></span><span class="token"><</span><span class="token">/</span>h2<span class="token">></span> <span class="token"><</span>p<span class="token">></span>产品名称<span class="token2">:</span> <span class="token4">".$title['title']."</span> <span class="token"><</span><span class="token">/</span>p<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">/</span>li<span class="token">></span>"<span class="token2">;</span> <span class="token2">}</span> <span class="token5">return</span> $imglist<span class="token2">;</span> <span class="token2">}</span> <span class="token6">/** * 返回顶级栏目图片,用于列表页,任何时候都只返回顶栏目级图片 * @param $typeid * @param string $default_pic * @return string */</span> <span class="token5">function</span> <span class="token1">get_topchannel_pic</span><span class="token2">(</span>$typeid<span class="token2">,</span>$default_pic<span class="token">=</span><span class="token4">"/images/defaultpic.gif"</span><span class="token2">)</span> <span class="token2">{</span> global $dsql<span class="token2">;</span> $row <span class="token">=</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">GetOne</span><span class="token2">(</span><span class="token4">"SELECT topid,arcpic FROM #@__arctype WHERE id= $typeid"</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span><span class="token2">(</span><span class="token3">0</span> <span class="token">==</span> $row<span class="token2">[</span><span class="token4">'topid'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">return</span> <span class="token2">(</span><span class="token1">empty</span><span class="token2">(</span>$row<span class="token2">[</span><span class="token4">'arcpic'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">)</span><span class="token">?</span>$default_pic<span class="token2">:</span>$row<span class="token2">[</span><span class="token4">'arcpic'</span><span class="token2">]</span><span class="token2">;</span> <span class="token2">}</span><span class="token5">else</span><span class="token2">{</span> $row1 <span class="token">=</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">GetOne</span><span class="token2">(</span><span class="token4">"SELECT arcpic FROM #@__arctype WHERE id= $row[topid]"</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> <span class="token2">(</span><span class="token1">empty</span><span class="token2">(</span>$row1<span class="token2">[</span><span class="token4">'arcpic'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">)</span><span class="token">?</span>$default_pic<span class="token2">:</span>$row1<span class="token2">[</span><span class="token4">'arcpic'</span><span class="token2">]</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token6">/* * 获取本栏目图片,只有找不到时候才返回顶级图片 * $typeid 栏目id * $default_pic 找不到时候默认图片 * [field:id function='get_channel_pic(@me)'/] * */</span> <span class="token5">function</span> <span class="token1">get_channel_pic</span><span class="token2">(</span>$typeid<span class="token2">,</span>$default_pic<span class="token">=</span><span class="token4">"/images/defaultpic.gif"</span><span class="token2">)</span> <span class="token2">{</span> global $dsql<span class="token2">;</span> $row <span class="token">=</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">GetOne</span><span class="token2">(</span><span class="token4">"SELECT arcpic,topid FROM #@__arctype WHERE id= $typeid"</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span><span class="token2">(</span><span class="token1">empty</span><span class="token2">(</span>$row<span class="token2">[</span><span class="token4">'arcpic'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">if</span><span class="token2">(</span><span class="token3">0</span> <span class="token">==</span> $row<span class="token2">[</span><span class="token4">'topid'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">return</span> $default_pic<span class="token2">;</span> <span class="token2">}</span><span class="token5">else</span><span class="token2">{</span> $row1 <span class="token">=</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">GetOne</span><span class="token2">(</span><span class="token4">"SELECT arcpic FROM #@__arctype WHERE id= $row[topid]"</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> <span class="token2">(</span><span class="token1">empty</span><span class="token2">(</span>$row1<span class="token2">[</span><span class="token4">'arcpic'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">)</span><span class="token">?</span>$default_pic<span class="token2">:</span>$row1<span class="token2">[</span><span class="token4">'arcpic'</span><span class="token2">]</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span><span class="token5">else</span><span class="token2">{</span> <span class="token5">return</span> $row<span class="token2">[</span><span class="token4">'arcpic'</span><span class="token2">]</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token6">/** * 根据id获取栏目名称 * @param $typeid * @return mixed */</span> <span class="token5">function</span> <span class="token1">get_chanel_name</span><span class="token2">(</span>$typeid<span class="token2">)</span> <span class="token2">{</span> global $dsql<span class="token2">;</span> $row <span class="token">=</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">GetOne</span><span class="token2">(</span><span class="token4">"SELECT typename FROM #@__arctype WHERE id= $typeid"</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> $row<span class="token2">[</span><span class="token4">'typename'</span><span class="token2">]</span><span class="token2">;</span> <span class="token2">}</span> <span class="token6">/* * 获取栏目连接,根据typeid */</span> <span class="token5">function</span> <span class="token1">get_chanel_link</span><span class="token2">(</span>$typeid<span class="token2">)</span><span class="token2">{</span> global $dsql <span class="token2">;</span> $typename <span class="token">=</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">GetOne</span><span class="token2">(</span><span class="token4">"SELECT typedir FROM #@__arctype where id=$typeid"</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> <span class="token1">str_replace</span><span class="token2">(</span><span class="token4">"{cmspath}"</span><span class="token2">,</span><span class="token4">''</span><span class="token2">,</span>$typename<span class="token2">[</span><span class="token4">'typedir'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token6">/*栏目描述*/</span> <span class="token5">function</span> <span class="token1">get_channel_desc</span><span class="token2">(</span>$tid<span class="token2">)</span><span class="token2">{</span> global $db<span class="token2">;</span> <span class="token5">return</span> $db<span class="token">-</span><span class="token">></span><span class="token1">getcolumn</span><span class="token2">(</span><span class="token4">'arctype'</span><span class="token2">,</span><span class="token1">array</span><span class="token2">(</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span>$tid<span class="token2">)</span><span class="token2">,</span><span class="token4">'description'</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` 无限极栏目调用 /\* - $channel 要调用的栏目id {dede:tagname runphp='yes'} @me =menu(2); {/dede:tagname} \*/ function menu($channel=0,$line=100) { global $dsql,$result; $line = empty($line)?10 : $line; $dsql->SetQuery("SELECT \* FROM #@\_\_arctype WHERE reid='$channel' And ishidden<>1 order by sortrank asc limit 0, $line "); $dsql->Execute($channel); if($dsql->GetTotalRow($channel)>0) { $result .= " "; while($row = $dsql->GetArray($channel)) { $id = $row\['id'\]; $typename = $row\['typename'\]; $typelink = GetOneTypeUrlA($row); ``` <pre class="calibre14">``` $result <span class="token2">.</span><span class="token">=</span> <span class="token4">"<li>"</span><span class="token2">;</span> $result <span class="token2">.</span><span class="token">=</span> <span class="token4">"<a href='{$typelink}' >{$typename}</a>"</span><span class="token2">;</span> <span class="token1">menu</span><span class="token2">(</span>$id<span class="token2">,</span>$line<span class="token2">)</span><span class="token2">;</span> $result <span class="token2">.</span><span class="token">=</span> <span class="token4">"</li>"</span><span class="token2">;</span> <span class="token2">}</span> $result <span class="token2">.</span><span class="token">=</span> <span class="token4">"</ul>"</span><span class="token2">;</span> ``` ``` } return $result; }