Any suggestions on how I can simplify the php script below?This was my previous question: How to check if a checkbox/ radio button is checked in php that is linked to this one, What I'm trying to do here is to output the data depending on the checkbox that is checked. But my code isn't really good, it shows 2 tables if the condition is met by the 2 results. As you can see in the code below, any suggestions on how I can simplify this?
$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);
?>
Although not a PHP person, I would do a logical based on when to include the last name column in your table. Would something like this help and keep simplified...
$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);
?>
You should do several things:
First you should split the display code from program logic. You really should use a template engine like smarty
In the second step you should move your database code to a separate class.
With this separation you get much clean and better to read source. Now you can react on you different business cases better. Use the called PHP file as Controller which acts as broker between data retrieval and output.
You should use PHP with HTML As Follows
<?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 YOU CAN MORE SIMPLIFY AS FOLLOWS
<?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); ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.