3
I have a problem with my code. I have the following code:
<?php
require('class/habeo.class.php');
$Habeo->DuplicateRegister('contatos', array('id'=>'1', 'id'=>'6'));
?>
This class duplicates the records I have in the database. It counts how many elements are in the array and loops to duplicate the id informed. The duplicate class is the following:
<?php
function($de, $condicao){
foreach($condicao as $campo=>$value){
$SQL = mysql_query('INSERT INTO {$de} ({$Columns})
SELECT {$Columns} FROM {$de}
WHERE {$campo}='{$value}');
}
}
?>
The problem is that it loops with only 1, and there are 2 records in the array.
I visualized that the error is because the array indices are the same name id and so are returning only 1, now when I change the name it returns 2.
This array has 2 items array('id'=>'1', 'id'=>'6'); and when I loop with foreach it returns me only the last item. If I change 1 element to another name, it returns 2 items in the loop.
– Alisson Acioli
It is normal this behavior, if vc specifies several times the same key in an array, only the last one.
– Bacco
how do I solve this problem, because I will duplicate several records and will inform "which" by id then I’ll have to put id=>XXX several times.
– Alisson Acioli
You will probably have to use another library or function, because anything based on associative array will have the same problem.
– Bacco
@Bacco would know some function to use instead of the array() ?
– Alisson Acioli
It’s not just changing the array, you have to rethink the whole code of the loop. I would probably use two arrays, this way:
novaFuncao( $tabela, Array( campo1, campo2 ...), Array( valor1, valor2...)
and afor ( count( ... ) ){ ... }
– Bacco