When DEDE templates are used to run php scripts and php variables, the underlying fields of the dede database are often processed directly, if there is no corresponding function in dede, we often need to solve it. For example, I want to retrieve the typeid field of a record in the data table addonimages, and then output the result of multiplying typeid by 2 in the browser. (Note: The typeid & #20540 DEDE template shows how to run php scripts and use php variables.
When using the dede template, it is often necessary to directly process the underlying fields of the dede Database. if there is no corresponding function in the dede, we often need to work out a solution.
For example, I want to retrieve the typeid field of a record in the data table addonimages, and then output the result of multiplying typeid by 2 in the browser. (Note: The typeid value here is 6)
At first I wrote this:
{dede:loop table='dede_addonimages' if='aid=94'}[field:typeid runphp='yes']echo @me*2;[/field:typeid]{/dede:loop}
Browser output: 12 6
I think the reason is that [field: typeid] will first execute the internal php statement. when it runs to the line [/field: typeid, it will call internal functions and directly return the content of the [field: typeid] underlying template. if you want to output 12 directly, you can only at/include/extend. fuc. add custom functions to the php file.
function abc($val){ return $val*2;}
Then rewrite the template:
{dede:loop table='dede_addonimages' if='aid=94'}[field:typeid function="abc(@me)" /]{/dede:loop}
Output result: 12
In addition, the variables in the two php codes in the same template are not universal. that is to say, the scope of a variable in a php code is limited to the short code.
Example:
{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}
Output result: 12 6 NULL
If you want to use the variables in the above php script in the subsequent php script, I come up with a temporary solution, that is, using global variables to solve this problem.
{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}
Output result: 6 12 (because [field: typeid] does not contain echo, 6 is output directly)