sorry if I don't ask correctly.
This is part of my table:
In daoExcel.php
I have this function:
public function selectInconsistencias(){
$aObjects=array();
$dbconn = new DBconnection();
$db = $dbconn->bdConnection();
$stmt = $db->prepare("SELECT rco_rut,rco_nombre,rco_marc, rco_estado FROM act_relcontrol");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetchAll();
foreach ($result as $row) {
$fecha = date("d/m/Y", strtotime(str_replace("/","-",$row['rco_marc']), mktime(0, 0, 0)));
$aTransfer = new DaoTransferExcel();
$aTransfer->setRut($row['rco_rut']);
$aTransfer->setNombre($row['rco_nombre']);
$aTransfer->setFecha($fecha);
$aTransfer->setEstado($row['rco_estado']);
$aObjects[]=$aTransfer;
}
return $aObjects;
}
Then I return the results to controllerExcel.pho
and I use the data in this function:
private function setDatosInconsistencias($datos){
foreach($datos as $sKey=>$oValue){
$list[] = array('rut' => $oValue->getRut(),
'nombre' => $oValue->getNombre(),
'fecha' => $oValue->getFecha(),
'estado' => $oValue->getEstado(),
'count_ent' => '0',
'count_sal' => '0'
);
}
print_r($list);
exit();
}
print_r($list);
Prints all the data from data base of this way:
Well, I need this:
Example 1:
I have this:
But I need only this:
Example 2:
I have this:
And I need only this:
Example 3:
I have this:
And I need only this:
So, I need count the 'M/Ent' and 'M/Sal' by day and I don't need [estado]
.
Some advice of how do this?
Sorry by my english.
You can store your results in an array where you can use "fecha" as an array key.
In your foreach loop you remember the value of "estado" so you know which field to increment and then unset it from the array because you don't need it.
Then you can reference the $result
array by "fecha" and increment the "count_sal" or "count_ent"
For example:
$result = array();
$list = array(
22 => array(
"rut" => "16.534.770-6",
"nombre" => "Miguel Pichipillán S",
"fecha" => "02/09/2015",
"estado" => "M/Ent",
"count_ent" => 0,
"count_sal" => 0
),
23 => array(
"rut" => "16.534.770-6",
"nombre" => "Miguel Pichipillán S",
"fecha" => "02/09/2015",
"estado" => "M/Ent",
"count_ent" => 0,
"count_sal" => 0
),
24 => array(
"rut" => "16.534.770-6",
"nombre" => "Miguel Pichipillán S",
"fecha" => "02/09/2015",
"estado" => "M/Ent",
"count_ent" => 0,
"count_sal" => 0
),
25 => array(
"rut" => "16.534.770-6",
"nombre" => "Miguel Pichipillán S",
"fecha" => "02/09/2015",
"estado" => "M/Sal",
"count_ent" => 0,
"count_sal" => 0
),
);
foreach ($list as $key => $value) {
$estado = $value["estado"];
$fecha = $value["fecha"];
unset($value["estado"]);
if (!isset($result[$fecha])) {
$result[$fecha] = $value;
}
if ($estado === "M/Sal") {
$result[$fecha]["count_sal"]++;
}
if ($estado === "M/Ent") {
$result[$fecha]["count_ent"]++;
}
}
var_dump($result);
Will result in:
array (size=1)
'02/09/2015' =>
array (size=5)
'rut' => string '16.534.770-6' (length=12)
'nombre' => string 'Miguel Pichipillán S' (length=21)
'fecha' => string '02/09/2015' (length=10)
'count_ent' => int 3
'count_sal' => int 1