dop83362 2018-02-04 19:46
浏览 54
已采纳

PHP循环 - 无法为每个组重复关闭标记[关闭]

I can't work out where I'm going wrong here I'm afraid.

I'm trying to loop through each unique group (each unique layout - let's say theres 'short', 'medium' and 'long' in this dataset) and for each item in that group to print it within the items section.

I've got to the following code, but the problem is the footers are only printing once at the end of all of the code - I need them to print after the items in each section.

<?php $seriesgroup = ''; if (mysqli_num_rows($result3) > 0) {while($row = mysqli_fetch_assoc($result3)) {
if($row['layout'] != $seriesgroup) {  ?>

    <!-- Print group headers -->

<?php $seriesgroup = $row['layout']; } ?>

        <!-- Print group items -->

<?php } ?>

    <!-- Print footers -->

<?php } ?>
  • 写回答

2条回答 默认 最新

  • doudie2693 2018-02-04 19:55
    关注

    There is a confusion in your mind about how HTML and PHP interact. The way to do this is to use the statement echo from inside the PHP part of your code.

    Edit: Try this new version. The code is cleaner:

    <?php 
      $previousrow = array ('layout' => ''); 
    
      if (mysqli_num_rows($result3) > 0) {
        while($row = mysqli_fetch_assoc($result3)) {
          if($row['layout'] != $previousrow['layout']) { // new group
            if ($previousrow['layout'] !=== '') { // not on first row
              print_footers ($previousrow);
            }
            print_headers ($row);
          } 
          print_group_items ($row);
          $previousrow = $row;
        }
        print_footers ($previousrow);
      } 
    
    function print_group_items ($this_row) {
      echo "  <!-- Print group items -->"
    ; // will depend on $this_row
    }
    
    function print_headers ($this_row) {
      echo "<!-- Print headers -->
    "; // will depend on $this_row
    }
    
    function print_footers ($this_row) {
      echo "<!-- Print footers -->
    "; // will depend on $this_row
    }
    ?>
    

    This was the previous version and wasn't correct, due to a.o. a tipo in the line containing print_footers; without ().

    <?php 
      $seriesgroup = ''; 
      $firstgroup = 1;
    
      if (mysqli_num_rows($result3) > 0) {
        while($row = mysqli_fetch_assoc($result3)) {
          if($row['layout'] != $seriesgroup) { 
            if (! $firstgroup) {
              print_footers ();
              $firstgroup = 0;
            }
            print_headers ();
            $seriesgroup = $row['layout']; 
          } 
          print_group_items ($row);
        }
        print_footers;
      } 
    
    function print_group_items ($this_row) {
      echo "  <!-- Print group items -->"
    ; // will depend on $this_row
    }
    
    function print_headers () {
      echo "<!-- Print headers -->
    ";
    }
    
    function print_footers () {
      echo "<!-- Print footers -->
    ";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵