我如何简化这个PHP脚本

时间:2010-04-26 09:43:01

标签: php mysql

关于如何简化下面的PHP脚本的任何建议?这是我之前的问题:How to check if a checkbox/ radio button is checked in php 与此相关的, 我在这里要做的是根据选中的复选框输出数据。 但是我的代码并不是很好,如果2个结果满足条件,它会显示2个表。 正如您在下面的代码中看到的,有关如何简化此操作的任何建议吗?

$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);
?>

3 个答案:

答案 0 :(得分:2)

虽然不是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); 
?> 

答案 1 :(得分:1)

你应该做几件事:

首先,您应该从程序逻辑中拆分显示代码。 你真的应该使用像smarty

这样的模板引擎

在第二步中,您应该将数据库代码移动到单独的类中。

通过这种分离,您可以获得更清洁,更好地阅读源代码。 现在,您可以更好地对不同的业务案例做出反应。 使用被调用的PHP文件作为Controller,它充当数据检索和输出之间的代理。

答案 2 :(得分:0)

您应该将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); ?>

已编辑您可以更加简化

<?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); ?>