【问题标题】:Dynamic table in template written in html/javascript用 html/javascript 编写的模板中的动态表
【发布时间】:2017-08-02 18:32:11
【问题描述】:

我尝试建立一个动态表,这个表应该替换一个修复表。

<table cellpadding="3" cellspacing="0" border="1">
    <tr>
    <th>Aufgabe</th>
    <th>1</th>
    <th>2</th>
    <th>3</th>
    <th>4</th>
    <th>5</th>
    <th>6</th>
    <th>Summe</th>
    <th>Note</th>
    </tr>
    <tr>
<td> </td>		

<td><input type="text" id="punkte0" name="punkte0" value="{$punkte.0}" size="3" onKeyUp="aktualisieren('punkte0','punkte1')" /></td>
<td><input type="text" id="punkte1" name="punkte1" value="{$punkte.1}" size="3" onKeyUp="aktualisieren('punkte1','punkte2')" /></td>
<td><input type="text" id="punkte2" name="punkte2" value="{$punkte.2}" size="3" onKeyUp="aktualisieren('punkte2','punkte3')" /></td>
<td><input type="text" id="punkte3" name="punkte3" value="{$punkte.3}" size="3" onKeyUp="aktualisieren('punkte3','punkte4')" /></td>
<td><input type="text" id="punkte4" name="punkte4" value="{$punkte.4}" size="3" onKeyUp="aktualisieren('punkte4','punkte5')" /></td>
<td><input type="text" id="punkte5" name="punkte5" value="{$punkte.5}" size="3" onKeyUp="aktualisieren('punkte5','punkte6')" /></td>

<td id="tds1">0</td>
<td></td>
</tr>
<tr>
<td>Gesamt</td>
<td colspan="6"></td>
<td id="summe">0</td>
<td id="note">{$student.note/10}</td>
</tr>
</table>

但是列应该是用户相关的,所以我在我的 PHP 代码中编写了一个函数,它将值(4 到 7 之间的数字)保存在我的数据库中。在数据库klausuraufgaben中调用。

我试过这个来替换列:

$iQuantity = (integer)$_POST['klausuraufgaben'];
$aTableHeads = [];
$aTableContents = [];

for ( $x=0; $x<$iQuantity; $x++ ) {
$aTableHeads[] = '<th>' . ($x + 1) . '</th>';
$aTableContents[] = '<td><input type="text" id="punkte' . (string)$x . '" name="punkte' . (string)$x . '" value="{$punkte.' . (string)$x . '}" size="3" onKeyUp="aktualisieren('punkte. (string)$x . '",'punkte. (string)$x+1 . '"></td>';

但它不起作用。有人能看出什么问题吗?

这是我得到的错误:

[在 kartei.tpl 第 254 行]:语法错误:无法识别的标签:$aTableHeads[] = '' 。 ($x + 1) 。 ''; $aTableContents[] = '

【问题讨论】:

  • 嗯,你用的不是twig或者其他模板引擎吗?
  • 是的。我用聪明的。修复表功能很好,但是php代码不行。这是一个聪明的问题吗?
  • 那么 smarty 中的 for 循环不适合你吗? smarty.net/docs/en/language.function.for.tpl
  • 谢谢,我以这种方式编辑模板的代码 {php} $iQuantity = (integer)$_POST['klausur_aufgaben']; $aTableHeads = []; $aTableContents = []; {for $x=0 to $iQuantity;} // {for( $x=0; $x' . ($x + 1) 。 ''; $aTableContents[] = ' '; {/for} {/php}

标签: javascript php html html-table


【解决方案1】:

谢谢,我用这种方式编辑模板的代码:

{php}
   $iQuantity = (integer)$_POST['klausur_aufgaben'];
   $aTableHeads = [];
   $aTableContents = [];
    
{for $x=0 to $iQuantity;}
 //  {for( $x=0; $x<$iQuantity; $x++ )

$aTableHeads[] = '<th>' . ($x + 1) . '</th>';
$aTableContents[] = '<td><input type="text" id="punkte' . (string)$x . '" name="punkte' . (string)$x . '" value="{$punkte.' . (string)$x . '}" size="3" onKeyUp="aktualisieren('punkte. (string)$x . '",'punkte. (string)$x+1 . '"></td>'; 
    {/for}
{/php}

【讨论】:

  • 是的,但我仍然收到错误消息,还有什么问题?
  • 可能是这样的答案?
【解决方案2】:

我不是聪明的专家,但让我们来解决这个问题。

{php}
   $iQuantity = (integer)$_POST['klausur_aufgaben'];
{/php}
   <tr> 
{for $x=0 to $iQuantity}
  <th>{$x + 1}</th>
{/for}
  </tr>

{for $x=0 to $iQuantity}
  <td><input type="text" id="punkte{$x}" name="punkte{$x}" value="NotSureHere this needs to come from another array?" size="3" onKeyUp="aktualisieren('punkte{$x}','punkte{$x+1}')"/></td>
{/for}

【讨论】:

  • 刚刚注意到我有一个额外的 php 标签,把它取下来告诉你我不是专家!
  • 对不起,理查德,但我不是这个意思,不管我做什么,我总是会犯聪明的错误
  • 好的,抱歉,我帮不了你更多。我确实看到我们不需要';'在 $iQuantity 变量的末尾 - 我将编辑答案。但不确定这整件事。我从来没有做过聪明的编程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多