1

データベースクエリから返された行数に基づいたフォームに値をエコーし​​ようとしています。エラーが発生し続ける解析エラー:構文エラー、予期しないT_ECHO、'、'、または';'が必要です

お分かりのように、私はこれにかなり慣れていません。誰かが私が変数をエコーするのを手伝ってもらえますか?var_dumpが示すように、$num_rowsが値を返していることはわかっています。ありがとう

<?

if($num_rows <= 10) {

echo '</br></br><form id="h1" class="rounded" action="4.php" target="" 
method="post"/>
<input type="submit" name="submit"  class="button" value="10" /><br>
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/>
</form>';
 }
if($num_rows >10) {
echo '</br></br><form id="h2" class="rounded" action="4.php"    
target="_blank" method="post"/>
<input type="submit" name="submit"  class="button" value="11"/><BR>
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/>

</form>';
}?>
4

2 に答える 2

2

両方のコードブロックで、出力を連結したり2つのステートメントを使用したりする代わりに、コマンドechoを繰り返します。あなたはこれをしました:

echo '</br></br><form id="h1" class="rounded" action="4.php" target="" 
method="post"/>
<input type="submit" name="submit"  class="button" value="10" /><br>
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/>
</form>';

これは構文エラーです。代わりに、これを行うことができます:

echo '</br></br><form id="h1" class="rounded" action="4.php" target="" 
method="post"/>
<input type="submit" name="submit"  class="button" value="10" /><br>
<input type="text" name="number_of_tests" value="' . $num_rows . '"/>
</form>';

またはこれ:

echo '</br></br><form id="h1" class="rounded" action="4.php" target="" 
method="post"/>
<input type="submit" name="submit"  class="button" value="10" /><br>
<input type="text" name="number_of_tests" value="';
echo $num_rows . '"/>';
echo '</form>';
于 2012-04-07T18:50:44.587 に答える
1

これは、文字列を連結して結果を出力するために使用する必要があるコードです

echo ' some value ' . $variable . ' other text ';

このecho関数は文字列を出力し、ドット(。)演算子は文字列を連結します。これは一種の間違ったコードです

echo 'value="'echo $num_rows;'"/>';

変数の値を挿入したい場合、これが方法です

$a_string = 'I\'m a string';
echo "I'm a double quoted string and can contain a variable: $a_string";

これはアレイでも機能します

$an_array = array('one', 'two', 'three');
echo "The first element of the array is {$an_array[0]}"

PHPマニュアルを参照してください

于 2012-04-07T18:40:07.600 に答える