【问题标题】:How to include a seperate header file?如何包含单独的头文件?
【发布时间】:2019-08-04 19:59:07
【问题描述】:

嘿,所以我目前正在处理一个 html/php 页面。

我有一个应该包含在我的页面中的头文件,但由于某种原因它无法正常工作,我不知道,我对所有其他页面都做了同样的事情,它已经工作了,所以我没有不知道为什么突然不行了。

<?php
include 'header.php';
?>

<center>
<p>
This page utilizes several postgreSQL method calls.  Such as pg_connect(),
pg_query(), and pg_fetch_result().
</p>
<!-- setup the table -->
<table border="1" width="75%">
    <tr><th width="50%">Make</th><th width="15%">Model</th><th width="20%">Year</th><th width="15%">MSRP</th></tr>
<?php
$output = ""; //Set up a variable to store the output of the loop
//connect
$conn = pg_connect("host=127.0.0.1 dbname=slotegraafd_db user=slotegraafd password=100658347" );

//issue the query
$sql = "SELECT automobiles.make, automobiles.model, automobiles.year, automobiles.msrp
            FROM automobiles
            ORDER BY automobiles.year ASC";
    $result = pg_query($conn, $sql);
    $records = pg_num_rows($result);

//generate the table
    for($i = 0; $i < $records; $i++){  //loop through all of the retrieved records and add to the output variable
        $output .= "\n\t<tr>\n\t\t<td>".pg_fetch_result($result, $i, "Make")."</td>";
        $output .= "\n\t\t<td>".pg_fetch_result($result, $i, "Model")."</td>";
        $output .= "\n\t\t<td>".pg_fetch_result($result, $i, "Year")."</td>";
        $output .= "\n\t\t<td>".pg_fetch_result($result, $i, "msrp")."</td>\n\t</tr>";
    }

    echo $output; //display the output
?>
</table>
<!-- end the table -->
</center>
</body>
</html>

这是我完整的包含语句的代码集。没有打开的 html 或 body 标记,因为它在头文件中,因此我不应该在此页面中添加它。 有什么帮助吗?

谢谢。

【问题讨论】:

  • 你这是什么意思?

标签: php html web include


【解决方案1】:

确保文件路径正确。如果这个新文件在一个文件夹中,那么您需要更改您的 include 语句。

例如,如果这个新文件位于名为 foo 的文件夹中,您可以说:include '/foo/header.php

或者你可以使用相对路径:include ../header.php

如果需要,请阅读文件路径。

【讨论】:

  • 很高兴听到这个消息!在查看此内容时,您可能需要考虑不要将 body 标记放在 header.php 文件中。这可能会导致问题或混乱。我只说一些,因为我之前这样做过,后来发现它使项目更加混乱。
【解决方案2】:

它应该像您的代码一样工作。可能你的 header.php 路径不正确,或者内容无法显示/执行所以什么都没有显示。

【讨论】:

    猜你喜欢
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 2012-05-24
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多