【发布时间】:2014-06-17 21:42:22
【问题描述】:
我试图将所有 PHP 文件与 HTML 文件分开。一种基于模板的项目,但没有使用任何模板引擎,因为它们大多臃肿,而且你需要学习另一种完全不是 PHP 的语言。
无论如何,我的index.php 文件中有以下代码:
<?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"; ?>
我的template.php 文件中有这段代码:
<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>
当我运行代码时,我基本上得到了与您在上面看到的完全一样的 HTML 页面。所以 MySQL 数据库中没有显示任何数据!
编辑:
我尝试在我的while 循环中使用以下代码,但还是一样:
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$shipping = $row["shipping"];
$category = $row["category"];
有人可以帮我解决这个问题吗?
【问题讨论】:
-
你在哪里定义了
$product_name?$product_name也不在 php 标签之间。 -
@Daan,我想,我不需要这样做,因为我使用的是
while loop。所以应该从while loop的mysql数据库中提取所有内容。 -
与其重新发明轮子,不如看看Mustache / MustachePHP。
-
@user3592614 搜索 MVC
-
@user3592614 如果不使用丝网印刷方法,您的变量内容应该如何打印在屏幕上?听起来像是对更多轮子的重新发明......说真的,你有多少(PHP)编码经验?