//INDEX.PHP
<?php
$array = [
"one",
"two",
"three",
"three",
"four",
"five",
"five"
];
foreach ($array as $value) {
echo "<p>$value</p>";
}
?>
//JAVASCRIPT
<script type="text/javascript">
var elements = document.getElementsByTagName("p"); //pego todos as tags "p" no meu caso
var alreadyStylized = []; //lista daqueles que já foram utilizados
Array.from(elements).forEach((elem) => {
//todos os que não se repetem ficam azul
elem.style.background = "dodgerblue";
//pego o texto/id do elemento atual
var id = elem.textContent;
//verifico se já foi estilizado
if (alreadyStylized.indexOf(id) != -1) {
//itero sobre todos novamente
Array.from(elements).forEach((elem2) => {
//porém verifico se o id confere
if (elem2.textContent == id) {
//adiciona yellow para todos que tem o mesmo
//id/texto
elem2.style.background = "yellow";
}
})
}
//no final coloca o id daqueles que ja foram estilizados
alreadyStylized = id;
});
</script>
I wonder if I could be using PHP for everything, but it is preferable to style the content of a page with javascript.
If you put all this inside a file called index.php and run it will appear this:
Support by browsers Array.from()
Support by browsers Array.forEach()
Source images: https://developer.mozilla.org/en-US/
It didn’t work out, buddy...
– Fabio Henrique
All the lines are in blue. my query returned me 5 lines and between the 5, two with equal ids, so only those with equal ids should stay with the same color , and yes it is integer
– Fabio Henrique
Managed to solve the problem ?
– Alex