0

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>
Mike
  • 23
  • 4

1 Answers1

0

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>';
        }
        ?>
Weston Walker
  • 91
  • 1
  • 3
  • I tried your solution but the only thing I get is an error about the echo line: syntax error, unexpected '$value' (T_VARIABLE), expecting ',' or ';' – Mike Mar 21 '19 at 21:03
  • There was a syntax error. Sorry, it has been updated. Try again. – Weston Walker Mar 21 '19 at 21:32
  • This is actually really neat! Thanks! If for example, the class would be all the same for css reason, knowing we can define this class in the html, What should we put in the array in array you have in your code? Like the `'class' => 'yellow',` , then we would have `$array = [ 'id01' => [],` with empty brackets at the end. I just tested this and it still works, but is it okay to leave a lot of these brackets empty? – Mike Mar 21 '19 at 21:53
  • Yes, if you don't need the class in the array you can leave the inner array empty or you could turn it into a standard 1 dimensional array. – Weston Walker Mar 22 '19 at 13:35
  • Okay, well I have changed a bit of things (added a second value but the code gets stuck on using the same else if values. I had to change the original question in order to parse the code, please see above. – Mike Mar 27 '19 at 11:39