dshnx48866 2019-03-21 20:00
浏览 80
已采纳

php - if / else if function是否基于具有某个类名的div的id名称?

Updated:

So I tried to change some things but I get stuck at it.

            <?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>

Has as output:

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

How come that the code uses only the elseif ($key == 'id05') { line for output after reading the identiaue div id name?

Original question:

So imagine these div's in my 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-->

With php, how would I be able to find the unique div id with only the class named yellow and make this into a variable to give it if/else if functions?

$divId = #divID.yellow

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


// a hundred more if statements

So that I get as html output:

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

1条回答 默认 最新

  • douhan8892 2019-03-21 20:34
    关注

    Instead of building your divs 1 by 1 you might as well store the information in an array and loop through it to print each one out as a div. This way you can store your id's in the array and apply your if/else statement as well.

    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>';
            }
            ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答