Warm tip: This article is reproduced from serverfault.com, please click

How to exclude HTML from PHP and PHP file still able to link with the HTML file?

发布于 2020-12-02 02:27:58

View table<-- this is my example. And code is provided too. I need to separate HTML from PHP, moving HTML to another file but my PHP code still be able to link with it. Is there any idea? I am trying to make something like View model controller.

<html>
<head>
<meta charset="utf-8">
<title>View Records</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="form">
<p>
    <a href="dashboard.php">Dashboard</a> 
|   <a href="view.php">View Records</a> 
|   <a href="insert.php">Add Admin</a>
|   <a href="logout.php" onclick="return confirm('Are you sure to Logout?');">Logout</a>

</p>

<table width="100%" border="1" style="border-collapse:collapse;">
    <thead>
        <tr>
            <th><strong>ID</strong></th>
            <th><strong>Username</strong></th>
            <th><strong>User Password</strong></th>
            <th><strong>Full Name</strong></th>
            <th><strong>Edit</strong></th>
            <th><strong>Delete</strong></th>
        </tr>
    </thead>
</body>


<?php
$count=1;
$sel_query="Select * from admin ORDER BY id ASC;";
$result = mysqli_query($con,$sel_query);
while($row = mysqli_fetch_assoc($result)) { ?>


<tr>
    <td align="center"><?php echo $row["ID"]; ?></td>
    <td align="center"><?php echo $row["username"]; ?></td>
    <td align="center"><?php echo $row["user_pass"]; ?></td>
    <td align="center"><?php echo $row["fullname"]; ?></td>
    <td align="center">
        <a href="edit.php?id=<?php echo $row["ID"];?>">Edit</a></td>
    <td align="center">
        <a href="delete.php?id=<?php echo $row["ID"]; ?>"onclick="return confirm('Data cannot be retrieved after deleted. Are you sure to delete?');">Delete</a></td>


</tr>
<?php $count++; } ?>
</tbody>
</table>
</div>
</body>
</html>```
Questioner
SF_IreSearch
Viewed
0
xtempore 2020-12-02 11:12:09

Here's a very basic solution.

Make an HTML file as a template. E.g. "template.html". Use HTML comments as placeholders for the data. (I went with comments as it means the HTML remains compliant) I've left out some of the non-relevant bits, so hopefully you get the idea:

<html>
...
<table width="100%" border="1" style="border-collapse:collapse;">
<thead>
<tr>
    <th><strong>ID</strong></th>
    <th><strong>Username</strong></th>
    <th><strong>User Password</strong></th>
    <th><strong>Full Name</strong></th>
    <th><strong>Edit</strong></th>
    <th><strong>Delete</strong></th>
</tr>
</thead>
<tbody>
<!--ROW-->
<tr>
    <td align="center"><!--ID--></td>
    <td align="center"><!--username--></td>
    <td align="center"><!--user_pass--></td>
    <td align="center"><!--fullname--></td>
    <td align="center">
        <a href="edit.php?id=<!--ID-->">Edit</a></td>
    <td align="center">
        <a href="delete.php?id=<!--ID-->"onclick="return confirm('Data cannot be retrieved after deleted. Are you sure to delete?');">Delete</a></td>
</tr>
<!--ENDROW-->
</tbody>
</table>
</div>
</body>
</html>

Then, in your PHP code, you read in the html, find the row template, and replace the fields as needed:

<?php
// Read the template
$html = file_get_contents('template.html');

// Find the row template
$regRowTemplate = '/<!--ROW-->(.*)<!--ENDROW-->/i';
preg_match($regRowTemplate, $html, $m);
$rowTemplate = $m[1];

// Start building our replacement rows
$htmlRows = '';

$count=1;
$sel_query="Select * from admin ORDER BY id ASC;";
$result = mysqli_query($con,$sel_query);
while ($row = mysqli_fetch_assoc($result)) {
    // Start with a fresh copy of the template
    $htmlRow = $rowTemplate;
    // Replace comment placeholders with values
    foreach ($row as $key => $value) {
        $htmlRow .= str_replace('<!--' . $key . '-->', $value, $htmlRow);
    }
    // Append to our rows
    $htmlRows .= $htmlRow;
    $count++;
}
// Replace the row template with our expanded rows
$html = preg_replace(regRowTemplate, $htmlRows, $html);
// Do something with the html

Source untested, but should give you a good starting point. I kept it pretty raw. If I was doing this for real, I'd allow for the possibility of spaces in the comment placeholders by using a regular expression instead, but for now it's good enough.