douwen1313 2014-06-08 10:56
浏览 22
已采纳

在表格的下拉列表中分配变量值

I have list of Materials as drop down in each row of table (100 rows). I want to selectively assign any row selected material.

For example, suppose $materialoptions is assigned an array (1=>'Material 1', 2=>'Material 2',3=>'Material 3'). This is list of dropdown in each row.

I want to assign in php say, In row 20, material selected is 'Material 2'.

Below is my implementation. Not able to assign properly. HTML/Smarty Code

<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 Code

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

//In row 20, material selected is 'Material 2'

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

1条回答 默认 最新

  • dongtan8979 2014-06-08 14:41
    关注

    You should change in your template file

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

    into

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

    But in this case you should probably defined all material_1, material_2 and so on variables because you use them for option creation

    ** EDIT **

    You haven't mentioned that you need it in Smarty 2. Smarty 2 doesn't support variable variables so you should rethink of using other way to achieve this.

    In PHP instead of

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

    you should assign variables this way:

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

    And in template file you should change

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

    into

    {html_options selected=$selections[$smarty.section.counter.index] options=$materialoptions}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?