我的数据库中有一个名为ts_rounds的表,结构如下
round | current
===============
P | 0
1 | 1
2 | 0
3 | 0
4 | 0
我试图在两种不同的情况下回显特定的
<div>
安排:一个当前值为1,其余为0
所有电流均为0,一个以上电流值为1
一旦满足条件,那么我想突破if语句。
到目前为止,这是我的代码,满足第一个条件,但满足第二个条件时遇到问题。
<div id="timeline">
<?php
$sql = "SELECT * from ts_rounds";
$result = $pdo->query($sql);
foreach ($result as $row)
{
if ($row["round"] == "P" && $row["current"] == "1") {
echo
'
<div id="r0" class="circle">
<h3>P</h3>
</div>
<div id="r1" class="circle_small"><h3>1</h3></div>
<div id="r2" class="circle_small"><h3>2</h3></div>
<div id="r3" class="circle_small"><h3>3</h3></div>
<div id="r4" class="circle_small"><h3>4</h3></div>
<div id="r5" class="circle_small"><h3>A</h3></div>
<hr id="line">
';
}
else if ($row["round"] == "1" && $row["current"] == "1") {
echo
'
<div id="r0" class="circle_small">
<h3>P</h3>
</div>
<div id="r1" class="circle"><h3>1</h3></div>
<div id="r2" class="circle_small"><h3>2</h3></div>
<div id="r3" class="circle_small"><h3>3</h3></div>
<div id="r4" class="circle_small"><h3>4</h3></div>
<div id="r5" class="circle_small"><h3>A</h3></div>
<hr id="line">
';
}
else if ($row["round"] == "2" && $row["current"] == "1") {
echo
'
<div id="r0" class="circle_small">
<h3>P</h3>
</div>
<div id="r1" class="circle_small"><h3>1</h3></div>
<div id="r2" class="circle"><h3>2</h3></div>
<div id="r3" class="circle_small"><h3>3</h3></div>
<div id="r4" class="circle_small"><h3>4</h3></div>
<div id="r5" class="circle_small"><h3>A</h3></div>
<hr id="line">
';
}
else if ($row["round"] == "3" && $row["current"] == "1") {
echo
'
<div id="r0" class="circle_small">
<h3>P</h3>
</div>
<div id="r1" class="circle_small"><h3>1</h3></div>
<div id="r2" class="circle_small"><h3>2</h3></div>
<div id="r3" class="circle"><h3>3</h3></div>
<div id="r4" class="circle_small"><h3>4</h3></div>
<div id="r5" class="circle_small"><h3>A</h3></div>
<hr id="line">
';
}
else if ($row["round"] == "4" && $row["current"] == "1") {
echo
'
<div id="r0" class="circle_small">
<h3>P</h3>
</div>
<div id="r1" class="circle_small"><h3>1</h3></div>
<div id="r2" class="circle_small"><h3>2</h3></div>
<div id="r3" class="circle_small"><h3>3</h3></div>
<div id="r4" class="circle"><h3>4</h3></div>
<div id="r5" class="circle_small"><h3>A</h3></div>
<hr id="line">
</div>';
}
else if ($row["round"] == "A" && $row["current"] == "1") {
echo
'
<div id="r0" class="circle_small">
<h3>P</h3>
</div>
<div id="r1" class="circle_small"><h3>1</h3></div>
<div id="r2" class="circle_small"><h3>2</h3></div>
<div id="r3" class="circle_small"><h3>3</h3></div>
<div id="r4" class="circle_small"><h3>4</h3></div>
<div id="r5" class="circle"><h3>A</h3></div>
<hr id="line">
';
}
else {
$rounds = '
<div id="r0" class="circle_small">
<h3>P</h3>
</div>
<div id="r1" class="circle_small"><h3>1</h3></div>
<div id="r2" class="circle_small"><h3>2</h3></div>
<div id="r3" class="circle_small"><h3>3</h3></div>
<div id="r4" class="circle_small"><h3>4</h3></div>
<div id="r5" class="circle_small"><h3>A</h3></div>
<hr id="line">
';
}
}
?>
</div>
最佳答案
我猜您正在寻找的是以下几行:
$circleSize = array ('circle_small', 'circle');
$rounds = '';
foreach ($result as $row) {
switch ($row['round']) {
case 'P':
$rounds .= '<div id="r0" class="'.$circleSize[$row['current']].'"><h3>P</h3></div>';break;
case '1':
$rounds .= '<div id="r1" class="'.$circleSize[$row['current']].'"><h3>1</h3></div>';break;
case '2':
$rounds .= '<div id="r2" class="'.$circleSize[$row['current']].'"><h3>2</h3></div>';break;
case '3':
$rounds .= '<div id="r3" class="'.$circleSize[$row['current']].'"><h3>3</h3></div>';break;
case '4':
$rounds .= '<div id="r4" class="'.$circleSize[$row['current']].'"><h3>4</h3></div>';break;
case 'A':
$rounds .= '<div id="r5" class="'.$circleSize[$row['current']].'"><h3>A</h3></div>';break;
}
}
echo $rounds;
关于php - 满足条件时中断if语句并循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14880757/