0
Setting
Let’s assume I have 100 records on any comic book.
ID | PESSOA | CARRETO | ENDERECO | DISTANCIA
1 | A | C10 | XX | 20
2 | B | C20 | XY | 25
3 | D | C50 | XZ | 19
4 | D | C50 | XZ | 19
5 | F | C10 | XW | 27
...
...
From a query, I will generate an Array with all these records, and while doing the fetch, i need to check some conditions, comparing the previous record of the current record:
IDof reg. previous=IDreg. current -1PESSOAof reg. previous=PESSOAreg. currentCARRETOof reg. previous=CARRETOreg. currentENDERECOof reg. previous=ENDERECOreg. currentDISTANCIAof reg. previous=DISTANCIAreg. current
Example:
ID | PESSOA | CARRETO | ENDERECO | DISTANCIA
3 | D | C50 | XZ | 19
4 | D | C50 | XZ | 19
(If all conditions are true, I record the current record with the DISTANCIA = 0.)
As I did
In the loop I make the fetch, i used auxiliary variables to bring and compare the previous record check the conditions, and save the current in the array:
// variáveis auxiliares:
$id_ant = null; // seto como null para a 1a comparação
$pes_ant; $car_ant; $end_ant; $dis_ant;
while ($row = $result->fetch_assoc()) {
// verifico as condições
if ($id_ant == $row['ID']-1 && $pes_ant == $row['PESSOA'] && $car_ant == $row['CARRETO'] && $end_ant == $row['ENDERECO'] && $dis_ant == $row['DISTANCIA'])
$row['DISTANCIA'] == 0;
// gravo o registro no array
$array[] = $row;
// seto cada variável para depois utilizar na comparação posterior
$id_ant = $row['ID'];
$pes_ant = $row['PESSOA'];
$car_ant = $row['CARRETO'];
$end_ant = $row['ENDERECO'];
$dis_ant = $row['DISTANCIA'];
}
Example of what the record would look like 4 in the array:
(
[ID] => 4
[PESSOA] => D
[CARRETO] => C50
[ENDERECO] => XZ
[DISTANCIA] => 0
)
Doubts
- What if I had a table with 100 columns to compare? Would I have to do all this one by one or has an "automatic" to do this?
- It’s good that I’m doing this
fetchor it would be better to play everything for an Array and then work on it? (thinking about performance)
That wouldn’t be the thing of a
select distinct? What you’re trying to do is stick with the ones that aren’t duplicated in every field ?– Isac
@Isac, no, I need them all. The only difference is treating if the condition is true, the distance is 0, but the record has to go too!
– rbz
In the
ifyou are using=in place of==. And you need to compare all fields? Only theidand the name ofpessoawould not be enough?– Thiago Magalhães
@Thiagomagalhães opa cara, essa do
=was typing failure! I need to compare several, so I’m already thinking ahead if I have to compare 100 columns, understand!? rs– rbz
You could save everything in an array and then go through using foreach.. this way you could use a logic to go through the attributes and make the comparison.
– Thiago Magalhães
@Thiagomagalhães then dude, I think you’re saying it the way that Gabriel.Elegrina posted it as an answer!?
– rbz
It’s a way. And about passing it all on to
array, thinking of performance, won or loss in this case would be minimal.– Thiago Magalhães
@Then Thiagomagalhães. But I do direct comparison on
fetchis 1 step. Generate Array and then doforeach, would be 2 steps. If you catch a large volume, I believe it is considerable yes.– rbz
The step of generating a
arraycomputationally will have a time oflinear = f(n). Then you will consume your timeforeach+n, wherenis the amount of results. Increase1 nI consider it to be too little, if compared with(n^2)and(2^n), for example.– Thiago Magalhães
I don’t understand. If I do the
fetchto generate the array, I run for example 10,000 records. Then mineforeachwill have to go through the 10,000 records again. It’s not like this!?– rbz