循环语句与空值?

时间:2017-08-22 22:05:50

标签: php sql database

我尝试做的是查看task1,task2等打印任务的列,并添加yes / no单选按钮,但如果它为null则不打印单选按钮。这是一个开关盒吗?如何在最短的代码中完成整个超过200行。

目前,即使任务为空,也会为所有行添加选项(单选按钮)

这是代码。

$sql1 = "SELECT * FROM PMList where AssetNum= '$AssetNum';";
    $result = mysqli_query($con, $sql1) or die(mysqli_error($con));

if ($result->num_rows > 0)

while($row = mysqli_fetch_array($result))
{   
            echo "<tr border='0'><td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task1'];
            echo "</td><td bgcolor='#D8D8D8'><input type='radio' name='Task1C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task1C' value='No'>No";
            echo "<tr border='0'><td bgcolor='ffffff' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task2'];
            echo "</font></td><td bgcolor='ffffff'><input type='radio' name='Task2C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task2C' value='No'>No"; 
            echo "<tr border='0'><td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task3'];
            echo "</font></td><td bgcolor='#D8D8D8'><input type='radio' name='Task3C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task3C' value='No'>No";
            echo "<tr border='0'><td bgcolor='ffffff' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task4'];
            echo "</font></td><td bgcolor='ffffff'><input type='radio' name='Task4C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task4C' value='No'>No";
            echo "<tr border='0'><td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task5'];
            echo "</font></td><td bgcolor='#D8D8D8'><input type='radio' name='Task5C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task5C' value='No'>No";
            echo "<tr border='0'><td bgcolor='ffffff' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task6'];
            echo "</font></td><td bgcolor='ffffff'><input type='radio' name='Task6C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task6C' value='No'>No";
            echo "<tr border='0'><td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task7'];
            echo "</font></td><td bgcolor='#D8D8D8'><input type='radio' name='Task7C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task7C' value='No'>No";
            echo "<tr border='0'><td bgcolor='ffffff' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task8'];
            echo "</font></td><td bgcolor='ffffff'><input type='radio' name='Task8C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task8C' value='No'>No";
            echo "<tr border='0'><td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task9'];
            echo "</font></td><td bgcolor='#D8D8D8'><input type='radio' name='Task9C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task9C' value='No'>No";
            echo "<tr border='0'><td bgcolor='ffffff' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task10'];
            echo "</font></td><td bgcolor='ffffff'><input type='radio' name='Task10C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task10C' value='No'>No";
            echo "<tr border='0'><td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task11'];
            echo "</font></td><td bgcolor='#D8D8D8'><input type='radio' name='Task11C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task11C' value='No'>No";
            echo "<tr border='0'><td bgcolor='ffffff' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task12'];
            echo "</font></td><td bgcolor='ffffff'><input type='radio' name='Task12C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task12C' value='No'>No";
            echo "<tr border='0'><td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task13'];
            echo "</font></td><td bgcolor='#D8D8D8'><input type='radio' name='Task13C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task13C' value='No'>No";
            echo "<tr border='0'><td bgcolor='ffffff' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task14'];
            echo "</font></td><td bgcolor='ffffff'><input type='radio' name='Task14C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task14C' value='No'>No";
            echo "<tr border='0'><td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task15'];
            echo "</font></td><td bgcolor='D8D8D8'><input type='radio' name='Task15C' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task15C' value='No'>No";
            echo "</td></table><Br><table border='0' width='80%' cellspacing='0' cellpadding='0' height='45' bgcolor='#ffffff' align='center'>";
            echo "<tr>";
            echo "<td border='0' width='50%'>";
            echo "&nbsp; Comments:";
            echo "<br> <br> <textarea align='center' cols='60' rows='4' name='Comments'></textarea>";
            echo "</td><td border='0' width='50%'>";
            echo "&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;  Repairs Made: ";
            echo "<input type='radio' name='Repairs' value='Yes'>Yes";
            echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            echo "<input type='radio' name='Repairs' value='No'>No";
            echo "&nbsp;&nbsp; &nbsp;&nbsp; If NO explain:";
            echo "<br><br> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;  <textarea align='right' cols='60' rows='4' name='Explained'></textarea>";
            echo "</td></tr>";
            echo "</table>";
            echo "<P><P>";
            echo "<table border='0' width='80%' cellspacing='0' cellpadding='0' height='45' bgcolor='#ffffff' align='center'>";
            echo "<tr>";
            echo "<td border='0'>";
            echo "&nbsp; Parts Needed:";
            echo "<br> <br> <textarea align='center' cols='132' rows='3' name='Parts'></textarea>";
            echo "</td>";
            echo "</table>";
            echo "<P><P>";
            echo "<center><input type='submit' name='txtbutton' value='SUBMIT' />";
            echo "</form>";

        }
        else
        {
        echo "0 results";
        }

        $con->close();

请帮忙。提前谢谢!

2 个答案:

答案 0 :(得分:0)

您可以在循环中检查任务是否为null,执行类似的操作:

if(!is_null($row['task1']) && $row['task1'] != "")
{
  // here echo your radio button 
}

答案 1 :(得分:0)

用非常短的代码检查所有任务:

foreach(range(1,15) as $taskNumber){
  if(is_null($row["task{$taskNumber}"]) OR empty($row["task{$taskNumber}"]))
     echo "The task number: $taskNumber is null or empty<br>";
}

将此代码放入while循环中,现在您知道其中一个任务是空还是空。