我有一个while循环,在三个不同的页面上完全一样。为了使其更容易更改,我将其放在自己的php文件gmldLoop中。但是,这样做之后,在我希望gmldLoop在文件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文件中。

10-04 15:32