Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of running php script in DEDE template

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to run the php script in the DEDE template, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

How do I run php scripts in DEDE templates?

It is often necessary to directly deal with the underlying fields of the dede database. If there is no corresponding function in dede, then we have to use other methods to implement it. As mentioned in the title, run php scripts and php variables. Here is a good example that interested friends can refer to.

Recommended study: dream weaving cms

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

For example: I want to take out the typeid field of a record in the datasheet addonimages and output the result of typeid times 2 in the browser. (note: the typeid value here is 6)

This is what I wrote at first:

The code is as follows:

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

The output of the browser is: 126

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

The code is as follows:

Function abc ($val) {return $val*2;}

Then the template is rewritten as follows:

The code is as follows:

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

The output is: 12

It is also important to note that the variables in two pieces of php code in the same template are not universal, that is, a variable in a piece of php code whose scope is limited to that short code.

Example:

The code is as follows:

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

The output is: 126 NULL

If I want to use the variables from the above php script in later php scripts, I have come up with a temporary solution to this problem with global variables.

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}

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

The above is all the contents of the method of running the php script in the DEDE template, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report