PHP While While循环不会多次显示数据

时间:2017-05-20 19:14:11

标签: php while-loop

我尝试使用while循环在<span>字段中显示带有id的数据,每次循环时。但它只显示第一个循环上的数据。我做错了什么?

<? php

$i = 0;
while($i < 5){
$i++;
echo "<input type = 'hidden' id = 'minute' value = ''>
<input type = 'hidden' id = 'second' value = ''>
<br><br>
<span id = 'selectmin' value = '0'></span>:<span id = 'selectsec' value = 
'0'></span>
<br><br>";
echo "<script>document.getElementById('selectmin').innerHTML = 'Hello ';
</script>";
?>

2 个答案:

答案 0 :(得分:2)

这段代码在php中无效。

使用<?之类的短标签,或者只使用合格的PHP标签来避免此类错误。 <?php之间没有空格。

答案 1 :(得分:0)

id='selectmin'您是否正在创建具有相同ID的5个相同HTML块? ID是唯一的,只会选择第一场比赛,这就是它只显示一次的原因。

使用'selectmin$i',以便为每个循环创建一个唯一的ID:

<?php

$i = 0;
while($i < 5){
$i++;
echo "<input type = 'hidden' id = 'minute$i' value = ''>
<input type = 'hidden' id = 'second' value = ''>
<br><br>
<span id = 'selectmin$i' value = '0'></span>:<span id = 'selectsec$i' value = '0'></span>
<br><br>";
echo "<script>document.getElementById('selectmin$i').innerHTML = 'Hello '; </script>";
}
?>

同时使用}关闭while循环,并使用<?php<?启动您的php代码。

输出HTML是:

&#13;
&#13;
<input type='hidden' id='minute1' value=''>
<input type='hidden' id='second' value=''>
<br>
<br>
<span id='selectmin1' value='0'></span>:<span id='selectsec1' value='0'></span>
<br>
<br>
<script>
  document.getElementById('selectmin1').innerHTML = 'Hello ';
</script>
<input type='hidden' id='minute2' value=''>
<input type='hidden' id='second' value=''>
<br>
<br>
<span id='selectmin2' value='0'></span>:<span id='selectsec2' value='0'></span>
<br>
<br>
<script>
  document.getElementById('selectmin2').innerHTML = 'Hello ';
</script>
<input type='hidden' id='minute3' value=''>
<input type='hidden' id='second' value=''>
<br>
<br>
<span id='selectmin3' value='0'></span>:<span id='selectsec3' value='0'></span>
<br>
<br>
<script>
  document.getElementById('selectmin3').innerHTML = 'Hello ';
</script>
<input type='hidden' id='minute4' value=''>
<input type='hidden' id='second' value=''>
<br>
<br>
<span id='selectmin4' value='0'></span>:<span id='selectsec4' value='0'></span>
<br>
<br>
<script>
  document.getElementById('selectmin4').innerHTML = 'Hello ';
</script>
<input type='hidden' id='minute5' value=''>
<input type='hidden' id='second' value=''>
<br>
<br>
<span id='selectmin5' value='0'></span>:<span id='selectsec5' value='0'></span>
<br>
<br>
<script>
  document.getElementById('selectmin5').innerHTML = 'Hello ';
</script>
&#13;
&#13;
&#13;