简体   繁体   中英

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

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>

Highly repeteitive code, especially since your html is basically only trivially changed between versions. 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. no need to repeat the html block 5 times for every variation.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM