将检索到的数据库记录传递到 php 文件
Passing retrieved database records to a php file
system/article.php
<?php
$sql = "SELECT articleTitle, articleSummary, articleContent FROM articles";
$result = $dbconnect->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["articleTitle"];
echo $row["articleSummary"];
echo $row["articleContent"];
}
} else {
echo "0 results";
}
include 'template/homepage.php';
从文章 table 中检索文章。
我已经包括了应该作为模板的 homepage.php。
template/homepage.php
<?php include 'template/common/header.php'; ?>
<h1>Article Title here</h1>
<p>articleSummary</p>
<?php include 'template/common/footer.php'; ?>
我现在如何将检索到的数据传递给 homepage.php 以在浏览器上显示它?
编辑
smarber 将我指向
在第一个文件中:
global $variable;
$variable = "apple";
include('second.php');
在第二个文件中:
echo $variable;
有效。但是我如何在我的问题上实现相同的?
您可以 do that 通过 GET、Session 或 Post;但是为什么不简单有效地定义一个函数并将那些变量传递给它,例如:
function displayArticle($title, $summary, $content) {
displayHeader(); // maybe some concepts you've used in template/common/header.php
echo "<h1>$title</h1><p>$summary</p><div>$content</div>";
displayFooter(); // again, what you've provided in footer.php
}
那么,您可以执行以下操作:
将 template/homepage.php
文件更改为:
<?php
include 'template/common/header.php';
echo "<h1>$articleName</h1>";
echo "<p>$articleSummary</p>";
include 'template/common/footer.php';
?>
并将 system/article.php
更改为:
<?php
global $articleName;
global $articleSummary;
global $articleContents;
$sql = "SELECT articleTitle, articleSummary, articleContent FROM articles";
$result = $dbconnect->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$articleName = $row["articleTitle"];
$articleSummary = $row["articleSummary"];
$articleContents = $row["articleContent"];
include 'template/homepage.php';
}
} else {
echo "0 results";
}
但是,最好使用编程语言中的一些功能来创建更清晰、更可重用的代码,例如使用函数和 类 :)
system/article.php
<?php
$sql = "SELECT articleTitle, articleSummary, articleContent FROM articles";
$result = $dbconnect->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["articleTitle"];
echo $row["articleSummary"];
echo $row["articleContent"];
}
} else {
echo "0 results";
}
include 'template/homepage.php';
从文章 table 中检索文章。
我已经包括了应该作为模板的 homepage.php。
template/homepage.php
<?php include 'template/common/header.php'; ?>
<h1>Article Title here</h1>
<p>articleSummary</p>
<?php include 'template/common/footer.php'; ?>
我现在如何将检索到的数据传递给 homepage.php 以在浏览器上显示它?
编辑
smarber 将我指向
在第一个文件中:
global $variable;
$variable = "apple";
include('second.php');
在第二个文件中:
echo $variable;
有效。但是我如何在我的问题上实现相同的?
您可以 do that 通过 GET、Session 或 Post;但是为什么不简单有效地定义一个函数并将那些变量传递给它,例如:
function displayArticle($title, $summary, $content) {
displayHeader(); // maybe some concepts you've used in template/common/header.php
echo "<h1>$title</h1><p>$summary</p><div>$content</div>";
displayFooter(); // again, what you've provided in footer.php
}
那么,您可以执行以下操作:
将 template/homepage.php
文件更改为:
<?php
include 'template/common/header.php';
echo "<h1>$articleName</h1>";
echo "<p>$articleSummary</p>";
include 'template/common/footer.php';
?>
并将 system/article.php
更改为:
<?php
global $articleName;
global $articleSummary;
global $articleContents;
$sql = "SELECT articleTitle, articleSummary, articleContent FROM articles";
$result = $dbconnect->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$articleName = $row["articleTitle"];
$articleSummary = $row["articleSummary"];
$articleContents = $row["articleContent"];
include 'template/homepage.php';
}
} else {
echo "0 results";
}
但是,最好使用编程语言中的一些功能来创建更清晰、更可重用的代码,例如使用函数和 类 :)