将 HTML 文件与 PHP 文件分开(基于模板)

编程入门 行业动态 更新时间:2024-10-20 08:00:39
本文介绍了将 HTML 文件与 PHP 文件分开(基于模板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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

<?php$query = "SELECT id FROM products ORDER by id";$product_list = "";如果 ($stmt = mysqli_prepare($db_conx, $query)) {/* 执行语句 */mysqli_stmt_execute($stmt);/* 绑定结果变量 */mysqli_stmt_bind_result($stmt, $id);/* 取值 */而 (mysqli_stmt_fetch($stmt)) {$product_list .= "}}?><?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() 函数来解决这个问题.然后它将开始作为您正在寻找的控制器 - 查看架构.

例如:

<?php$query = "SELECT id FROM products ORDER by id";$product_list = "";如果 ($stmt = mysqli_prepare($db_conx, $query)) {/* 执行语句 */mysqli_stmt_execute($stmt);/* 绑定结果变量 */mysqli_stmt_bind_result($stmt, $id);/* 取值 */而 (mysqli_stmt_fetch($stmt)) {$product_list .= "";}}$data['product_list'] = $product_list;$view = 'template.php';loadTemplate($view, $data);函数 loadTemplate($view, $data){提取($数据);包括($模板);}?>

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

工作示例:

<?php$data['test'] = '这是一个工作示例';$view = 'workingExampleView.php';loadTemplate($view,$data);函数 loadTemplate($viewName,$viewData){提取($viewData);包括($viewName);}?>

创建一个名为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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 06:29:03,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   模板   HTML   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!