How about people! I have a PrintServer with the software PaperCut PrintLogger which generates some CSV (and HTML) files with the printing log. I'm doing a script that reads the CSV in the data entered into a database, for further analysis.
In the database I have 3 tables: Printer
, User
and Activity
. This last table is related to the other 2 by their id.
In another method charge in memory 2 arrays attributes, $this->printers
and $this->users
with the information of printers (of the table Printers
) and users (table Users
) that are currently in the database (this works perfect ).
Now, for each record of the CSV read, I have a method that verifies that the name of the printer appears in the table " Printer
". If it does not appear, then insert the printer in that table and update $this->printers
with that data so that it is not reinserted in subsequent comparisons.
private function check_or_insert_printer_db($value) {
// Inserta datos nuevos en las tablas de Printer o User
$key_item = array_search($value, $this->printers);
if($key_item === False) {
// Si no se encuentra la impresora, insertarla en la tabla
$query = "INSERT INTO Printer (prname) VALUES ('$value')";
$this->dbconn->query($query);
$id_item = $this->dbconn->lastInsertRowid();
// Actualiza el array para no volver a insertar un elemento nuevo
$this->printers[$id_item] = $value;
} else {
// Si existe la impresora en la tabla, asignar el valor del id.
$id_item = $key_item;
}
return $id_item; // retorna el ID relacionado
}
This works perfect.
Here comes my problem, which is more a doubt. I wanted the described method to also work for " Users
" since it is exactly the same logic as with " Printers
". So I modified the previous method and now I have a local variable, $arr_elements
, to which I assign the reference of $this->printers
or $this->users
, as the case may be.
private function check_or_insert_item_db($value, $table, $column) {
// Inserta datos nuevos en las tablas de Printer o User
$arr_elements = ($table == 'Printer') ? &$this->printers : &$this->users;
$key_item = array_search($value, $arr_elements);
if($key_item === False) {
// Si no se encuentra el item, insertarlo en la tabla
$query = "INSERT INTO $table ($column) VALUES ('$value')";
$this->dbconn->query($query);
$id_item = $this->dbconn->lastInsertRowid();
// Actualiza el array para no volver a insertar un elemento nuevo
$arr_elements[$id_item] = $value;
} else {
$id_item = $key_item;
}
return $id_item; // retorna el ID relacionado
}
But the following error appears.
PHP Parse error: syntax error, unexpected '&'
Can not attributes be assigned by reference to local variables of a method? Is there a better way to solve this issue without having to have two separate methods for each case?