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:
ID
of reg. previous=
ID
reg. current -1PESSOA
of reg. previous=
PESSOA
reg. currentCARRETO
of reg. previous=
CARRETO
reg. currentENDERECO
of reg. previous=
ENDERECO
reg. currentDISTANCIA
of reg. previous=
DISTANCIA
reg. 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
fetch
or 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
if
you are using=
in place of==
. And you need to compare all fields? Only theid
and the name ofpessoa
would 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
fetch
is 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
array
computationally will have a time oflinear = f(n)
. Then you will consume your timeforeach
+n
, wheren
is the amount of results. Increase1 n
I 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
fetch
to generate the array, I run for example 10,000 records. Then mineforeach
will have to go through the 10,000 records again. It’s not like this!?– rbz