【问题标题】:How to store a Coldfusion #variable# in MySQL and output the variable value (and not the variable name) from the database?如何在 MySQL 中存储 Coldfusion #variable# 并从数据库中输出变量值(而不是变量名)?
【发布时间】:2012-09-21 14:08:48
【问题描述】:

我正在运行Coldfusion8/MySQL 5.0.88

我的数据库中有两个表:

modules
textblocks

我的网站运行多种语言,所以我在每个页面的开头都这样做,以根据用户选择的语言从数据库中加载文本块。

<cfquery datasource="#Session.datasource#" name="texte">
   SELECT *
   FROM textblocks
   WHERE lang = <cfqueryparam value = "#Session.lang#" cfsqltype="cf_sql_varchar" maxlength="2">  
</cfquery>
<cfoutput query="texte">
    <cfset "#trim(label)#" = "#trim(content)#">
</cfoutput>

如果我需要一个文本块,我可以像这样简单地使用label

<cfoutput>#tx_hello_world#</cfoutput>

我的问题在于包含模块列表的第二张表。

每个模块都有自己的描述(标题、子标题、信息、项目符号 1-5)。我必须遍历所有模块(25),所以我只是将labels 存储在数据库中,希望进行一次循环并从数据库中输出labels。不幸的是,输出 label123 返回

 #label123#

vs我希望

 "text from table textblocks stored at label123"

问题:
是否可以在 MySQL 中存储“动态变量”并在输出这些变量时检索它们的基础值?如果不是,那么在一个循环中输出 25 个模块而不说“如果这是模块 A,则取文本 tx_module_a_title,否则如果...”的更好方法是什么?

谢谢!

编辑:
所以我希望避免在每个循环上都运行这个:

<cfif mods.module_name EQ "agents">
    <cfset variables.module_header = tx_module_b2b_agents_title>
    <cfset variables.module_subheader = tx_module_b2b_agents>
    <cfset variables.module_info = tx_module_b2b_agents_info>
    <cfset variables.module_bull_1 = tx_module_b2b_agn_accounts>
    <cfset variables.module_bull_2 = tx_module_b2b_agn_price_clients>
    <cfset variables.module_bull_3 = tx_module_b2b_agn_client_mgt>
    <cfset variables.module_bull_4 = tx_module_b2b_agn_create_retailer>
    <cfset variables.module_bull_5 = tx_module_b2b_exp_grace>
    <cfset variables.module_usage_tx = tx_vertreter/tx_gen_month>
<cfelseif mods.module_name EQ "preorder">
    <cfset variables.module_header = tx_module_b2b_preorder_title>
    <cfset variables.module_subheader = tx_module_b2b_preorder>
    <cfset variables.module_info = tx_module_b2b_preord_info>
    <cfset variables.module_bull_1 = tx_module_b2b_pre_split_type>
    <cfset variables.module_bull_2 = tx_module_b2b_pre_qty_y_n>
    <cfset variables.module_bull_3 = tx_module_b2b_pre_deliverydate>
    <cfset variables.module_bull_4 = tx_module_b2b_exp_grace>
    <cfset variables.module_bull_5 = "">
    <cfset variables.module_usage_tx = tx_gen_month >
<cfelseif mods.module_name EQ "export">
    <cfset variables.module_header = tx_module_b2b_export_title>
    <cfset variables.module_subheader = tx_module_b2b_export>
    <cfset variables.module_info = tx_module_b2b_export_info>
    <cfset variables.module_bull_1 = tx_module_b2b_exp_pricelists>
    <cfset variables.module_bull_2 = tx_module_b2b_exp_currencies>
    <cfset variables.module_bull_3 = tx_module_b2b_exp_customerlist>
    <cfset variables.module_bull_4 = tx_module_b2b_exp_regional_assortme>
    <cfset variables.module_bull_5 = tx_module_b2b_exp_grace>
    <cfset variables.module_usage_tx = tx_gen_month>
... 22 else-ifs to go
</cfif>

如果我可以将文本块存储在数据库模块记录中tx_ 值后面,我可以从那里检索它们。

【问题讨论】:

    标签: mysql variables dynamic coldfusion


    【解决方案1】:

    当您从数据库输出内容时,您可以在内容上使用de()evalaute() 来处理变量。您可能需要使用嵌套组合。我忘记了确切的语法。 De(evaluate('#var#')) 或类似的东西。

    但是,如果您的内容中有任何其他 # 不是 var,这将证明是有问题的。我建议改用词典。看这篇文章Content Management : Processing Dynamic Content

    【讨论】:

      【解决方案2】:

      假设您的 texte 查询中包含可变内容您可以这样做:

      <cfloopquery="texte">
          <cfset variables[trim(texte.label)] = trim(texte[content][texte.currentrow])>
      </cfloop>
      

      您的查询是一个结构,您可以像另一个结构一样引用它,您可以使用内置的 currentrow 属性来引用结构中的当前行

      重新阅读您的问题后,上下文似乎存储在模块表中?那你就可以了。无论哪种方式,您都应该尽可能避免使用评估,尽管这会起作用。

      <cfloop query="texte">
        <cfloop query="modulesquery">
          <cfset variables[trim(texte.label)] = trim(modulesquery[content][modulesquery.currentrow])>
       </cfloop>
      </cfloop>
      

      【讨论】:

      • 是的,没有评估的朋友。也会试试的。谢谢!
      【解决方案3】:

      我只关注这一点:

      问题:是否可以在 MySQL 中存储“动态变量”和 输出这些后,检索它们的基础值吗?

      我可能会做的是将文本存储为文件,并将文件的路径存储在数据库中。然后,当您需要让 CF 处理文件时(这是解析变量的唯一方法),只需从数据库中提取路径,并将文件包含在该路径中即可。

      如果文本需要进入数据库,那么您需要从数据库中读取它,将其写入文件,然后包含该文件。要处理 CF 变量,它需要由 CF 处理,为此,您需要将文件传递给 ColdFusion。就是这样。

      但是到处写文件并在每次需要时都包含它们会很慢,因为文件需要在运行之前进行编译,并且编译过程相对较慢。

      解决此问题的方法是仅在文件更改时写入文件,而不是在每次需要时写入。在大多数网站上,读取数据的频率比更改数据的频率高出几个数量级,这取决于网站的工作方式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-20
        • 1970-01-01
        • 1970-01-01
        • 2015-04-07
        相关资源
        最近更新 更多