1
I have two selects that return me two arrays and I need to go through them, in the same table, but the records are being duplicated. How can I best resolve this issue ?
Follow the example:
<?php
foreach ($contratantes as $contratante) {
foreach ($contratados as $contratado) {
?>
<tr>
<td><?= $contratante['dataContratacao'] ?></td>
<td><?= $contratante['anuncioContratante'] ?></td>
<td><?= $contratante['tipoAnuncio'] ?></td>
<td><?= $contratado['anuncioContratado'] ?></td>
<td><?= $contratado['tipoAnuncioContratado'] ?></td>
<td><?= $contratado['login'] ?></td>
<td>Opções</td>
</tr>
<?php
}
}
?>
So, it works when I don’t have any repeated ID, when some id repeats in another record returns this error: Undefined offset: 2
– XLannes
I had not yet edited... It was now
– XLannes
From what you put in the question, I think the problem lies elsewhere in your code. You probably want a JOIN on DB, but you’re trying to make PHP a relation instead of right on select. But I’m not sure that’s it. Anyway, it doesn’t seem right to use arrays for this.
– Bacco
If this is the case, see if it helps: http://answall.com/questions/6441/70 - if this is not the case, put the piece of code that mounts the arrays in the question as well, or preferably make a [mcve] of the problem.
– Bacco
The Join is working as it should by the bank, usually... Only to build this table, I use two methods from two joins... until then beauty, I tried to use two foreach, gave error... I did what you suggested and seemed to have solved, only when adding data in the table, some of the records in this query gives Undefined offset, it may not be for the reason I mentioned the ids
– XLannes
Undefined offset can be an array shorter than the other. You would have to see the part that mounts the arrays to know. Without seeing the excerpt of the code that Join makes and picks up the arrays, I am tied hands to help. Normally in this case I would not need two arrays. Could take from DB and already play on screen
– Bacco
Dude, I figured out the cause of the offset problem, it was an array_search I was using before interacting the array, it was a solution I was testing before asking here... I took it out and it worked as it should, with your first answer. Thanks anyway
– XLannes
When you ask a next question, try to put more details, then we can help better. At least a little more of the code that works with the problem data. I don’t mean by the whole code, if it’s too much, but usually by putting the whole section of the part involved helps not only to respond, but also makes it easier to propose certain optimizations. We’re here to help, but first we need to understand what’s going on :)
– Bacco