【发布时间】: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