【问题标题】:Issue displaying data to table by using PDO and nested while loop使用 PDO 和嵌套的 while 循环将数据显示到表中
【发布时间】:2018-03-18 15:26:29
【问题描述】:

我正在使用 while 循环将数据显示到表中。我正在尝试使用 PDO::FETCH_ASSOC 语句显示所有父元素和子元素。

使用while 循环,我只显示第一个父类别和第一个子类别。

这是我的数据库结构:顶级父母设置为 0,ID 自动递增,子类别设计为与父母 ID 链接: DB Categories table

我正在学习 mysqli 教程,但想以 PDO 方式制作我的应用程序。

mysqli 代码工作示例

<?php $result = $db->query("SELECT * FROM categories WHERE parent = 0"); ?>
<?php while ($parent = mysqli_fetch_assoc($result)): ?>
    <?php
    $parent_id = (int) $parent['id'];
    $cresult = $db->query("SELECT * FROM categories WHERE parent = '{$parent_id}'");
    ?>
    <tr class="bg-primary">
        <td><?php echo $parent['category']; ?></td>
        <td>Parent</td>
        <td>
            <a class="btn btn-xs btn-default" href="categories.php?edit=<?php echo $parent['id']; ?>">
                <span class="glyphicon glyphicon-pencil"></span>
            </a>
            <a class="btn btn-xs btn-default" href="categories.php?delete=<?php echo $parent['id']; ?>">
                <span class="glyphicon glyphicon-remove-sign"></span>
            </a>
        </td>
    </tr>
    <?php while ($child = mysqli_fetch_assoc($cresult)): ?>
        <tr class="bg-info">
            <td><?php echo $child['category']; ?></td>
            <td><?php echo $parent['category']; ?></td>
            <td>
                <a class="btn btn-xs btn-default" href="categories.php?edit=<?php echo $child['id']; ?>">
                    <span class="glyphicon glyphicon-pencil"></span>
                </a>
                <a class="btn btn-xs btn-default" href="categories.php?delete=<?php echo $child['id']; ?>">
                    <span class="glyphicon glyphicon-remove-sign"></span>
                </a>
            </td>
        </tr>
    <?php endwhile; ?>
<?php endwhile; ?>
</tbody>
</table>
</div>  
</div>

我尝试过的 PDO 方式

<?php
include_once "../config.php";
$izraz = $veza->prepare("SELECT * FROM categories WHERE parent = 0;");
$izraz->execute();
?>

<div class="row">
    <div class="columns">
        <h2>Categories</h2>
        <p>To stack a table on small screens, add the class <code>.stack</code></p>
        <table class="stack" >
            <thead>
                <tr>
                    <th width="300">Category</th>
                    <th>Parent</th>
                    <th width="150">Table Header</th>
                    <th width="150">Table Header</th>
                </tr>
            </thead>
            <tbody >
                <?php while ($parent = $izraz->fetch(PDO::FETCH_ASSOC)):
                    $parent_id = (int) $parent['id'];
                    $sql2 = "SELECT * FROM categories WHERE parent='$parent_id'";
                    $edit_result = $izraz=$veza->prepare($sql2);
                    $cresult = $izraz->execute();
                    print_r($cresult);
                ?>
                <tr>
                    <td><?=$parent['category'];?></td>
                    <td>Parent</td>
                    <td>
                        <a href="categories.php?edit=<?=$parent['id'];?>" class ="button tiny"><i class="far fa-edit fa-2x"></i></a>
                        <a href="categories.php?delete=<?=$parent['id'];?>" class ="button tiny"><i class="far fa-trash-alt fa-2x"></i></a>
                    </td>
                </tr>

//child
                <?php while($child = $izraz->fetch(PDO::FETCH_ASSOC)): ?>
                    <tr>
                        <td><?=$child['category'];?></td>
                        <td><?=$parent['category'];?></td>
                        <td>
                            <a href="categories.php?edit=<?=$child['id'];?>" class ="button tiny"><i class="far fa-edit fa-2x"></i></a>
                            <a href="categories.php?delete=<?=$child['id'];?>" class ="button tiny"><i class="far fa-trash-alt fa-2x"></i></a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            <?php endwhile;?>
            </tbody>
        </table>
    </div>
</div>
<?php include_once 'includes/scripts.php';?>

工作示例Expected result

我的APP 结果

【问题讨论】:

  • 您似乎对这两个查询都使用了$izraz,这可能会导致不可预知的行为。尝试使用不同的变量名称,例如 $izraz2 用于第二个查询。
  • 我试过了,但我的机器死机了,不得不强制退出一切......
  • 你知道它是否进入了无限循环吗?
  • 是的,我认为是正确的,仍然找不到解决方案

标签: php pdo while-loop


【解决方案1】:

尝试将&lt;tbody&gt;...&lt;/tbody&gt; 中的代码部分替换为以下代码:

<?php while ($parent = $izraz->fetch(PDO::FETCH_ASSOC)):
    $parent_id = (int) $parent['id'];
    $sql2 = "SELECT * FROM categories WHERE parent='$parent_id'";
    $izraz2 = $veza->prepare($sql2);
    $cresult = $izraz2->execute();
    ?>
    <tr>
        <td><?=$parent['category'];?></td>
        <td>Parent</td>
        <td>
            <a href="categories.php?edit=<?=$parent['id'];?>" class ="button tiny"><i class="far fa-edit fa-2x"></i></a>
            <a href="categories.php?delete=<?=$parent['id'];?>" class ="button tiny"><i class="far fa-trash-alt fa-2x"></i></a>
        </td>
    </tr>
    <!-- child -->
    <?php while($child = $izraz2->fetch(PDO::FETCH_ASSOC)): ?>
        <tr>
            <td><?=$child['category'];?></td>
            <td><?=$parent['category'];?></td>
            <td>
                <a href="categories.php?edit=<?=$child['id'];?>" class ="button tiny"><i class="far fa-edit fa-2x"></i></a>
                <a href="categories.php?delete=<?=$child['id'];?>" class ="button tiny"><i class="far fa-trash-alt fa-2x"></i></a>
            </td>
        </tr>
    <?php endwhile; ?>
<?php endwhile;?>

【讨论】:

  • 太棒了!既然它现在对你有用,你可以接受这个答案。
猜你喜欢
  • 1970-01-01
  • 2012-10-30
  • 2017-07-26
  • 2018-11-28
  • 1970-01-01
  • 2020-06-23
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
相关资源
最近更新 更多