gpt4 book ai didi

php - Echo forloop 从 phpmyadmin sql 检索到 div (PHP 文件)

转载 作者:行者123 更新时间:2023-11-29 10:27:13 25 4
gpt4 key购买 nike

我在 mysql 中存储了值/数据,我想在 div 中回显它们。就像在表格中显示一样。但是,当我这样做时,它只会回显第一个数据,而不是全部数据。请指教。

     <?php
$sql = "SELECT budget_id, item, budget, paid, spent FROM budget WHERE title ='attireandaccssories' ORDER BY budget_id ";
$resultset = mysqli_query($con, $sql) or die("database error:". mysqli_error($con));
$rowcount=mysqli_num_rows($resultset);
for ($i=0;$i<$rowcount;$i++){
$row = mysqli_fetch_assoc($resultset);
$budget_id[$i]=$row['budget_id'];
$item[$i]=$row['item'];
$budget[$i]=$row['budget'];
$paid[$i]=$row['paid'];
$spent[$i]=$row['spent'];

}
?>


<?php
for($i=0;$i<$rowcount;$i++){
echo "$rowcount";
?>
<div id="itemcomponent-attireandaccssories">
<div id="itemcomponentjscontainer-attireandaccssories">
<div id="itemcomponenttitle"><?php echo $item[$i]; ?></div>
<div id="itemcomponentdelete" budget_id=<?php echo $budget_id[$i]; ?> ><i
class="fa fa-trash-o" aria-hidden="true fa-2x"></i></div>
<div id="itemcomponentspent"><?php echo $spent[$i]; ?></div>
<div id="itemcomponentpaid"><?php echo $paid[$i]; ?></div>
<div id="itemcomponentbudget" ><?php echo $budget[$i]; ?> </div>
<div style="clear: both;" id="clearall" class="clearall"></div>
</div>
</div>
<?php } ?>

最佳答案

根据PHP documentation您需要使用 while,此代码可以完美运行:

<?php 

$sql = "SELECT budget_id, item, budget, paid, spent FROM budget WHERE title ='attireandaccssories' ORDER BY budget_id ";

$resultset = mysqli_query($con, $sql) or die("database error:". mysqli_error($con));

while ($row = mysqli_fetch_array($resultset)){ ?>

<div id="itemcomponent-attireandaccssories">
<div id="itemcomponentjscontainer-attireandaccssories">
<div id="itemcomponenttitle"><?php echo $row['item']; ?></div>
<div id="itemcomponentdelete" budget_id="<?php echo $row['budget_id']; ?>" ><i
class="fa fa-trash-o" aria-hidden="true fa-2x"></i></div>
<div id="itemcomponentspent"><?php echo $row['spent']; ?></div>
<div id="itemcomponentpaid"><?php echo $row['paid']; ?></div>
<div id="itemcomponentbudget" ><?php echo $row['paid']; ?> </div>
<div style="clear: both;" id="clearall" class="clearall"></div>
</div>
</div>

<?php
}
?>

关于php - Echo forloop 从 phpmyadmin sql 检索到 div (PHP 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48080402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com