查看表<--这是我的例子。并且还提供了代码。我需要将HTML从PHP中分离出来,将HTML移到另一个文件中,但我的PHP代码仍然可以与之链接。有什么想法吗?我正在尝试制作类似View模型控制器的东西。
<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>```
分离php和html是一个良好的开端,可以帮助您了解转换为OOP和MVC的下一步。
MVC在这一点上过于宽泛,无法在这里给出简单的答案,但我建议将其作为第一步,因为它有以下基本原则:
-
PHP总是名列前茅;在完成所有逻辑之前,永远不要输出任何内容
-
负载配置
-
使用用户输入并在POST 时重定向
-
执行业务逻辑
-
退出PHP并输出HTML。记住,PHP本质上是一种模板语言,不妨将其用作
然后你的代码看起来像这样:
<?php
// load database connection, etc
$url = 'your url';
// deal with user input. Always use POST if data will be changed!
if($_POST['action'] == 'delete') {
// delete from admin where id=?
header('location: '.$url);
die;
}
// end "controller" section
// begin "model" section
$sel_query="Select * from admin ORDER BY id ASC;";
$result = mysqli_query($con,$sel_query);
// end "model" section
// begin "view" section.
// Note, you could simply put the html in a separate file and just include it here.
?>
<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>
</tbody>
<?php while($row = mysqli_fetch_assoc($result)): ?>
<tr>
<td align="center"><?= $row["ID"] ?></td>
<td align="center"><?= $row["username"] ?></td>
<td align="center"><?= $row["user_pass"] ?></td>
<td align="center"><?= $row["fullname"] ?></td>
<td align="center">
<form method="post">
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="<?= $row["ID"] ?>" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</body>
</html>
请注意,遵循此模式为迁移到mvc奠定了基础。但首先,开始处理oop,并将所有业务逻辑移动到一个模型对象中。
之后,您可以将模板移动到其自己的文件中,并将模型注入到视图中,这样视图就可以访问所需的信息,然后渲染输出。
同时,您需要一个路由器(所有流量都被重新路由到index.php(和控制器,并确定您将使用哪种MVC解释;(
这里有一个非常基本的解决方案。
制作一个HTML文件作为模板。例如";template.html";。使用HTML注释作为数据的占位符。(我选择了注释,因为这意味着HTML仍然是兼容的(我遗漏了一些不相关的部分,所以希望你能明白:
<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>
然后,在PHP代码中,读取html,找到行模板,并根据需要替换字段:
<?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
来源未经测试,但应该给你一个良好的起点。我把它保持得很粗糙。如果我真的这么做了,我会通过使用正则表达式来允许在注释占位符中使用空格,但现在已经足够了。