每四个结果后打破一个循环

时间:2017-06-15 08:33:38

标签: php mysql

我正在尝试创建一个新行或打破我的结果行以下拉到我的结果集中的新行,因为此时他们只是继续并打开容器并且不创建新的行在它到达容器末端的行。

我的代码非常简单 - 我使用bulma css框架,特别是is-3类来显示每个结果,因此它在容器中给了我4列。所以在每四个人之后应该创建一个新行。

我的代码非常简单:

<div class="container">
<div class="columns projectList">
    <?php while($stmt->fetch()) { ?>
    <div class="column is-3">
        <div class="card">
        <div class="card-content">
        <div class="content">
        <p class="projectStatus">
        <?php
        if($phase == 0){
            echo 'Phase: Pending';
        } 
        elseif($phase == 1){
            echo 'Phase: Planning';
        }
        elseif($phase == 2){
            echo 'Phase: Design';
        }  
        elseif($phase == 4){
            echo 'Phase: Development';
        }
        else{
            echo 'No Phase Added';
        }           
        ?>
        </p>
        <p class="projectTitle"><span>!</span><?php echo $projectName; ?></p>
            <div class="projectTimes">
                <div class="inner">
                    <span class="days">12</span>
                    <span class="for">Days Left In Design</span>
                </div>
                <div class="inner">
                    <span class="days">42</span>
                    <span class="for">Days to finish</span>
                </div>
            </div>
        </div>
        </div>
        <footer class="card-footer">
        <a class="card-footer-item"><i class="material-icons">&#xE8EF;</i>32</a>
        <a class="card-footer-item"><i class="material-icons">&#xE0B9;</i>10</a>
        </footer>
        </div>
    </div>
    <!-- END ROW HERE AND CREATE NEW ONE -->
    <?php } $stmt->close(); } ?>
</div>  
</div>

我的问题:我如何在每第四个结果后换行?

5 个答案:

答案 0 :(得分:2)

<div class="container">


    <?php 
    $i = 0;
     while($stmt->fetch()) { 
   if($i%4==0){
        echo '<div class="columns projectList">';
    }
    $i++;
  ?>
    <div class="column is-3">
        <div class="card">
        <div class="card-content">
        <div class="content">
        <p class="projectStatus">
        <?php
        if($phase == 0){
            echo 'Phase: Pending';
        } 
        elseif($phase == 1){
            echo 'Phase: Planning';
        }
        elseif($phase == 2){
            echo 'Phase: Design';
        }  
        elseif($phase == 4){
            echo 'Phase: Development';
        }
        else{
            echo 'No Phase Added';
        }           
        ?>
        </p>
        <p class="projectTitle"><span>!</span><?php echo $projectName; ?></p>
            <div class="projectTimes">
                <div class="inner">
                    <span class="days">12</span>
                    <span class="for">Days Left In Design</span>
                </div>
                <div class="inner">
                    <span class="days">42</span>
                    <span class="for">Days to finish</span>
                </div>
            </div>
        </div>
        </div>
        <footer class="card-footer">
        <a class="card-footer-item"><i class="material-icons">&#xE8EF;</i>32</a>
        <a class="card-footer-item"><i class="material-icons">&#xE0B9;</i>10</a>
        </footer>
        </div>
    </div>
    <?php
     if($i%4==0){
        echo '</div>'
        $i = 0;
    }
  ?>
    <!-- END ROW HERE AND CREATE NEW ONE -->
    <?php } $stmt->close(); } ?>

</div>  
</div>

答案 1 :(得分:1)

有一种叫做modulus

的东西
for ($c = 1; $c < count($results); $c++) {
    if ($c % 4 == 0) {
        // This is the fourth iteration
    }
}

答案 2 :(得分:0)

使用此逻辑,您可以在第四次迭代中破坏您的行

$i = 0; 
while(while($stmt->fetch())){

  if($i%4==0){
       //div to create new ROW
  }

  <div class="column is-3">
    //all your logic go here
  </div>

  if($i%4==0){
       // close the div for create new ROW
  }
  $i++;
}

答案 3 :(得分:0)

这是一个相当简单的问题。我从你的问题中得出你基本上想要将查询结果分成3组?每个组显示为一行?

我建议的是立即获取所有结果并存储为变量。假设此变量是一个数组,您可以使用 array_chunk()将行数组拆分为所需的块。这将生成一个数组数组,每个内部数组包含指定数量的项。请参阅:http://php.net/manual/en/function.array-chunk.php

我在这里为你嘲笑了一个简单的例子:

<?php 

//Fetch all data, this will become the $items array
$items = array(
    'item 1', 
    'item 2', 
    'item 3',
    'item 4',
    'item 5',
    'item 6',
    'item 7',
    'item 8',
    'item 9',
    'item 10',
    'item 11',
    'item 12',
    'item 13',
    'item 14',
);

//Split the items into 'rows' (chunks)
//The second parameter here basically indiciates how many items you want in each row
$rows = array_chunk($items, 3);

//Loop over each row group, remembering each row is an array of items (columns)
//Do your row building HTML within this foreach loop
//Do your columnd building HTML within the inner foreach loop 
foreach($rows as $columns){
    echo "---------------<br />";

    //Build column html for each item within the row
    foreach($columns as $column){
         echo $column.' | ';   
    }

    echo "<br />";
    echo "---------------";
    echo "<br /><br />";
}

?>

在此处运行abover:http://phpfiddle.org/

你可以看到它如何分割行等。

希望这有帮助!

答案 4 :(得分:-1)

这就是你要找的东西吗?

    <div class="container">

            <?php 
        $x = 1;
        while($stmt->fetch()) { 

        ?>
           <?php if($x == 1) { ?> <div class="columns projectList"> <?php }
  $x++;
 ?>
            <div class="column is-3">
                <div class="card">
                <div class="card-content">
                <div class="content">
                <p class="projectStatus">
                <?php
                if($phase == 0){
                    echo 'Phase: Pending';
                } 
                elseif($phase == 1){
                    echo 'Phase: Planning';
                }
                elseif($phase == 2){
                    echo 'Phase: Design';
                }  
                elseif($phase == 4){
                    echo 'Phase: Development';
                }
                else{
                    echo 'No Phase Added';
                }           
                ?>
                </p>
                <p class="projectTitle"><span>!</span><?php echo $projectName; ?></p>
                    <div class="projectTimes">
                        <div class="inner">
                            <span class="days">12</span>
                            <span class="for">Days Left In Design</span>
                        </div>
                        <div class="inner">
                            <span class="days">42</span>
                            <span class="for">Days to finish</span>
                        </div>
                    </div>
                </div>
                </div>
                <footer class="card-footer">
                <a class="card-footer-item"><i class="material-icons">&#xE8EF;</i>32</a>
                <a class="card-footer-item"><i class="material-icons">&#xE0B9;</i>10</a>
                </footer>
                </div>
            </div>
            <!-- END ROW HERE AND CREATE NEW ONE -->
            <?php
          if($x % 4 == 0)
        {
         echo '<div/>';
         $x = 1;
        }
         } $stmt->close(); } ?>
        </div>