doumenshi1475 2013-02-14 17:37
浏览 92
已采纳

当条件满足时,断开if语句和循环

I have a table called ts_rounds in my database with the following structure

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:

  1. one current value is 1 and the rest are 0

  2. all current are 0, more than one current value is 1

Once a condition is met then I want to break out of the if statement.

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>
  • 写回答

2条回答 默认 最新

  • douan7601 2013-02-14 18:05
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度