大家好,我需要php专家的建议。 这是我的问题。用户有2个输入开始和结束这都是整数。这将能够识别奇数和偶数。我已经解决了奇怪甚至差不多完成的事情。主要问题是起始值1结束5. 1是奇数,应该在表格中显示奇数。但问题是它在偶数表中找到了。初始值是问题。其余的都很好。
这是我的代码
<?php
$firstnum = $_POST['first_input'];
$secondnum = $_POST['second_input'];
$counter = 0;
echo "<table border='1'>";
if ($firstnum < $secondnum) {
echo "<tr>"; //first tr
echo "<th>"; echo "Even numbers"; echo "</th>";
echo "<th>"; echo "Odd numbers"; echo "</th>";
echo "</tr>";
for ($counter=$firstnum; $counter <= $secondnum ; $counter++) {
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
} else {
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}
}
} elseif ($firstnum > $secondnum) {
# code...
//first num is < second num
echo "<tr>"; //first tr
echo "<th>"; echo "Even numbers"; echo "</th>";
echo "<th>"; echo "Odd numbers"; echo "</th>";
echo "</tr>";
for ($counter=$firstnum; $counter >= $secondnum ; $counter--) {
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
} else {
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}
}
}
echo "</table>";
?>
答案 0 :(得分:4)
您的问题是,if
/ else
阻止了您的html无效。
如果您有if
<tr>
<td><td>
并在您的else
中
<td></td>
</tr>
这两个都需要完整的行/单元格标记
<tr>
<td></td>
<td></td>
</tr>
所以你的代码应该是
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
echo "<td>";
echo "</td>";
echo "</tr>";
} else {
echo "<tr>";
echo "<td>";
echo "</td>";
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}