Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP循环-can';t为每组重复结束标记_Php_Mysql_Loops - Fatal编程技术网

PHP循环-can';t为每组重复结束标记

PHP循环-can';t为每组重复结束标记,php,mysql,loops,Php,Mysql,Loops,恐怕我不知道我哪里出了问题 我试图循环遍历每个独特的组(每个独特的布局——假设数据集中有“short”、“medium”和“long”),并在items部分中打印该组中的每个项目 我有以下代码,但问题是页脚只在所有代码的末尾打印一次-我需要它们在每个部分的项目之后打印 <?php $seriesgroup = ''; if (mysqli_num_rows($result3) > 0) {while($row = mysqli_fetch_assoc($result3)) { if(

恐怕我不知道我哪里出了问题

我试图循环遍历每个独特的组(每个独特的布局——假设数据集中有“short”、“medium”和“long”),并在items部分中打印该组中的每个项目

我有以下代码,但问题是页脚只在所有代码的末尾打印一次-我需要它们在每个部分的项目之后打印

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

对于HTML和PHP如何交互,您的头脑中存在一种困惑。实现这一点的方法是从代码的PHP部分内部使用语句
echo

编辑:尝试此新版本。代码更清晰:

<?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 -->"\n; // will depend on $this_row
}

function print_headers ($this_row) {
  echo "<!-- Print headers -->\n"; // will depend on $this_row
}

function print_footers ($this_row) {
  echo "<!-- Print footers -->\n"; // will depend on $this_row
}
?>

如果组页脚不是第一个,则需要在循环顶部附近打印组页脚,如果有行,则在循环末尾打印组页脚

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

Print group footer if seriesgroup isn't blank here

<!-- Print group headers -->

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

    <!-- Print group items -->

<?php } ?>

<!-- Print footers -->

<?php } ?>

Print group footer again if series group isn't blank

如果seriesgroup在此不为空,则打印组页脚
如果序列组不为空,请再次打印组页脚

移动循环中的页脚输出我的问题不可能那么不清楚,因为Pierre已经理解并提供了下面的解决方案…但这给了我
我需要的是
无论如何,既然代码的结构是可读的,那么以前就不是这样了,您可以看到循环必须如何嵌套。根据您的说明,我编辑并更正了上面的代码。我认为页眉和页脚是常量。这很有帮助,但是页脚在页眉之前打印了三次之后,仍然只在底部出现一次。我需要打印三个页脚,每组项目后打印一个。我认为您的标记显示不正确。不…显示不正确
<?php $seriesgroup = ''; if (mysqli_num_rows($result3) > 0) {while($row = mysqli_fetch_assoc($result3)) {
if($row['layout'] != $seriesgroup) {  ?>

Print group footer if seriesgroup isn't blank here

<!-- Print group headers -->

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

    <!-- Print group items -->

<?php } ?>

<!-- Print footers -->

<?php } ?>

Print group footer again if series group isn't blank