分离php

时间:2015-03-04 16:36:22

标签: php

这是一个显示产品详细信息的php文件(使用产品ID)。

<?php require 'config/config.php'; ?>
<!DOCTYPE html>
<head>
<title>Untitled Document</title>
</head>

<body>
<?php
    //Get all details from the products table for the product selected 
    $sql = "SELECT productid, productsku, productname, productimage WHERE productid = \"" . $connect->real_escape_string($_GET['id']) . "\" LIMIT 0,1";
    $result = $connect->query($sql);
    $row = $result->fetch_assoc();

    extract($row);
    echo $productname;
    echo '<img src=../lbsystem/'.$productimage.' width="210" height="240">';
    echo $productname; 
    echo $productsku; 
    ?>
</body>
</html>

我已将html与php分开并将它们放在两个不同的文件夹中。

喜欢,folderA / code.php

    <?php
    require 'config/config.php'; ?>
    //Get all details from the products table for the product selected 
    $sql = "SELECT productid, productsku, productname, productimage WHERE productid = \"" . $connect->real_escape_string($_GET['id']) . "\" LIMIT 0,1";
    $result = $connect->query($sql);
    $row = $result->fetch_assoc();

    extract($row);
    echo $productname;
    echo '<img src=../lbbosystem/'.$productimage.' width="210" height="240">';
    echo $productname; 
    echo $lbproductsku; 
    ?>

和folderB / productdetails.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

</body>
</html>

如何使用产品ID将它们组合在一起以生成显示?请帮忙。

2 个答案:

答案 0 :(得分:1)

文件夹层次结构:

root/
  |----index.php
  |----folderA
  |       |----code.php
  |----folderB
  |       |----productdetails.html

<强> Code.php

 <?php
    require 'config/config.php'; ?>
    //Get all details from the products table for the product selected 
    $sql = "SELECT productid, productsku, productname, productimage WHERE productid = \"" . $connect->real_escape_string($_GET['id']) . "\" LIMIT 0,1";
    $result = $connect->query($sql);
    $row = $result->fetch_assoc();

    extract($row);
    echo $productname;
    echo '<img src=../lbbosystem/'.$productimage.' width="210" height="240">';
    echo $productname; 
    echo $lbproductsku; 
    ?>

<强> Productdetails.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
        [content]
</body>
</html>

<强>的index.php

<?php

ob_start();
include_once("folderA/code.php");
$codes = ob_get_contents();
ob_end_clean();

$html=file_get_contents('folder/productdetails.html');
$html_with_code=str_replace("[content]", $codes, $html);

echo $html_with_code;

?>

答案 1 :(得分:-1)

如果您正在寻找模板系统,您应该查看Smarty(http://www.smarty.net/

代码看起来像这样

require 'config/config.php'; 
require 'smarty/libs/Smarty.class.php';
//Get all details from the products table for the product selected 
$sql = "SELECT productid, productsku, productname, productimage WHERE productid = \"" . $connect->real_escape_string($_GET['id']) . "\" LIMIT 0,1";
$result = $connect->query($sql);
$row = $result->fetch_assoc();

extract($row);
$smarty = new Smarty();
$smarty->assign("productname", $productname);
$smarty->assign("productimage", $productimage);
$smarty->assign("lbproductsku", $lbproductsku); 
$smarty->display("template.tpl");

而template.tpl就是这样的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
    {$productname}<br />
    <img src="{$productimage}" />
    {$lbproductsku}
</body>
</html>