如何以正确的HTML表格格式输出此PHP代码?

时间:2017-02-09 14:46:20

标签: php html mysql

我有以下PHP代码。我希望在适当的表格中显示输出(在我的情况下,它是学生的标记)。

<?php

include_once("config.php");

$enrollmentno = $_POST['enrollmentno'];
$enrollmentno = mysql_real_escape_string($enrollmentno); // For MySQL injection

$result = mysql_query("SELECT * FROM exam WHERE enrollmentno='$enrollmentno'");

if ($row = mysql_fetch_array($exam)) {
    echo $row['enrollmentno'];
    echo $row['studentname'];
    echo $row['schoolname'];
    echo $row['class'];
    echo $row['physics'];
    echo $row['chemistry'];
    echo $row['zeology'];
    echo $row['botany'];
    echo $row['marksscored'];
    echo $row['totalmarks'];
    echo $row['percentagescored'];
} else {
    echo "Invalid Enrollment Number";
}

?>

3 个答案:

答案 0 :(得分:0)

如果你想要一个真实的HTML table,你可以做类似的事情:

if($row = mysql_fetch_array($exam))
{
    echo "<table>"; # Begin the table
    echo "<tr>"; # Begin a row
    echo "<td>" . $row['enrollmentno'] . "</td>"; # First column
    ...
    echo "<td>" . $row['percentagescored'] . "</td>"; # Last column
    echo "</tr>"; # Close the row
    echo "</table>"; # Close the table
}

答案 1 :(得分:0)

请尝试使用此if()声明:

  echo '<table>';
if($row = mysql_fetch_array($exam))
{
echo '<tr>';
echo '<td>'.$row['enrollmentno'].'</td>';
echo '<td>'.$row['studentname'].'</td>';
echo '<td>'.$row['schoolname'].'</td>';
echo '<td>'.$row['class'].'</td>';
echo '<td>'.$row['physics'].'</td>';
echo '<td>'.$row['chemistry'].'</td>';
echo '<td>'.$row['zeology'].'</td>';
echo '<td>'.$row['botany'].'</td>';
echo '<td>'.$row['marksscored'].'</td>';
echo '<td>'.$row['totalmarks'].'</td>';
echo '<td>'.$row['percentagescored'].'</td>';
echo '</tr>';
}
echo '</table>';

答案 2 :(得分:0)

<?php if ($row = mysql_fetch_array($exam)) : ?>

<table>
    <tr>
        <td><?= $row['enrollmentno']; ?></td>
        <td><?= $row['studentname']; ?></td>
        <td><?= $row['schoolname']; ?></td>
        <td><?= $row['class']; ?></td>
        <td><?= $row['physics']; ?></td>
        <td><?= $row['chemistry']; ?></td>
        <td><?= $row['zeology']; ?></td>
        <td><?= $row['botany']; ?></td>
        <td><?= $row['marksscored']; ?></td>
        <td><?= $row['totalmarks']; ?></td>
        <td><?= $row['percentagescored']; ?></td>
    </tr>
</table>

<?php else : ?>

<p>Invalid Enrollment Number</p>

<?php endif; ?>