php - if / else if функция, основанная на имени идентификатора div, который имеет определенное имя класса? - PullRequest
0 голосов
/ 21 марта 2019

Обновлено :

Поэтому я попытался изменить некоторые вещи, но я застрял в этом.

            <?php
        $array = [
            'id01' => [
                'class' => 'yellow',
            ],
            'id02' => [
                'class' => 'blue',
            ],
            'id03' => [
                'class' => 'yellow',
            ],
            'id04' => [
                'class' => 'yellow',
            ],
            'id05' => [
                'class' => 'yellow',
            ],
        ];

        foreach ($array as $key => $value) {
            if ($key == 'id01') {
                $vrnr = "1";
                $text = "Some text here";
            } elseif ($key == 'id02') {
                $vrnr = "2";
                $text = "lala";
            } elseif ($key == 'id03') {
                $vrnr = "2";
                $text = "bobobo";
            } elseif ($key == 'id04') {
                $vrnr = "2";
                $text = "testtest";
            } elseif ($key == 'id05') {
                $vrnr = "2";
                $text = "another one here";
            }
            echo '<div id="' . $key . '" class="' . $value['class'] . '">' . $text . '</div>';
        }
        ?>


<div id="id01" class="blue"><?=$text?><?=$vrnr?></div>

<div id="id01" class="yellow"><?=$text?><?=$vrnr?></div>
<div id="id02" class="yellow"><?=$text?><?=$vrnr?></div>
<div id="id03" class="yellow"><?=$text?><?=$vrnr?></div>
<div id="id04" class="yellow"><?=$text?><?=$vrnr?></div>
<div id="id05" class="yellow"><?=$text?><?=$vrnr?></div>

Имеет в качестве вывода:

Some text here
lala
bobobo
testtest
another one here
another one here2
another one here2
another one here2
another one here2
another one here2
another one here2

Как получилось, что код использует только строку elseif ($key == 'id05') { для вывода после чтения имени идентификатора div идентификатора?

Оригинальный вопрос:

Итак, представьте, что эти divв моем html:

<div id="id01" class="blue"><?=$text?></div>

<div id="id01" class="yellow"><?=$text?></div>
<div id="id02" class="yellow"><?=$text?></div>
<div id="id03" class="yellow"><?=$text?></div>
<div id="id04" class="yellow"><?=$text?></div>
<div id="id05" class="yellow"><?=$text?></div>

<-- a hundred more divs after this-->

С помощью php, как мне найти уникальный идентификатор div только с классом с именем yellow и преобразовать его в переменную, чтобы передать его в функции if / else if?

$ divId = # divID.yellow

if ($divId == id01) {
    $text = "Some text here";
} elseif ($divId== id02) {
    $text = "another phrase here";


// a hundred more if statements

Так что я получаю как вывод html:

<div id="id01" class="yellow">Some text here</div>
<div id="id02" class="yellow">another phrase here</div>

1 Ответ

0 голосов
/ 21 марта 2019

Вместо того, чтобы строить свои деления 1 на 1, вы можете также хранить информацию в массиве и проходить по ней, чтобы вывести каждый из них как деление. Таким образом, вы можете хранить свои идентификаторы в массиве и применять оператор if / else.

Ex ...

        <?php
        $array = [
            'id01' => [
                'class' => 'yellow',
            ],
            'id02' => [
                'class' => 'blue',
            ],
            'id03' => [
                'class' => 'yellow',
            ],
        ];

        foreach ($array as $key => $value) {
            if ($key == 'id01') {
                $text = "Some text here";
            } elseif ($key == 'id02') {
                $text = "another phrase here";
            }
            echo '<div id="' . $key . '" class="' . $value['class'] . '">' . $text . '</div>';
        }
        ?>
...