Home >Backend Development >PHP Tutorial >How to use php tags with sql in dede

How to use php tags with sql in dede

墨辰丷
墨辰丷Original
2018-05-15 16:20:011616browse

This article mainly introduces how to use php tags with sql in dede. Friends who are interested can refer to it. I hope it will be helpful to everyone.

  {dede:php}  
    //echo 'vvvv';  
    {/dede:php}   
      
{dede:php}  
  
//print_r($refObj->Fields);  
$sql = "select ars.*, art.typedir from dede_archives AS ars   
        LEFT JOIN dede_arctype AS art ON ars.typeid=art.id   
        where typeid=2 ORDER BY weight ASC ";  
$dsql->SetQuery($sql);//将SQL查询语句格式化  
$dsql->Execute();//执行SQL操作  
//通过循环输出执行查询中的结果  
while($row = $dsql->GetArray()){  
    $link   = $refObj->Fields['indexurl'].'/'.$row['typedir'].'/'.$row['id'];  
    $link   = str_replace('{cmspath}', '', $link);  
    $class  = "ahy";  
    if($refObj->Fields['id'] == $row['id']) $class="ahytt";  
  
    echo &#39;<a href="&#39;.$link.&#39;.html" class="&#39;.$class.&#39;">&#39;;  
    echo $row[&#39;title&#39;];  
    echo &#39;</a> &#39;;  
}  
{/dede:php}


{dede:sql} label usage

sql label can be called a universal Tag, query the database and output it. Here are some usages of this tag:

1. It is used to output statistical content. This is good. For example, let’s count how many messages have been sent in total. Article, the idea is to output the total number of additional table contents of the article dede_addonarticle.

{dede:sql sql="SELECT COUNT(*) AS nums FROM dede_addonarticle"}  
  
[field:name = "nums"/]  
  
{/dede:sql}

2. Use ~field~ to conduct special queries. For example, many members on the forum previously needed to output relevant content of members who are currently publishing articles. This was not possible with sql or arclist tags before, because The content of each article is different. Here we will use the 5.5 SQL statement as a demonstration.

We first add the following tag to the corresponding position in the article template

 {dede:sql sql="SELECT * FROM dede_archives WHERE writer=~writer~"}  
  [field:id/], DEDE模板   
  {/dede:sql}

Put this on the article_article.htm page, which will retrieve relevant articles published by the current member, here~ writer ~ will be replaced according to the environment variables of the current content before executing the query.

The ~writer~ that appears in the conditional query in the SQL statement, that is, the relevant content in $refObj->Fields[$value]

Use PHP in the template

Many people hope that Dreamweaver’s template tags can be more flexible and add the function of running PHP. Here, DedeCMS V5.5 has added a special tag {dede:php}{/dede:php} that can execute PHP. This tag can execute PHP statements.

Here are a few commonly used examples:

1. The simplest output content:

  {dede:php}  
  $numA = 1;  
  $numB = 2;  
  echo $numA + $numB;  
   
  {/dede:php}

If you want to output information in {dede:php}, you can use it directly Print, echo and the like are printed out and assigned to @me. Invalid

The content of this output is the calculation result:

  3

2. Combined with SQL query to output a single piece of content

 {dede:php}  
  $row = $dsql->GetOne(&#39;select id,typename from dede_arctype where id=2&#39;);  
  print_r($row);  
  {/dede:php}

The content of this output is

  Array  
  (  
  [id] => 2  
  [typename] => 问答  
  )

3. Get the variables of the current page

For example, when we enter the [Template]-[Global Mark Test] in the background of the system, we enter the code Fill in the following content:

 {dede:php}  
  print_r($refObj->Fields);  
   
  {/dede:php}


If you want to get the dede field value in PHP, you can use the $refObj->Fields object to get it and include it to get title = > $refObj->Fields['title']

If the environment variable remains default, that is, "do not use environment ID", we will see the following results:

 Array  
  (  
  [typeid] => 0  
  [phpurl] => /plus  
  [indexurl] => /  
  [templeturl] => /templets  
  [memberurl] => /member  
  [specurl] => /special  
  [indexname] => 主页  
  [templetdef] => /templets/default  
  )

Then let’s change the environment variable test. Here I use my local column as the test:

 Array  
  (  
  [id] => 3  
  [reid] => 0  
  [topid] => 0  
  [sortrank] => 1  
  [typename] => 产品  
  [typedir] => {cmspath}/product  
  … …  
  [indexname] => 主页  
  [templetdef] => /templets/default  
  [position] => 主页 > 产品 >  
  [title] => 产品  
  )

In this way, the local variables of the current page can be retrieved. Next, we can combine the previous SQL statements to call different column contents based on different environment IDs.

For example:

 {dede:php}  
  $thisid = $refObj->Fields[&#39;id&#39;];  
  $row = $dsql->GetOne(&#39;select id,typename from dede_arctype where id=&#39;.$thisid);  
  print_r($row);  
  {/dede:php}

This is to call the title of the current column. The function of this label is similar to {dede:field.typename/}

Next we execute the query to sql and output variables are processed:

$dsql->Execute(&#39;me&#39;,$sql);  
while($rs = $dsql->GetArray(&#39;me&#39;))  
{  
//根据属性处理查询变量  
$rs[&#39;title&#39;] = cn_substr($rs[&#39;title&#39;], $titlelen);  
//获取底层模板  
$ctp->LoadSource($innertext);  
foreach($ctp->CTags as $tagid=>$ctag) {  
if(!empty($rs[strtolower($ctag->GetName())])) {  
$ctp->Assign($tagid,$rs[$ctag->GetName()]); DEDE模板   
}  
}  
//根据底层模板及查询变量得到处理结果  
$revalue .= $ctp->GetResult();  
}

In this way, we will replace the query results with the relevant variables that appear in the underlying template, then generate the output string, and store all the string information in $revalue .

Finally return this value return $revalue;

The content of the entire file is as follows:

if(!defined(&#39;DEDEINC&#39;))  
{  
exit("Request Error!");  
}  
function lib_writerarc(&$ctag,&$refObj)  
{  
global $dsql,$envs;  
//属性处理  
$attlist="row|12,titlelen|24";  
FillAttsDefault($ctag->CAttribute->Items,$attlist);  
extract($ctag->CAttribute->Items, EXTR_SKIP);  
$revalue = &#39;&#39;;  
$innertext = $ctag->GetInnerText();  
$ctp = new DedeTagParse();  
$ctp->SetNameSpace(&#39;field&#39;, &#39;[&#39;, &#39;]&#39;);  
$sql = "SELECT * FROM dede_archives WHERE writer=&#39;{$refObj->Fields[&#39;writer&#39;]}&#39; limit 0, $row";  
$dsql->Execute(&#39;me&#39;,$sql);  
while($rs = $dsql->GetArray(&#39;me&#39;))  
{  
//根据属性处理查询变量  
$rs[&#39;title&#39;] = cn_substr($rs[&#39;title&#39;], $titlelen);  
//获取底层模板  
$ctp->LoadSource($innertext);  
foreach($ctp->CTags as $tagid=>$ctag) {   
if(!empty($rs[strtolower($ctag->GetName())])) {  
$ctp->Assign($tagid,$rs[$ctag->GetName()]);  
}  
}  
//根 据底层模板及查询变量得到处理结果  
$revalue .= $ctp->GetResult();  
}  
return $revalue;  
}  
?>

Next we will test our tag, we modify the article_article.htm template, in it Add the following tag code:

{dede:writerarc row=&#39;10&#39; titlelen=&#39;6&#39;}  
[field:title/]   
{/dede:writerarc}

Related recommendations:

##dedecms5.7 latest sql use guestbook .php injection vulnerability

dedecms SESSION variable overwriting leads to SQL injection common.inc.php solution

PHP command injection dedecms remote writing file link example sharing


The above is the detailed content of How to use php tags with sql in dede. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn