0

I try do build a dynamic table, this table should replace a fix table.

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

But the columns should be user-dependent, so I wrote a function in my PHP code, which save the value (number between 4 an 7) in my database. It is called in the database klausuraufgaben.

I tried this to replace the columns:

$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>';

But it doesn't work. Can someone see what is wrong?

This is the Error, which I get:

[in kartei.tpl line 254]: syntax error: unrecognized tag: $aTableHeads[] = '' . ($x + 1) . ''; $aTableContents[] = '

4
  • Mmm, aren't you using twig or other template engine? Commented Jul 31, 2017 at 13:45
  • yes. i use smarty. the fix table function very well, but the php code not. is it a smarty problem? Commented Jul 31, 2017 at 13:55
  • So wouldn't the for loop in smarty work for you? smarty.net/docs/en/language.function.for.tpl Commented Jul 31, 2017 at 13:57
  • thanks, i edit the code for the template in this way {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} Commented Jul 31, 2017 at 14:28

2 Answers 2

2

thanks, i edit the code for the template in this way:

{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}

Sign up to request clarification or add additional context in comments.

2 Comments

yes, but i still get error messages, what else is wrong?
Perhaps something like that answer?
0

I'm not smarty expert, but lets have a crack at this.

{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}

3 Comments

Just noticed I had an extra php tag, taken it off told you I wasn't an expert!
sorry, richard, but it wasn´t meant in this way, no matter what i did, i alway get smarty errors
Ok, sorry I can't help you more. I did see that we don't need the ';' at the end of the $iQuantity variable - I'll edit the answer. But unsure about this whole thing. I've never done smarty programming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.