将 HTML 文件与 PHP 文件分开(基于模板) [英] Keeping HTML files separated from the PHP files (template based)

查看:28
本文介绍了将 HTML 文件与 PHP 文件分开(基于模板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将所有 PHP 文件与 HTML 文件分离.某种基于模板的项目,但不使用任何模板引擎,因为它们大多臃肿,而且您需要学习另一种完全不是 PHP 的语言.

无论如何,我的 index.php 文件中有以下代码:

<?php 包含 "template.php";?>

我的 template.php 文件中有此代码:

<头><身体>

<div class='center_prod_box'><div class='product_title'><a href='#'>$product_name</a></div><div class='product_img'><a href='#'><img src='images/" . $id . "Image1.jpg' alt='' border='0'/></a></div><div class='prod_price'><span class='reduce'>350$</span><span class='price'>270$</span></div>

<div class='prod_details_tab'><a href='#' class='prod_buy'>加入购物车</a><a href='#' class='prod_details'>详情</a>

</html>

当我运行代码时,我基本上得到了与您在上面看到的完全一样的 HTML 页面.所以没有显示来自 MySQL 数据库的数据!

我尝试在我的 while 循环中使用以下代码,但仍然相同:

$id = $row["id"];$product_name = $row["product_name"];$price = $row["price"];$shipping = $row["shipping"];$category = $row["category"];

有人可以帮我解决这个问题吗?

解决方案

你需要使用extract() 函数来解决这个问题.然后它将开始作为您正在寻找的控制器 - 查看架构.

例如:

然后在视图部分直接使用$product_list.应该这样做.

工作示例:

创建一个名为workingExampleView.php 的新文件:

<身体><span><?php echo $test;?></span></html>

I am trying to keep all the PHP files separated from the HTML files. Sort of a template based project but without using any template engines as they are mostly bloated and the fact that you will need to learn another language which is not PHP at all.

Anyway, I have the following code in my index.php file:

<?php

$query = "SELECT id FROM products ORDER by id";

$product_list = "";
if ($stmt = mysqli_prepare($db_conx, $query)) {

    /* execute statement */
    mysqli_stmt_execute($stmt);

    /* bind result variables */
    mysqli_stmt_bind_result($stmt, $id);

    /* fetch values */
    while (mysqli_stmt_fetch($stmt)) {
        $product_list .= "

}
}

?>
<?php include "template.php"; ?>

And I have this code in my template.php file:

<html>
<head>
</head>

<body>

<div class='prod_box'>
        <div class='center_prod_box'>
          <div class='product_title'><a href='#'>$product_name</a></div>
          <div class='product_img'><a href='#'><img src='images/" . $id . "Image1.jpg' alt='' border='0' /></a></div>
          <div class='prod_price'><span class='reduce'>350$</span> <span class='price'>270$</span></div>
        </div>
        <div class='prod_details_tab'> <a href='#' class='prod_buy'>Add to Cart</a> <a href='#' class='prod_details'>Details</a> </div>
      </div>

</body>
</html>

When I run the code, I basically get the HTML page displayed exactly as you see it above. So no data is being shown from the MySQL database!

EDIT:

I tried using the following code in my while loop and still the same:

$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$shipping = $row["shipping"];
$category = $row["category"];

Could someone please help me out with this?

解决方案

You need to use the extract() function in PHP to get this sorted out. It will then start working as a Controller - View architecture that you are looking for.

For example:

<?php

$query = "SELECT id FROM products ORDER by id";
$product_list = "";

if ($stmt = mysqli_prepare($db_conx, $query)) {

    /* execute statement */
    mysqli_stmt_execute($stmt);

    /* bind result variables */
    mysqli_stmt_bind_result($stmt, $id);

    /* fetch values */
    while (mysqli_stmt_fetch($stmt)) {
        $product_list .= "";
    }
}

$data['product_list'] = $product_list;
$view = 'template.php';
loadTemplate($view, $data);

function loadTemplate($view, $data)
{
    extract($data);
    include($template);
}

?>

Then directly use $product_list in the view section. This should do it.

Working example:

<?php
    $data['test'] = 'This is a working example';
    $view = 'workingExampleView.php';
    loadTemplate($view,$data);

function loadTemplate($viewName,$viewData)
{
    extract($viewData);
    include($viewName);
}
?>

Create a new File naming it workingExampleView.php:

<html>
<body>
    <span><?php echo $test; ?></span>
</body>
</html>

这篇关于将 HTML 文件与 PHP 文件分开(基于模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆