簡體   English   中英

如何從 PHP 和 PHP 文件中排除 HTML 仍然能夠與 Z4C4AD5FCA2E7A3F74DBB1CED003 文件鏈接?

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

查看表<--這是我的例子。 並且還提供了代碼。 我需要將 HTML 與 PHP 分開,將 HTML 移動到另一個文件,但我的 Z2FEC392304A5C23AC138DA228 仍然可以鏈接到它的代碼。 有什么想法嗎? 我正在嘗試制作類似於 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>```

將 php 和 html 分開是一個好的開始,它可以幫助您了解轉換為 OOP 和 MVC 的下一步。

在這一點上,MVC 過於寬泛,無法在這里給出簡單的答案,但我建議將其作為第一步,因為它具有基本原則:

  1. PHP 始終在頂部; 在完成所有邏輯之前,永遠不要 output 任何事情

  2. 負載配置

  3. 使用用戶輸入並在 POST 時重定向

  4. 執行業務邏輯

  5. 退出 PHP 和 output 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 切片並將所有業務邏輯移動到 model object 中。

之后,您可以將模板移動到自己的文件中,並將 model 注入到視圖中,讓視圖可以訪問所需的信息,然后渲染 output。

同時,您需要一個路由器(所有流量都重新路由到 index.php)和 controller,並確定您將使用哪種 MVC 解釋;)

這是一個非常基本的解決方案。

制作一個 HTML 文件作為模板。 例如“模板.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

源未經測試,但應該給你一個很好的起點。 我把它保持得很原始。 如果我真的這樣做,我會通過使用正則表達式來允許評論占位符中的空格,但現在它已經足夠好了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM