【问题标题】:Assigning variable value in dropdown list in table在表格的下拉列表中分配变量值
【发布时间】:2014-06-08 10:56:14
【问题描述】:

我在表格的每一行(100 行)中都有材料列表作为下拉列表。 我想有选择地分配任何行选定的材料。

例如,假设为 $materialoptions 分配了一个数组 (1=>'Material 1', 2=>'Material 2',3=>'Material 3')。这是每行中的下拉列表。

我想在 php 中指定,在第 20 行,选择的材料是“材料 2”。

以下是我的实现。无法正确分配。 HTML/Smarty 代码

<form name='materialsel' id='materialsel' method='POST' action=''>
<table>
{section name=counter start=1 loop=100 step=1}
  <tr>
    <td><SELECT name="materialid_{$smarty.section.counter.index}" id="materialid_{$smarty.section.counter.index}" onchange='return document.forms.materialsel.submit();'><option value='0'>Select Material</option>
 {html_options selected=$material_$smarty.section.counter.index options=$materialoptions}
   </SELECT>
      </td>
 </tr>
{/section}
 </table>
 </form>

PHP 代码

$smarty->assign("materialoptions", array (1=>'Material 1', 2=>'Material 2',3=>'Material 3'));

//在第20行,选择的材料是'Material 2'

$smarty->assign("material_20",2); //Not able to do this

【问题讨论】:

    标签: php smarty smarty2


    【解决方案1】:

    你应该改变你的模板文件

    {html_options selected=$material_$smarty.section.counter.index options=$materialoptions}
    

    进入

    {html_options selected=${"material_{$smarty.section.counter.index}"} options=$materialoptions}
    

    但在这种情况下,您可能应该定义所有 material_1、material_2 等变量,因为您将它们用于创建选项

    ** 编辑 **

    你没有提到你在 Smarty 2 中需要它。Smarty 2 不支持可变变量,所以你应该重新考虑使用其他方式来实现这一点。

    在 PHP 中而不是

    $smarty->assign("material_20",2);
    

    你应该这样分配变量:

    $selections = array( '20' => 2);        
    $smarty->assign("selections", $selections);
    

    在模板文件中你应该改变

    {html_options selected=$material_$smarty.section.counter.index options=$materialoptions}
    

    进入

    {html_options selected=$selections[$smarty.section.counter.index] options=$materialoptions}
    

    【讨论】:

    • Hello Marcin, {html_options ..... } 如上在 PHP 日志中给出错误。 PHP 致命错误:Smarty 错误:[在 modules/materials.html 第 51 行]:语法错误:无效属性名称:'material_{$smarty.section.counter.index'(Smarty_Compiler.class.php,第 1547 行)。我正在使用 Smarty 2
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    相关资源
    最近更新 更多