Php 在表的下拉列表中指定变量值

Php 在表的下拉列表中指定变量值,php,smarty,smarty2,Php,Smarty,Smarty2,我在表格的每一行(100行)中都有下拉列表形式的材料列表。 我想有选择地指定任何行选定的材质 例如,假设为$materialoptions分配了一个数组(1=>'material1',2=>'material2',3=>'material3')。这是每行中的下拉列表 我想在php中指定,比如说,在第20行中,选择的材料是“材料2” 下面是我的实现。不能正确分配。 HTML/Smarty代码 <form name='materialsel' id='materialsel' method='

我在表格的每一行(100行)中都有下拉列表形式的材料列表。 我想有选择地指定任何行选定的材质

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

我想在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>
//在第20行中,所选材料为“材料2”

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

您应该在模板文件中进行更改

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

但是在这种情况下,您可能应该定义所有材质\u1、material_u2等变量,因为您使用它们创建选项

**编辑**

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

用PHP代替

$smarty->assign("material_20",2);
您应该这样分配变量:

$selections = array( '20' => 2);        
$smarty->assign("selections", $selections);
在模板文件中,您应该进行更改

进入


您好,Marcin,{html_options…}如上所述,PHP日志中出现错误。PHP致命错误:Smarty error:[在modules/materials.html第51行]:语法错误:无效的属性名称:“material_{$Smarty.section.counter.index”(Smarty_Compiler.class.PHP,第1547行)。我正在使用Smarty 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}