gpt4 book ai didi

php - 为什么我的 while 循环在主文件中工作,但如果我尝试从单独的文件中包含它则不起作用?

转载 作者:行者123 更新时间:2023-11-29 10:39:58 25 4
gpt4 key购买 nike

我有一个 while 循环,它在三个不同的页面上的工作原理完全相同。为了更容易更改它,我将其放入自己的 php 文件 gmldLoop.php 中。但是,这样做并在我希望它在文件 g 上运行的地方包含 gmldLoop 后,它不起作用。我不知道为什么会发生这种情况。该代码是完全复制并粘贴到两个文件中的,所以我知道这不是拼写错误。

适用于 g.php 的代码:

$query = "SELECT * FROM guidelines ORDER BY sortingLetter ASC, age ASC, category ASC";
$data= mysqli_query($db_conn, $query);

while ($row = mysqli_fetch_array($data)) {
$id = $row['id'];
$age = $row['age'];
$cat = $row['category'];
$title = $row['title'];
$desc = $row['description'];
$ageRange = $row['ageRange'];
$sortingLetter = $row['sortingLetter'];

?>
<section class="content
<?php
switch($ageRange){
case 1:
echo "one";
break;
case 12:
echo "one";
break;
case 123456:
echo "all";
break;
case 2:
echo "two";
break;
case 23:
echo "two three";
break;
case 23456:
echo "two three four five six";
break;
case 3:
echo "three";
break;
case 34:
echo "three four";
break;
case 4:
echo "four";
break;
case 45:
echo "four five";
break;
case 5:
echo "five";
break;
case 56:
echo "five six";
break;
case 6:
echo "six";
break;
case 0:
echo "all";
break;
}
?>"
>
<h2 class="heading noborder">
<div class="title textfloatL">
<small><strong><?php echo $title; ?></strong></small>
</div>

</h2>

<br><br>
<div class="sqldata">
<div class="desc">
<?php echo $desc; ?>
</div>
<div class="agesdiv incap textfloatR">
<p class="inup">
<small><?php if (!$age) {
echo 'All Ages';
} else {
echo $age;
} ?></small>
</p>
|
<p class="incap">
<small><?php echo $cat; ?></small>
</p>
</div>
<br>
</div>
</section>
<?php
}
?>

来自 gmldLoop 的代码:

    $data= mysqli_query($db_conn, $query);

while ($row = mysqli_fetch_array($data)) {
$id = $row['id'];
$age = $row['age'];
$cat = $row['category'];
$title = $row['title'];
$desc = $row['description'];
$ageRange = $row['ageRange'];
$sortingLetter = $row['sortingLetter'];

?>
<section class="content
<?php
switch($ageRange){
case 1:
echo "one";
break;
case 12:
echo "one";
break;
case 123456:
echo "all";
break;
case 2:
echo "two";
break;
case 23:
echo "two three";
break;
case 23456:
echo "two three four five six";
break;
case 3:
echo "three";
break;
case 34:
echo "three four";
break;
case 4:
echo "four";
break;
case 45:
echo "four five";
break;
case 5:
echo "five";
break;
case 56:
echo "five six";
break;
case 6:
echo "six";
break;
case 0:
echo "all";
break;
}
?>"
>
<h2 class="heading noborder">
<div class="title textfloatL">
<small><strong><?php echo $title; ?></strong></small>
</div>

</h2>

<br><br>
<div class="sqldata">
<div class="desc">
<?php echo $desc; ?>
</div>
<div class="agesdiv incap textfloatR">
<p class="inup">
<small><?php if (!$age) {
echo 'All Ages';
} else {
echo $age;
} ?></small>
</p>
|
<p class="incap">
<small><?php echo $cat; ?></small>
</p>
</div>
<br>
</div>
</section>
<?php
}
?>

如果我使用 include 'gmldLoop.php',则 g.php 中的代码:

<?php
$query = "SELECT * FROM guidelines ORDER BY sortingLetter ASC, age ASC, category ASC";
include $_SERVER["DOCUMENT_ROOT"] . '/inc/pageStructure/gmldLoop.php';
?>

最佳答案

包括

 $query = "SELECT * FROM guidelines ORDER BY sortingLetter ASC, age ASC, category ASC";

在 gmldLoop 文件中。

关于php - 为什么我的 while 循环在主文件中工作,但如果我尝试从单独的文件中包含它则不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45672486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com