【问题标题】:How to exclude HTML from PHP and PHP file still able to link with the HTML file?如何从 PHP 和 PHP 文件中排除 HTML 仍然能够与 HTML 文件链接?
【发布时间】:2020-12-02 02:27:58
【问题描述】:

View table

<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>```

【问题讨论】:

  • MVC 是一种设计模式。尝试实施它,如果遇到任何问题,请回来。
  • 你应该看看 Laravel,它是一个使用 MVC 模式的 PHP 框架。
  • 有几个选项取决于您想要实现的目标以及如何使用它。例如,HTML 是否需要是用户可编辑的模板?我的第一个想法是让您的 HTML 有一些占位符用于将要填充的数据。即将定义一个 ,并带有某种可识别的标记供您替换为值。然后在 PHP 中你阅读 HTML 找到行模板,循环你的数据创建行的副本,然后输出。顺便说一句:你有

标签: php html model-view-controller


【解决方案1】:

分离 php 和 html 是一个好的开始,它可以帮助您了解转换为 OOP 和 MVC 的下一步。

此时的 MVC 过于宽泛,无法在这里简单回答,但我建议将其作为第一步,因为它具有基本原则:

  1. PHP 始终位于顶部;在所有逻辑完成之前永远不要输出任何东西

  2. 加载配置

  3. 使用用户输入并在 POST 时重定向

  4. 执行业务逻辑

  5. 退出 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 解释;)

【讨论】:

    【解决方案2】:

    这是一个非常基本的解决方案。

    制作一个 HTML 文件作为模板。例如。 “模板.html”。使用 HTML cmets 作为数据的占位符。 (我选择了 cmets,因为这意味着 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
    

    源未经测试,但应该给你一个很好的起点。我把它保持得很原始。如果我真的这样做了,我会通过使用正则表达式来允许评论占位符中有空格的可能性,但现在已经足够了。

    【讨论】:

    • 嗨,我出现了这个错误:注意:未定义的偏移量:在第 14 行的 /var/www/html/test/view2php.php 中的 1 已检查,这是 $rowTemplate = $m[ 1];
    • 我做了一些编辑,错误消失了,但页面显示为空白......
    • 调试器是你的朋友。单步执行代码。确保它正在创建行。最重要的是:阅读我的代码中的最后一条评论“用 html 做一些事情”。如果你的页面是空白的,可能是因为你没有。
    • 您好,先生,这就是问题所在~我不太确定如何处理 html,您能提供更多帮助吗?我在这方面很新。
    • 这里需要放东西吗? $regRowTemplate = '/(.*)/i';
    猜你喜欢
    • 2017-10-22
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    相关资源
    最近更新 更多