获取另一个 PHP-File 的内容并使用当前 PHP-File 的变量
Get content of another PHP-File and use variables for current PHP-File
我想在我的 PHP-File constructor.php
中的另一个 PHP-File template.php
的 HTML-String 中使用一个变量。
我在 Whosebug 上搜索了一个解决方法来包含另一个 PHP-File 的内容。我将以下代码包含在 constructor.php
中,因为它比使用 file_get_contents();
Source:
更安全
function requireToVar($file){
ob_start();
require($file);
return ob_get_clean();
}
constructor.php
的其余部分如下所示:
...
$sqli = mysqli_query($mysqli, "SELECT ...");
if(mysqli_num_rows($sqli) > 0){
$ii = 0;
while ($row = $sqli->fetch_assoc()) {
$ii++;
if($row['dummy']=="Example"){
$content.=requireToVar('template.php');
...
template.php
看起来像这样:
<?php echo "
<div class='image-wrapper' id='dt-".$row['id']."' style='display: none;'>
...
</div>
"; ?>
constructor.php
不会将 template.php
字符串中的 var $row['id']
识别为自己的变量,也不会执行它。该变量绝对适用于 constructor.php
.
中的其他代码
如果我在 $content.=
之后将 template.php
的代码复制并粘贴到 constructor.php
中,它的工作就像一个魅力。但我想重组我的 constructor.php
,因为它变大了,这样更容易定制。
我不知道如何更准确地描述这个问题,但我希望这个标题适合我的问题。
使用 MVC 模型和渲染器函数。例如:index.php
<!DOCTYPE HTML>
<html>
<head>
<title><?=$title; ?></title>
</head>
<body>
<h3><?=$text; ?></h3>
</body>
</html>
然后我会有 PHP 个包含这些变量的数组:
$array = array("title"=>"Mypage", "text"=>"Mytext");
现在我们将在渲染器函数中使用两者
function renderer($path, $array)
{
extract($array); // extract function turn keys into variables
include_once("$path.php");
}
renderer("index", $array);
你的做法比较奇怪,但无论如何;您可以从 $GLOBALS 数组中访问 $row。
将模板写成:
<?php echo "
<div class='image-wrapper' id='dt-".$GLOBALS['row']['id']."' style='display: none;'>
...
</div>
";
?>
更新您的功能
function requireToVar($file,$row_id){
ob_start();
require($file);
return ob_get_clean();
}
所以你可以这样称呼它
while ($row = $sqli->fetch_assoc()) {
$ii++;
if($row['dummy']=="Example"){
$content.=requireToVar('template.php',$row['id']);
并在 template.php 中显示它
<div class="image-wrapper" id="dt-<?php echo $row_id; ?>" style="display: none;"></div>
我想在我的 PHP-File constructor.php
中的另一个 PHP-File template.php
的 HTML-String 中使用一个变量。
我在 Whosebug 上搜索了一个解决方法来包含另一个 PHP-File 的内容。我将以下代码包含在 constructor.php
中,因为它比使用 file_get_contents();
Source:
function requireToVar($file){
ob_start();
require($file);
return ob_get_clean();
}
constructor.php
的其余部分如下所示:
...
$sqli = mysqli_query($mysqli, "SELECT ...");
if(mysqli_num_rows($sqli) > 0){
$ii = 0;
while ($row = $sqli->fetch_assoc()) {
$ii++;
if($row['dummy']=="Example"){
$content.=requireToVar('template.php');
...
template.php
看起来像这样:
<?php echo "
<div class='image-wrapper' id='dt-".$row['id']."' style='display: none;'>
...
</div>
"; ?>
constructor.php
不会将 template.php
字符串中的 var $row['id']
识别为自己的变量,也不会执行它。该变量绝对适用于 constructor.php
.
如果我在 $content.=
之后将 template.php
的代码复制并粘贴到 constructor.php
中,它的工作就像一个魅力。但我想重组我的 constructor.php
,因为它变大了,这样更容易定制。
我不知道如何更准确地描述这个问题,但我希望这个标题适合我的问题。
使用 MVC 模型和渲染器函数。例如:index.php
<!DOCTYPE HTML>
<html>
<head>
<title><?=$title; ?></title>
</head>
<body>
<h3><?=$text; ?></h3>
</body>
</html>
然后我会有 PHP 个包含这些变量的数组:
$array = array("title"=>"Mypage", "text"=>"Mytext");
现在我们将在渲染器函数中使用两者
function renderer($path, $array)
{
extract($array); // extract function turn keys into variables
include_once("$path.php");
}
renderer("index", $array);
你的做法比较奇怪,但无论如何;您可以从 $GLOBALS 数组中访问 $row。
将模板写成:
<?php echo "
<div class='image-wrapper' id='dt-".$GLOBALS['row']['id']."' style='display: none;'>
...
</div>
";
?>
更新您的功能
function requireToVar($file,$row_id){
ob_start();
require($file);
return ob_get_clean();
}
所以你可以这样称呼它
while ($row = $sqli->fetch_assoc()) {
$ii++;
if($row['dummy']=="Example"){
$content.=requireToVar('template.php',$row['id']);
并在 template.php 中显示它
<div class="image-wrapper" id="dt-<?php echo $row_id; ?>" style="display: none;"></div>