【问题标题】:Breaking out of if statement and loop when condition is met满足条件时跳出 if 语句和循环
【发布时间】:2013-02-14 17:37:32
【问题描述】:

我的数据库中有一个名为 ts_rounds 的表,其结构如下

round | current
===============
P     | 0
1     | 1
2     | 0
3     | 0
4     | 0

我试图在两种不同的条件下回应某个<div> 安排:

  1. 一个当前值为1,其余为0

  2. 所有电流为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>

【问题讨论】:

  • 使用while 而不是if
  • if elseif 和 else 不需要任何中断!!!它们用于您的目的! break 和 continue 只能在循环内部使用。
  • PHP 提供breakcontinue
  • 大家好,感谢您的回复。如何确保以最佳方式满足这两个条件?
  • 您将从查询中提取多少条记录?满足条件是模棱两可的。

标签: php mysql pdo


【解决方案1】:

我猜你正在寻找的是以下几行:

$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;

【讨论】:

  • 我要把这个放在$sql = "SELECT * from ts_rounds"; $result = $pdo-&gt;query($sql);之后吗?
  • 它看起来像这样i.imgur.com/Q41kBnM.png,但它只意味着显示一排 P 1 2 3 4 5,如果 current=1 则只有一个圆圈亮起。如果有多个圆圈 current=1 或没有,则所有圆圈都应为灰色
  • 你确定SELECT * from ts_rounds 只给你6条记录吗?上面的代码你试过了吗?
  • 到目前为止,这是我的代码:pastebin.com/5mvU8KjL,是的。我试过上面的代码:-)
  • 如果可以的话,将生成的 html 页面(源代码)与 CSS 一起分享。
【解决方案2】:

高度重复的代码,特别是因为您的 html 基本上只是在版本之间进行了微不足道的更改。为什么不更像

$sql = "SELECT current, round FROM yourtable"
$results = array()
while($row = fetch($result)) {
   $results[$row['current']][] = $row['round'];
}

这会给你一个数组

0: p, 2, 3, 4
1: 1

从那里输出逻辑只是“我现在输出哪个 0/1 值”以根据需要调整 html。无需为每个变体重复 html 块 5 次。

【讨论】:

  • 感谢您的回复!我如何将数组编写为满足这两个条件的逻辑?我以前没有做过这样的事情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
相关资源
最近更新 更多