我使用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结果
答案 0 :(得分:0)
尝试使用以下代码替换<tbody>...</tbody>
中的代码部分:
<?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;?>