0

我的数据库中有一个名为 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>
4

2 回答 2

1

高度重复的代码,特别是因为您的 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 次。

于 2013-02-14T17:45:33.933 回答
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;
于 2013-02-14T18:05:42.553 回答