Php 在每四次结果之后打破while循环
我正在尝试创建一个新行或打断结果行,以便在结果集中下拉到一个新行,因为此时它们只是不断地跳出容器,在到达容器末尾时不会创建新行 我的代码相当简单-我使用bulma css框架,特别是Php 在每四次结果之后打破while循环,php,mysql,Php,Mysql,我正在尝试创建一个新行或打断结果行,以便在结果集中下拉到一个新行,因为此时它们只是不断地跳出容器,在到达容器末尾时不会创建新行 我的代码相当简单-我使用bulma css框架,特别是is-3类来显示每个结果,因此它在容器中给了我4列。因此,每四行之后应该创建一个新行 我的代码相当简单: <div class="container"> <div class="columns projectList"> <?php while($stmt->fetch()
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"></i>32</a>
<a class="card-footer-item"><i class="material-icons"></i>10</a>
</footer>
</div>
</div>
<!-- END ROW HERE AND CREATE NEW ONE -->
<?php } $stmt->close(); } ?>
</div>
</div>
12
设计剩余天数
42
还剩几天
;32
;10
我的问题:如何在每四次结果之后换行?这就是您要找的吗
<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"></i>32</a>
<a class="card-footer-item"><i class="material-icons"></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>
12
设计剩余天数
42
还剩几天
;32
;10
这就是你要找的吗
<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"></i>32</a>
<a class="card-footer-item"><i class="material-icons"></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>
12
设计剩余天数
42
还剩几天
;32
;10
有一种叫做模的东西
for ($c = 1; $c < count($results); $c++) {
if ($c % 4 == 0) {
// This is the fourth iteration
}
}
for($c=1;$c
有一种叫做模的东西
for ($c = 1; $c < count($results); $c++) {
if ($c % 4 == 0) {
// This is the fourth iteration
}
}
for($c=1;$c
12
设计剩余天数
42
还剩几天
;32
;10
12
设计剩余天数
42
还剩几天
;32
;10
使用此逻辑,您可以在第四次迭代时中断行
$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++;
}
$i=0;
while(while($stmt->fetch())){
如果($i%4==0){
//div以创建新行
}
//你所有的逻辑都在这里
如果($i%4==0){
//关闭用于创建新行的div
}
$i++;
}
使用此逻辑,您可以在第四次迭代时中断行
$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++;
}
$i=0;
while(while($stmt->fetch())){
如果($i%4==0){
//div以创建新行
}
//你所有的逻辑都在这里
如果($i%4==0){
//关闭用于创建新行的div
}
$i++;
}
这是一个相当简单的问题。我从你的问题中了解到,你基本上想要将查询结果分成3组?是否将每个组显示为一行
我建议您一次获取所有结果并将其存储为变量。假设此变量是一个数组,您可以使用数组\u chunk()将行数组拆分为所需的cunk。这将导致数组,其中每个内部数组包含指定的项数。见:
我在这里为你们模拟了一个简单的例子:
<?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 />";
}
?>
请在此处运行以上命令:
您可以看到它如何分割行等
希望这有帮助 这是一个相当简单的问题。我从你的问题中了解到,你基本上想要将查询结果分成3组?是否将每个组显示为一行
我建议您一次获取所有结果并将其存储为变量。假设此变量是一个数组,您可以使用数组\u chunk()将行数组拆分为所需的cunk。这将生成一个数组数组,其中每个内部数组都包含指定数量的项。见:
我在这里为你们模拟了一个简单的例子:
<?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 />";
}
?>
请在此处运行以上命令:
您可以看到它如何分割行等
希望这有帮助 所以每四行之后就应该创建一个新行。
?你说的是哪一排?你能用英语指出吗