How to use PHP tags with SQL in dede

Source: Internet
Author: User

This article mainly introduces how to use PHP tags in the Dede with SQL, interested friends to reference, I hope to help you.

  {dede:php}      Echo ' VVVV ';      {/dede:php}         {dede:php}    Print_r ($REFOBJ->fields);  $sql = "Select Ars.*, Art.typedir from Dede_archives as Ars left           joins Dede_arctype as art on Ars.typeid=art.id           whe Re typeid=2 ORDER by weight ASC ";  $dsql->setquery ($sql);//Format SQL query statements  $dsql->execute ();//Execute SQL Operations  //execute the results in the query through the loop output 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 ' <a href= '. $link. HTML "class=" '. $class. ' " > ';      echo $row [' title '];      Echo ' </a> ';  }  {/dede:php}


{Dede:sql} Usage of tags

SQL tags can be called a universal tag, query the database to output it, here are some about the use of this tag:

1. Used to output statistical content, this is good, for example, we have to count the total number of articles issued, the idea is to output dede_addonarticle This article add the total number of table content can be.

{dede:sql sql= "select COUNT (*) as Nums from Dede_addonarticle"}    [Field:name = "nums"/]    {/dede:sql}

2. Use ~field~ for special queries, such as before the forum many members need to do an output of the current post members of the relevant content, before using SQL or arclist tag is not possible, because each content is different, here we use 5.5 of SQL statements to do a demonstration.

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

{dede:sql sql= "select * from Dede_archives WHERE writer=~writer~"}    [Field:id/], dede template     {/dede:sql}

This is put on the Article_article.htm page, will retrieve the current member published related articles, where the ~ writer~ will be based on the current content of the environment variables to be replaced before the query execution.

This appears in the SQL statement of the conditional query ~ writer~, that is, $refobj->fields[$value] This inside the relevant content

Using PHP in Templates

Many people want to weave a dream template tag can be more flexible, to join the function that can run PHP, here in Dedecms V5.5 added a can execute PHP special tag {dede:php}{/dede:php}, this tag can execute PHP statement.

Here are a few common examples:

1. The simplest output content:

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

In {dede:php} want to output information can be directly used print, echo, such as printing out to assign value to @me invalid

The content of this output is the result of the calculation:

  3

2. Output a single content with SQL query

{dede:php}    $row = $dsql->getone (' Select Id,typename from Dede_arctype where id=2 ');    Print_r ($row);    {/dede:php}

The content of this output is

  Array    (    [id] + 2    [typename] = Q    )

3. Get the variables for the current page

For example, we enter the [Template]-[Global tag test] in the system background, we fill in the following content in the code:

{dede:php}    Print_r ($REFOBJ->fields);       {/dede:php}


In PHP you want to get Dede a field value can use $REFOBJ->fields object to get included in get title = = $refObj->fields[' title ']

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

Array    (    [typeid] = 0    [Phpurl] =/plus    [Indexurl] =/    [Templeturl] =/templets    [Memberurl] =/member    [Specurl] =/special    [IndexName] = home    [Templetdef] =/templets/ Default    )

Let's change the environment variable test to see, here in my local column for testing:

Array    (    [id] = 3    [Reid] = 0    [Topid] = 0    [Sortrank] + 1    [TypeName] + Products    [ Typedir] = {cmspath}/product ...    [IndexName] + home    [templetdef] [/templets/default]    [position] + Home > Products >    [title] + Products    )

This retrieves the local variables of the current page, and then we can combine the preceding SQL statements to invoke the contents of different columns according to different environment IDs.

For example:

{dede:php}    $thisid = $REFOBJ->fields[' id '];    $row = $dsql->getone (' Select Id,typename from Dede_arctype where id= '. $thisid);    Print_r ($row);    {/dede:php}

This is to invoke the title of the current column, which functions like {dede:field.typename/}

Next we process the SQL and output variables by executing a query:

$dsql->execute (' Me ', $sql);  while ($rs = $dsql->getarray (' Me '))  {  //Handle query variables based on attributes  $rs [' title '] = Cn_substr ($rs [' title '], $titlelen);  Get the underlying template  $ctp->loadsource ($innertext);  foreach ($ctp->ctags as $tagid = + $ctag) {  if (!empty ($rs [Strtolower ($ctag->getname ())])) {  $CTP Assign ($tagid, $rs [$ctag->getname ()]); Dede template   }  }  //According to the underlying template and query variables to get processing results  $revalue. = $CTP->getresult ();  }

This allows us to replace the query results with the related variables that appear in the underlying template, and then generate the output string to store all the string information in the $revalue.

Finally return this value to return $revalue;

The entire document reads as follows:

if (!defined (' Dedeinc ')) {exit ("Request error!");  } function Lib_writerarc (& $ctag,& $refObj) {global $dsql, $envs;  Property handling $attlist = "row|12,titlelen|24";  Fillattsdefault ($ctag->cattribute->items, $attlist);  Extract ($ctag->cattribute->items, Extr_skip);  $revalue = ";  $innertext = $ctag->getinnertext ();  $CTP = new Dedetagparse ();  $CTP->setnamespace (' field ', ' [', '] ');  $sql = "SELECT * from Dede_archives WHERE writer= ' {$refObj->fields[' writer '} ' limit 0, $row";  $dsql->execute (' Me ', $sql);  while ($rs = $dsql->getarray (' Me ')) {//Handle query variables based on attributes $rs [' title '] = Cn_substr ($rs [' title '], $titlelen);  Get the underlying template $CTP->loadsource ($innertext); foreach ($ctp->ctags as $tagid = + $ctag) {if (!empty ($rs [Strtolower ($ctag->getname ())])) {$CTP->assign ($  TagID, $rs [$ctag->getname ()]);  }}//According to the underlying template and query variables to get processing results $revalue. = $CTP->getresult ();  } return $revalue; }; 

Next we will test our label, we modify the article_article.htm template, add the following tag code:

{dede:writerarc row= ' titlelen= ' 6 '}  [field:title/]   {/DEDE:WRITERARC}

Related recommendations:

dedecms5.7 Latest SQL Exploits guestbook.php Injection Vulnerability

DEDECMS Session variable overlay causes SQL injection common.inc.php to be resolved

PHP command Injection dedecms remote Write file link instance sharing


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.