[英]Breaking out of if statement and loop when condition is met
I have a table called ts_rounds in my database with the following structure 我的数据库中有一个名为ts_rounds的表,结构如下
round | current
===============
P | 0
1 | 1
2 | 0
3 | 0
4 | 0
I'm trying to echo a certain <div>
arrangement in two different conditions: 我试图在两种不同的条件下回应某种
<div>
排列:
one current value is 1 and the rest are 0 一个当前值为1,其余为0
all current are 0, more than one current value is 1 所有电流均为0,多个电流值为1
Once a condition is met then I want to break out of the if statement. 一旦条件满足,我想要打破if语句。
This is my code so far, the first condition is met but I'm having trouble meeting the second one. 这是我的代码到目前为止,第一个条件已经满足,但我遇到第二个问题。
<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>
Highly repeteitive code, especially since your html is basically only trivially changed between versions. 高度重复的代码,特别是因为你的html基本上只是在版本之间轻微改变。 why not something more like
为什么不更喜欢的东西
$sql = "SELECT current, round FROM yourtable"
$results = array()
while($row = fetch($result)) {
$results[$row['current']][] = $row['round'];
}
That'll give you an array 那会给你一个数组
0: p, 2, 3, 4
1: 1
From there the output logic becomes merely "which 0/1 value am I outputting now" to adjust the html as necessary. 从那里输出逻辑变为“我现在输出的0/1值”以根据需要调整html。 no need to repeat the html block 5 times for every variation.
每次变化都不需要重复5次html块。
I guess what you're looking for is something on these lines: 我想你正在寻找的是这些方面的东西:
$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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.