2015-10-03 59 views
1

我有這個PHP錯誤,但腳本的作品。我認爲第一個​​是正確的,只有第二個​​有這個錯誤。vprintf()參數太少

vprintf(): Too few arguments 

function CreateLangTable($csvFile, $startRow, $endRow, $number) { 
    global $lang_code1, $lang_code2, $vocabulary_group, $teilurl; 

    if ($endRow < $startRow) { 
     return; 
    } 
    echo ' 
    <a id="' . $vocabulary_group[$teilurl[4]][$number] . '"></a>  
    <table class="table-vocabulary">       
    <thead>'; 
    $csvFile->seek($startRow); 

    vprintf(' 
     <tr> 
      <th><div data-text="%1$s" data-lang="' . $lang_code1 . '" class="trigger_play"> %1$s</div></th> 
      <th><div data-text="%2$s" data-lang="' . $lang_code2 . '" class="trigger_play"> %2$s</div></th> 
     </tr> 
    </thead> 
    <tbody>', $csvFile->current()); 

    while ($csvFile->key() <= $endRow) { 
     $csvFile->next(); 

    vprintf(' 
     <tr> 
      <td><div data-text="%1$s" data-lang="' . $lang_code1 . '" class="trigger_play"> %1$s</div></td> 
      <td><div data-text="%2$s" data-lang="' . $lang_code2 . '" class="trigger_play"> %2$s</div></td> 
     </tr>', $csvFile->current()); 
    } 

    echo ' 
    </tbody> 
    </table>' . "\n"; 
} 

我已經在另一個答案閱讀與sprintf的()toescape的$\$一個問題,但我會得到很多這樣的錯誤的。

例如csv文件

th-value 1,th-Wert 1 
value 2,Wert 2 
value 3,Wert 3 
value 4,Wert 4 
value 5,Wert 5 

th-value 1,th-Wert 1 
value 2,Wert 2 
value 3,Wert 3 
value 4,Wert 4 
value 5,Wert 5 

我用這個代碼來生成正確的表:

CreateLangTable($file, 0, 4, 0); 
CreateLangTable($file, 6, 9, 1); 

如果我改變4〜3,9〜8中的錯誤都沒有了,但是爲什麼呢?

+1

當你接觸不到的地方'$ csvFile'做的最後一個元素'next'獲取你什麼呢? –

+0

@u_mulder我現在已經發布了整個功能。 – Grischa

+0

似乎@u_mulder建議您在到達$ $ endRow後檢查$ csvFile-> current()中的內容。你可以使用XDebug來找到它,或者在$ csvFile-> next()後面添加一個'var_dump($ csvFile-> current())'來檢查每一行的值。通過這種方式,您可以確認實際上是否所有行都有至少兩個值的數組。 –

回答

1

這裏的問題在於,您在 之前將指針移動到之前,您將獲得當前值。

所以,當指針指向你的最後一個元素,你向前推進使用next。那麼現在的元素是什麼?我認爲它是NULL。

所有你所要做的就是使用nextvsprintf 或者,也許使用valid檢查,如果當前值是一個有效的值。

+0

謝謝。我在'vsprintf'之後使用'next'並且錯誤消失了,所以我不必更改第二個參數的值。 – Grischa

+0

當我使用'next'後'vsprintf'腳本會從第一行generet兩倍值,首先作爲一個'',然後其他人''​​像。它應該只顯示它​​在''所以 – Grischa

+0

開始'while'環 –