Home > CMS Tutorial > DEDECMS > body text

How to run php script in DEDE template

藏色散人
Release: 2019-12-26 09:25:54
Original
3154 people have browsed it

How to run php script in DEDE template

How to run php script in DEDE template?

It is often necessary to directly process the underlying fields of the dede database. If there is no corresponding function in dede, then we have to use other methods to achieve it. As the title says, run the php script. and php variables, there is a good example below, interested friends can refer to

Recommended learning: 梦Weavercms

When using dede templates, often It will be necessary to directly process the underlying fields of the dede database. If there is no corresponding function in dede, we often need to find a way to deal with it.

Example: I want to take out the typeid field of a record in the data table addonimages, and then output the result of typeid multiplied by 2 in the browser. (Note: The typeid value here is 6)

At first I wrote this:

The code is as follows:

 
{dede:loop table='dede_addonimages' if='aid=94'} 
[field:typeid runphp='yes'] 
echo @me*2; 
[/field:typeid] 
{/dede:loop} 
Copy after login

The browser output is: 12 6

There is an extra 6 here. I think the reason is that [field:typeid] will execute the internal php statement first. When it reaches the line [/field:typeid], it will call the internal function and return directly [ field:typeid] The content of the underlying template, if you want to directly output 12, you can only add a custom function in the /include/extend.fuc.php file.

The code is as follows:

function abc($val){ 
return $val*2; 
}
Copy after login

Then the template is rewritten as:

The code is as follows:

 
{dede:loop table='dede_addonimages' if='aid=94'} 
[field:typeid function="abc(@me)" /] 
{/dede:loop} 
Copy after login

The output result is: 12

In addition, Note that the variables in two pieces of PHP code in the same template are not common, that is to say, the scope of a certain variable in a piece of PHP code is limited to the short code.

Example:

The code is as follows:

 
{dede:loop table='dede_addonimages' if='aid=94'} 
[field:typeid runphp='yes'] 
echo $a=@me*2; 
[/field:typeid] 
{/dede:loop} 
{dede:php}var_dump($a);{/dede:php} 
Copy after login

The output result is: 12 6 NULL

If you want to use the above php script in the subsequent php script variables, I came up with a temporary solution, which is to use global variables to solve this problem.

The code is as follows:

 
{dede:loop table='dede_addonimages' if='aid=94'} 
[field:typeid runphp='yes'] 
$GLOBALS['a']=@me*2; 
[/field:typeid] 
{/dede:loop} 
{dede:php}echo $GLOBALS['a'];{/dede:php} 
Copy after login

The output result is: 6 12 (because there is no echo in [field:typeid], so 6 is output directly)

The above is the detailed content of How to run php script in DEDE template. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!