将PHP代码解压缩到Separate文件中

时间:2015-07-12 08:22:23

标签: php html

任何人都可以帮我将以下php代码解压缩到一个单独的文件中吗?以下是代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div id="wrapperDiv" style="cursor:pointer;width:200px;height:350px;border:1px solid;background:green;">
        <div class="innerDiv" id="topInnerDiv" style="width:200px;height:20px;border:1px solid;background:yellow;">
            <span>Main Topic Image</span>
        </div>
        <div class="innerDiv" id="mainInnerDiv" style="width:200px;height:210px;border:1px solid;background:green;">
            <span>Grayed out Topic Image</span>
        </div>
        <div class="innerDiv" id="midInnerDiv" style="width:200px;height:20px;border:1px solid;background:blue;">
            <span>rating by star</span>
        </div>
        <div class="innerDiv" id="bottomInnerDiv" style="width:200px;height:100px;border:1px solid;background:red;">
            <div class="rating" value="1" id="rating1" style="border:0px;float:left;width:30px;height:50px;background:blue">
                <span>1</span>
            </div>
            <div class="rating" value="2" id="rating2" style="border:0px;float:left;width:30px;height:50px;background:black">
                <span>2</span>
            </div>
            <div class="rating" value="3" id="rating3" style="border:0px;float:left;width:30px;height:50px;background:pink">
                <span>3</span>
            </div>
            <div class="rating" value="4" id="rating4" style="border:0px;float:left;width:30px;height:50px;background:purple">
                <span>4</span>
            </div>
            <div class="rating" value="5" id="rating5" style="border:0px;float:left;width:30px;height:50px;background:white">
                <span>5</span>
            </div>
        </div>
    </div>

以下是数据库连接文件,仅供参考:

<?php
//ini_set('display_errors', true);//Set this display to display  all erros while testing and developing the script
//////////////////////////////
require "config.php"; // Database Connection 

echo "<!doctype html public \"-//w3c//dtd html 3.2//en\">
<html>
<head>
<title>Demo script from example.com</title>
</head>
<body>
";

echo "<input id=\"city\" list=\"city1\" >
<datalist id=\"city1\" >";

////  Collect options from table ///
$sql="select city from city ";  // Query to collect records
foreach ($dbo->query($sql) as $row) {
echo  "<option value=\"$row[city]\"/>"; // Format for adding options 
}
////  End of data collection from table /// 

echo "</datalist>";
?>

<center>
<br><br>a href='http://www.example.com' rel='nofollow'>example.com : Footer text.</a></center>

</body>

</html>

我只想在主显示页面中提取PHP代码,以获得更清晰的代码结构。

1 个答案:

答案 0 :(得分:1)

通常,这样做的方法是将您的HTML放入&#34;模板&#34;和你的PHP代码在另一个文件中。您可以使用功能齐全的模板引擎,例如SmartyPlates,也可以将HTML放在单独的PHP文件中。我们假设您的主文件名为myfile.php。创建一个名为myfile.template.php的新文件。

myfile.php

<?php
require "config.php";

$cities = array();

$sql = "SELECT city FROM city";

foreach ($dbo->query($sql) as $row)
{
    $cities[] = $row['city'];
}

include('myfile.template.php');

myfile.template.php

<!DOCTYPE html public "-//w3c//dtd html 3.2//en">
<html>
    <head>
        <title>Demo script from example.com</title>
    </head>

    <body>
        <input id="city" list="city1" />
        <datalist id="city1" >
            <?php foreach ($cities as $city): ?>
                <option value="<?php echo $city; ?>" />
            <?php endforeach; ?>
        </datalist>

        <div align="center">
            <br><br>
            <a href='http://www.example.com' rel='nofollow'>example.com : Footer text.</a>
        </div> 
    </body>
</html>