于晗 發表於 2023-7-22 00:00:00

织梦CMS常用的几种字段判断输出实例详解

<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        我们在使用织梦CMS制作网站时,对于某个字段,无论是默认字段还是自定义字段,偶尔会使用一些判断语句来实现我们的需求。下边列出了几种常见的需求。以及字段的判断输出实例。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        <strong>第一种:织梦自定义字段,如果没有值的时候要显示指定的默认内容,</strong></p>
<div class="jb51code" style='margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";'>
        <pre class="brush:php;toolbar:false;">
{dede:field name='ziduan' runphp='yes'}
if(@me=='') { @me = '' ;}
else { @me = "&lt;div class='red'&gt;&lt;a href='/.@me'&gt;此处是自定义的内容&lt;/a&gt;&lt;/div&gt;"; }
{/dede:field}</pre>
</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        <strong>第二种:DedeCMS判断简略标题为空时则显示完整标题</strong></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        方法1,适用于内容页</p>
<div class="jb51code" style='margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";'>
        <pre class="brush:php;toolbar:false;">
{dede:field name='array' runphp='yes'}
if (@me['shorttitle']=='')
@me=@me['title'];

else
@me=@me['shorttitle'];
{/dede:field}</pre>
</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        方法2,适用于列表页</p>
<div class="jb51code" style='margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";'>
        <pre class="brush:php;toolbar:false;">

if (@me['shorttitle']=='') @me=@me['title'];
else @me=@me['shorttitle'];
</pre>
</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        <strong>第三种:对某一个字段多重判断</strong></p>
<div class="jb51code" style='margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";'>
        <pre class="brush:php;toolbar:false;">

if(@me['risklevel']=="HR")@me="HR.png";
else if(@me['risklevel']=="D")@me="D.png";
else if(@me['risklevel']=="AA")@me="AA.png";


</pre>
</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        以上意思就是说如果查询得到的风险等级字段的取值是HR,那么将输出HR.png, 如果查询得到的风险等级字段的取值是D,那么将输出D.png,如果查询得到的风险等级字段的取值是AA,那么将输出AA.png, 这里的HR.png,只是一个字符串,代表<u>图片</u>文件所在的位置,你可以设置成任意的图片路径,上面的代码是一种分支条件输出语句,@me代表的就是输出变量,刚进入标签field:array时,@me代表数据库的一条记录,其数据类型是数组,我们根据其中risklevle的取值来判断,从而将@me设置成不同的值,当退出标签field:array时,@me的值就会被显示在原位置。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        <strong>第四种:判断过程中需要调用别的字段。</strong></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        需要在模板里做一个小判断,如果文章的价格为0时直接输出其下载地址,否则直接输出购买链接,那么这个时候肯定是需要运行runphp='yes'这个功能了,下面我们来看下代码:<br>
        经过搜索发现两个方法基本上可以实现</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        1、首先把需要调用其它字段的值放在公共变量里,然后再直接调用公共变量的值,看代码</p>
<div class="jb51code" style='margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";'>
        <pre class="brush:php;toolbar:false;">
{dede:php}$GLOBALS['title']=$arc-&gt;Fields['title']{/dede:php}
{dede:php}$GLOBALS['baidupan']=$arc-&gt;Fields['baidupan']{/dede:php}
{dede:field.price runphp="yes"}
if(@me="0")
@me = "下载地址:".$GLOBALS['baidupan'];
else
@me = "商品(".$GLOBALS['title'].")购买链接为:XXX";
{/dede:field.xxxx}</pre>
</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        2、直接使用name='array'将其它字段的值赋值到数组里,然后使用,看代码</p>
<div class="jb51code" style='margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";'>
        <pre class="brush:php;toolbar:false;">
{dede:field name='array' runphp='yes'}
if(@me['price']=='0' )
@me = '
&lt;li&gt;&lt;a class="demo" href="'.@me['baidupan'].'" target="_blank"&gt;下载:'.@me['baidupwd'].'&lt;/a&gt;&lt;/li&gt;
';
else
@me = '
&lt;div id="pdBuy" class="PDB2C_moban_warp" dataP="'.@me['title'].'" dataR="'.@me['price'].'"&gt;&lt;img src="/uploads/allimg/170702/155R26021-0.gif"&gt;&lt;/div&gt;
';
{/dede:field}</pre>
</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        上述多处需要注意符号,单引号,双引号。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。</p>
頁: [1]
查看完整版本: 织梦CMS常用的几种字段判断输出实例详解