代码之家  ›  专栏  ›  技术社区  ›  xtian

描述一个PHP页面模板如何将另一个PHP页面写为HTML

php
  •  -3
  • xtian  · 技术社区  · 15 年前

    描述一个php/mysql页面(a.php)的php相关术语中的机制,该页面将1)使用一个模板来编写自己(简单),2)从用户处获取输入以更新数据库(简单),3)在命令解析另一个php页面(b.php)时(???)并将(b.php)页面保存为静态HTML(b.html)(???).

    update=我在这里找到了一篇帖子,它很有帮助地建议(对另一个,groan,非uber极客来说,这是一个非常普通的问题),他可以使用输出缓冲区从PHP页面捕获HTML。这是否适用于其他PHP文件?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Ezku    15 年前

    每个问题都有更复杂和更好的答案,但我要记下最简单的答案。

    1. PHP是一种模板语言,所以使用模板的PHP文件就是您的答案。这个问题有点含糊。
    2. 使用$\u get或$\u post superglobals访问用户提供的数据,具体选择取决于您的HTTP请求方法。基本上,get用于URL数据,post用于表单数据。一旦你有了数据,就验证它。然后使用 PDO 连接到数据库并执行插入查询。
    3. 您可以使用输出缓冲区,如下所示:
      ob_start(); // Start output buffer
      require 'B.php'; // Execute B.php, storing its output to the buffer
      file_put_contents('B.html', ob_get_clean()); // Clean the buffer, retrieve its contents and write them to B.html
      
        2
  •  0
  •   xtian    14 年前

    在这个问题上我被激怒了,这使我很难过。为了证明我的问题是真诚的,我用一个简单的解决方案来回答我自己的问题。我创建了generate.php以便在内容发生更改时运行。不需要缓存。

    // the switch...
    $update_live = isset($_GET['update_live']) ? TRUE : FALSE;
    // $adminPath, $livePath, $adminUrl are set in an include and contains site config data...
    $tempfile = $adminPath . 'tempindex.html'; //a temp file...
    $livefile = $livePath . 'index.html'; //the static live file...
    $this_template = $adminUrl . 'main-index.php'; //the php template file...
    $username = "php_admin";
    $password = "123xyz456";
    
    if(!($update_live)){
            $errors[] = "Did not submit from an edit page. You can only access this page by referral!";
    }else{
            if(file_exists($tempfile)){
                    unlink($tempfile);
            }
            /* =3, $html = file_get_contents($this_template, false, $context);*/
            $html = file_get_contents($this_template);
            if($html === false){
                    $errors[] = "Unable to load template. Static page update aborted!";
                    exit();
            }
            if(!file_put_contents($tempfile, $html)){
                    $errors[] = "Unable to write $tempfile. Static page update aborted!";
                    exit();
            }
    
            if(!copy($tempfile, $livefile)){
                    $errors[] = "Unable to overwrite index file. Static page update aborted!";
                    exit();
            }
            if(!unlink($tempfile)){
                    $errors[] = "Unable to delete $tempfile. Static page update aborted!";
                    exit();
            }
    }
    
    推荐文章