回声forloop从phpmyadmin sql检索到div(PHP FILE)

时间:2018-01-03 15:30:01

标签: php html mysql

我将值/数据存储在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 } ?> 

1 个答案:

答案 0 :(得分:1)

根据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 
    }
?>