我该如何简化这个php脚本
问题描述:
关于如何简化下面的php脚本的任何建议?这是我以前的问题:How to check if a checkbox/ radio button is checked in php 这是链接到这个, 我在这里要做的是输出数据取决于选中的复选框。 但是我的代码不是很好,它显示了2个表,如果条件满足2个结果。 正如你可以在下面的代码中看到的,关于如何简化这个的任何建议?我该如何简化这个php脚本
$id = mysql_real_escape_string($_POST['idnum']);
if ($_POST['yr'] == 'year' and $_POST['sec'] == 'section'){
$result2 = mysql_query("SELECT * FROM student WHERE IDNO='$id'");
echo "<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
<th>SECTION</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['IDNO'] . "</td>";
echo "<td>" . $row['YEAR'] . "</td>";
echo "<td>" . $row['SECTION'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
if ($_POST['yr'] == 'year' and $_POST['sec'] == 'section' and $_POST['lname'] == 'lastname'){
$result3 = mysql_query("SELECT * FROM student WHERE IDNO='$id'");
echo "<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
<th>SECTION</th>
<th>LASTNAME</th>
</tr>";
while($row = mysql_fetch_array($result3))
{
echo "<tr>";
echo "<td>" . $row['IDNO'] . "</td>";
echo "<td>" . $row['YEAR'] . "</td>";
echo "<td>" . $row['SECTION'] . "</td>";
echo "<td>" . $row['LASTNAME'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysql_close($con);
?>
答
虽然不是一个PHP的人,我会做一个逻辑基础的在何时在表中包含姓氏列。请问像这样的帮助,并保持简单...
$ShowLastName = ($_POST['yr'] == 'year'
and $_POST['sec'] == 'section'
and $_POST['lname'] == 'lastname');
$id = mysql_real_escape_string($_POST['idnum']);
$result2 = mysql_query("SELECT * FROM student WHERE IDNO='$id'");
echo "<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
<th>SECTION</th> ";
if($ShowLastName)
echo "<th>LASTNAME</th> ";
echo "</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['IDNO'] . "</td>";
echo "<td>" . $row['YEAR'] . "</td>";
echo "<td>" . $row['SECTION'] . "</td>";
if($ShowLastName)
echo "<td>" . $row['LASTNAME'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
答
你应该使用PHP与HTML如下
<?PHP $id = mysql_real_escape_string($_POST['idnum']);
if ($_POST['yr'] == 'year' and $_POST['sec'] == 'section'){
$result2 = mysql_query("SELECT * FROM student WHERE IDNO='$id'");
?>
<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
<th>SECTION</th>
</tr>
<?PHP
while($row = mysql_fetch_array($result2))
{
<tr>
<td> <?PHP echo $row['IDNO'] ?>
<td> <?PHP echo $row['YEAR'] ?></td>
<td> <?PHP echo $row['SECTION'] ?></td>
</tr>
<?PHP } ?>
</table>
<?PHP }
if ($_POST['yr'] == 'year' and $_POST['sec'] == 'section' and $_POST['lname'] == 'lastname'){
$result3 = mysql_query("SELECT * FROM student WHERE IDNO='$id'");
?>
<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
<th>SECTION</th>
<th>LASTNAME</th>
</tr>
<?PHP }
while($row = mysql_fetch_array($result3))
{
?>
<tr>
<td> <?PHP echo $row['IDNO'] ?></td>
<td> <?PHP echo $row['YEAR'] ?></td>
<td> <?PHP echo $row['SECTION'] ?> </td>
<td> <?PHP echo $row['LASTNAME'] ?> </td>
</tr>";
<?PHP } ?>
</table>
<?PHP } ?>
<?PHP mysql_close($con); ?>
EDITED可以更要简化如下
<?PHP $id = mysql_real_escape_string($_POST['idnum']);
if ($_POST['yr'] == 'year' and $_POST['sec'] == 'section'){
$result2 = mysql_query("SELECT * FROM student WHERE IDNO='$id'");
?>
<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
<th>SECTION</th>
<?PHP if ($_POST['lname'] == 'lastname')?> <th>LASTNAME</th> <?PHP } ?>
</tr>
<?PHP while($row = mysql_fetch_array($result2)) { ?>
<tr>
<td> <?PHP echo $row['IDNO'] ?>
<td> <?PHP echo $row['YEAR'] ?></td>
<td> <?PHP echo $row['SECTION'] ?></td>
<?PHP if ($_POST['lname'] == 'lastname')?> <th><?PHP echo $row['LASTNAME'] ?></th> <?PHP } ?>
</tr>
<?PHP } ?>
</table>
<?PHP } ?>
<?PHP mysql_close($con); ?>
答
你应该做几件事情:
首先,你应该从拆分程序逻辑显示代码。 你真的应该使用模板引擎就像smarty
在第二个步骤,你应该将你的数据库代码到一个单独的类。
通过这种分离,您可以更清晰,更好地阅读源代码。 现在,您可以更好地对您的不同业务案例做出反应。 使用被调用的PHP文件作为控制器,充当数据检索和输出之间的代理。
不是一个答案,但是你想用'&&'而不是'和',你需要调查sql注入攻击,你真的不想在你的internet上发布你的根MYSQL密码:) – oedo 2010-04-26 09:44:28
Imho你不应该回应HTML标签...这只是简单的丑陋。只要走出PHP,做你的HTML,然后再次启动一个PHP块。但这只是一个风格问题。 – Bas 2010-04-26 09:45:55
我在您的查询中看不到任何区别。它应该是平等的吗?或者应该有不同的WHERE子句? – 2010-04-26 09:47:33