dedecms在模板中操作数据库可以使用{dede:field}{/dede:field}标签,也可以使用{dede:php}{/dede:php}标签。
下面就用两个例子给大家讲解这两个标签的用法。
示例一:下面代码的意思是,查询当前栏目是否有子级栏目,如果有就输出'<i class="fa fa-caret-down"></i>',如果没有就输出空。
{dede:field name=typeid runphp="yes"}
global $dsql;
$sql = "SELECT id From `dede_arctype` WHERE reid='@me' And ishidden<>1 order by sortrank asc limit 0, 100 ";
$row = $dsql->GetOne($sql);
@me=is_array($row)?'<i class="fa fa-caret-down"></i>':'';
{/dede:field}
global $dsql;
$sql = "SELECT id From `dede_arctype` WHERE reid='@me' And ishidden<>1 order by sortrank asc limit 0, 100 ";
$row = $dsql->GetOne($sql);
@me=is_array($row)?'<i class="fa fa-caret-down"></i>':'';
{/dede:field}
示例二:要在模板中使用{dede:php}标签,就必须在后台-系统设置-系统基本参数-其它选项中把模板引擎禁用标签php删掉。
下面代码的意思就是,判断当前栏目是否有子栏目,如果有子栏目,就把子栏目输出。
{dede:php}
$id = $refObj->Fields['id'];
$sql = ("SELECT * FROM dede_arctype where reid ='$id'");
$db->SetQuery($sql);
$db->Execute();
if($db->GetTotalRow()){
echo '<ul class="sub-menu dr-menu2">';
while($row = $db->GetArray()){
$url = str_replace('{cmspath}','',($row[typedir]));
echo "<li class='Lev2'><a class='menu2' href=".$url.">".($row[typename])."</a></li>";
};
echo '</ul>';
};
{/dede:php}
$id = $refObj->Fields['id'];
$sql = ("SELECT * FROM dede_arctype where reid ='$id'");
$db->SetQuery($sql);
$db->Execute();
if($db->GetTotalRow()){
echo '<ul class="sub-menu dr-menu2">';
while($row = $db->GetArray()){
$url = str_replace('{cmspath}','',($row[typedir]));
echo "<li class='Lev2'><a class='menu2' href=".$url.">".($row[typename])."</a></li>";
};
echo '</ul>';
};
{/dede:php}
上面两个例子组合起来,可以写成一个导航,当前导航添加高亮样式,导航如果有子栏目就输出子栏目
<nav id="mainnav" class="mainnav">
<ul class="menu">
<li {dede:field name=typeid runphp="yes"}(@me=="")? @me=" class='Lev1 active' ":@me="class='Lev1'";{/dede:field}> <a href="/" class="menu1">网站首页 </a></li>
{dede:channelartlist typeid='top' currentstyle='active'}
<li class="Lev1 {dede:field.currentstyle/}"> <a href="{dede:field name='typeurl'/}" class="menu1">{dede:field name='typename'/}
{dede:field name=typeid runphp="yes"}
global $dsql;
$sql = "SELECT id From `dede_arctype` WHERE reid='@me' And ishidden<>1 order by sortrank asc limit 0, 100 ";
$row = $dsql->GetOne($sql);
@me=is_array($row)?'<i class="fa fa-caret-down"></i>':'';
{/dede:field}</a> {dede:php}
$id = $refObj->Fields['id'];
$sql = ("SELECT * FROM dede_arctype where reid ='$id'");
$db->SetQuery($sql);
$db->Execute();
if($db->GetTotalRow()){
echo '
<ul class="sub-menu dr-menu2">
';
while($row = $db->GetArray()){
$url = str_replace('{cmspath}','',($row[typedir]));
echo "
<li class='Lev2'><a class='menu2' href=".$url.">".($row[typename])."</a></li>
";
};
echo '
</ul>
';
};
{/dede:php} </li>
{/dede:channelartlist}
</ul>
</nav>
<ul class="menu">
<li {dede:field name=typeid runphp="yes"}(@me=="")? @me=" class='Lev1 active' ":@me="class='Lev1'";{/dede:field}> <a href="/" class="menu1">网站首页 </a></li>
{dede:channelartlist typeid='top' currentstyle='active'}
<li class="Lev1 {dede:field.currentstyle/}"> <a href="{dede:field name='typeurl'/}" class="menu1">{dede:field name='typename'/}
{dede:field name=typeid runphp="yes"}
global $dsql;
$sql = "SELECT id From `dede_arctype` WHERE reid='@me' And ishidden<>1 order by sortrank asc limit 0, 100 ";
$row = $dsql->GetOne($sql);
@me=is_array($row)?'<i class="fa fa-caret-down"></i>':'';
{/dede:field}</a> {dede:php}
$id = $refObj->Fields['id'];
$sql = ("SELECT * FROM dede_arctype where reid ='$id'");
$db->SetQuery($sql);
$db->Execute();
if($db->GetTotalRow()){
echo '
<ul class="sub-menu dr-menu2">
';
while($row = $db->GetArray()){
$url = str_replace('{cmspath}','',($row[typedir]));
echo "
<li class='Lev2'><a class='menu2' href=".$url.">".($row[typename])."</a></li>
";
};
echo '
</ul>
';
};
{/dede:php} </li>
{/dede:channelartlist}
</ul>
</nav>