dongqian9013 2017-08-14 10:27
浏览 36
已采纳

为什么我的while循环在主文件中工作,但是如果我尝试从单独的文件中包含它呢?

I have a while loop that works exactly the same on three different pages. To make it easier to alter it, I put it in it's own php file, gmldLoop. However, after doing so and including gmldLoop where I'd like it to run on the file, g, it doesn't work. I'm not sure why this is happening. The code is exactly copied and pasted in both files, so I know it's not a typo.

The code that works on 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
 }
?>

The code from 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
 }
?>

The code in g.php if I use the include 'gmldLoop.php' :

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

1条回答 默认 最新

  • dongyo7931 2017-08-14 10:42
    关注

    Include

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

    in gmldLoop file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面