我需要有关此for循环的帮助。我希望它为列出的每个项目在行的侧面打印控件,但要列出5个而不是2个控件。
<tbody>
<?php
//get list of supplies
$numOfRows = 0;
$result = mysqli_query($conn,"SELECT * FROM supplies");
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach ($row as $item) {
echo '<td>' . $item . '</td>';
$numOfRows ++;
}
//controls
for ($i = 0;$i <= $numOfRows; $i++) {
echo '<td><a><i class="fas fa-edit"></i></a><a><i class="fas fa-trash-alt">
</i></a></td>';
}
}
echo '</tr>';
?>
</tbody>
答案 0 :(得分:0)
查看您的代码,第二个原因是使用numOfRows变量,该变量根本不正确,您正在迭代表的行数不仅仅是5次,如果您有100条记录,那么您会看到TD打印了100次,顺便说一句,您可以将这两个按钮放到一个TD中,这样就完全不需要第二个了
答案 1 :(得分:0)
我认为控件不需要另一个循环。试试这个,希望对您有所帮助。谢谢
<?php
$result = mysqli_query($conn,"SELECT * FROM supplies");
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach ($row as $item) {
echo '<td>' . $item . '</td>';
echo '<td><a><i class="fas fa-edit"></i></a><a><i class="fas fa-trash-alt">
</i></a></td>';
}
}
}
echo '</tr>';
?>
答案 2 :(得分:0)
如果您希望每个项目在其自己的行中在第一列中显示项目($item
),在第二列中显示“编辑/删除”链接,则只需要while
就可以浏览每一行找到。
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo '<td>' . $item . '</td>';
echo '<td>
<a><i class="fas fa-edit"></i></a>
<a><i class="fas fa-trash-alt"></i></a>
</td>';
echo '</tr>';
}
答案 3 :(得分:0)
// In your code you have the
// "<tr>" element inside of your loop.
// It needs to come before your loop. Try the code below
// This is where you need <tr>
echo "<tr>";
while ($row = mysqli_fetch_assoc($result)) {
// this is where you had tr // echo <tr>
foreach ($row as $item) {
echo '<td>' . $item . '</td>';`enter code here`
$numOfRows ++;
}
//controls
for ($i = 0;$i <= $numOfRows; $i++) {
echo '<td><a><i class="fas fa-edit"></i></a><a><i class="fas fa-trash-alt">
</i></a></td>';
}
}
echo '</tr>';