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>
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>';
}
?>