-1

the teacher told me that i cannot use id of a class inside a loop in php since its going to generate it in multiple lines with the same id while the id is unique. Is this true ? because I tried it and it worked thanks. exemple :

for ($j = 1; $j <= 9; ++$j)
{
    if ($j % 2 == 0)
    {
        echo '<td class="pair" id="DiagP">' . $j .'</td>';
    }

}       
Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
Mouttie Dje
  • 43
  • 1
  • 9
  • 1
    YES that is true – RiggsFolly Mar 15 '17 at 12:44
  • if you want to make your id's unique just append `$j` to your id value. `id="DiagP$j"` Please delete this no value question and read the manual on ids. – mickmackusa Mar 15 '17 at 12:44
  • if you try your code you will see many different td tags with the same id. your code is fine on a PHP side but not on a html one and you will have troubles to use the id in your js – Lelio Faieta Mar 15 '17 at 12:45
  • maybe its better to use a class instead of an id – Nomistake Mar 15 '17 at 12:46
  • 1
    [ID's Must Be Unique](http://stackoverflow.com/questions/5611963/can-multiple-different-html-elements-have-the-same-id-if-theyre-different-eleme), specifically because it will cause problems in [JavaScript](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id) and CSS when you try to interact with those elements. – Jay Blanchard Mar 15 '17 at 12:49
  • It will work, but when you try to access a specific element with javascript by `document.getElementById("DiagP")` it wont work, due to javascript can't make a difference between all those elements, since they've all the same ID. So it will select only the first one. So if you don't need this feature, it will work.. but its not the propper way to code. You should look for unique ID's anyways. – Twinfriends Mar 15 '17 at 12:49
  • Is this close enough to be used as the duplicate? There are several questions that are close like this one. http://stackoverflow.com/questions/23592497/why-are-duplicate-ids-an-error-in-html5-checkup – mickmackusa Mar 15 '17 at 12:57

5 Answers5

2

It works in the sense that PHP will generate the file without a problem.

It's wrong in the sense that, according to the HTML standard, ids must be unique with a page. Here, every <td> element you create has the same id, DiagP, inso your HTML will be invalid.

Reference: W3C HTML standard

id = name [CS]

This attribute assigns a name to an element. This name must be unique in a document.

Community
  • 1
  • 1
SolarBear
  • 4,534
  • 4
  • 37
  • 53
0

It will allow you to generate multiple tags with the same id, however you will only be able to access one of them: the first one.

If you don't care about accessing them (via javascript or such), then you have no problem (but in this case it would be easier to avoid using an id in the first place, as it's bad practice to have multiple tags with the same id). If you do need to access them based on their ids, you will run into problems.

You can test this yourself: open developer console (SHIFT + CTRL + I on chrome) and run document.getElementById("DiagP").value and you will see that you only get the first value of j you have. Hope this clears up the confusion!

Emad Y
  • 415
  • 4
  • 14
0
  1. you need to use unique id for same page. but you can use same class name multiple
  2. if you use same id in multiple then you can not get value , select etc using jquery , javascript etc. but you can use class for when you need same all data need to get,select etc

you can use this code. use your php variable for create dynamic and unique id so id will not same .

<table>
<tr>
    <?php
    for ($j = 1; $j <= 9; ++$j) {
        if ($j % 2 == 0) {
            echo '<td class="pair" id="DiagP_' . $j . '">' . $j . '</td>';
        }

    } ?>
  </tr>
  </table>

you can also add dynamic class and also fixed class

<table>
<tr>
    <?php
    for ($j = 1; $j <= 9; ++$j) {
        if ($j % 2 == 0) {
            echo '<td class="pair test_'.$j.'" id="DiagP_' . $j . '">' . $j . '</td>';
        }

    } ?>
</tr>
</table>

check this also Difference between id and class in CSS and when to use it

Community
  • 1
  • 1
Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43
  • A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Mar 15 '17 at 12:51
0

you cannot use id inside loop. beacuse it generate multiple id. and id is unique. while your code generate multiple td with same id. it is wrong.

Code will work. there is no any error. but logic is wrong. it will create problem while you use javascript or jquery on that id. if there is no use of id. then it is ok. but if you want to use id anywhere then the code is as below.

 for ($j = 1; $j <= 9; ++$j)
        {
                   if ($j % 2 == 0)
                    {

                     echo '<td class="pair" id="DiagP'.$j.'">' . $j .'</td>';
                    }

       }  

So it will create different id for all td.

0

As far as i know, an ID and a class are not the same.

You can not say "ID of a class" in this meaning.

Either it is an ID id="someId" or its a CLASS class="someClass"

If the td has a unique format or something, try making the id unique:

https://developer.mozilla.org/nl/docs/Web/CSS/ID_selectors

for ($j = 1; $j <= 9; ++$j)
{
    if ($j % 2 == 0)
    {
        echo '<td class="pair" id="DiagP_' . $j . '">' . $j .'</td>';
    }

}

If the td doesnt have to be unique, use a class:

https://developer.mozilla.org/nl/docs/Web/CSS/Class_selectors

for ($j = 1; $j <= 9; ++$j)
{
    if ($j % 2 == 0)
    {
        echo '<td class="pair" class="DiagP">' . $j .'</td>';
    }

} 

Further reading about id versus class

https://css-tricks.com/the-difference-between-id-and-class/

Nomistake
  • 893
  • 2
  • 17
  • 32