To cycle through the loop horizontally, sort the data according to the format you need.
Considering that this is your Database answer:
$row = mysql_fetch_row($result);
Ideally, the database will filter it by Accion='Compra'
, in any case this code will select only% of Accion='Compra'
:
//los datos selecccionado se almacenan en "$rowLimpia"
$rowLimpia = array();
for($i=0;$i<count($row);$i++){
if($row[$i]["Accion"]=="Compra"){
array_push($rowLimpia,$row[$i]);
}
}
Now to order Horizontally:
//Los datos ordenados estaran en "$data"
$data = array();
$datahijo = array();
for($i=0;$i<count($rowLimpia);$i++){
//comparando "Id" de la fila actual con la anterior, si es diferente indica que se esta cambiando a los registro del siguente Cliente.
if($rowLimpia[$i]["Id"]==@$rowLimpia[($i-1)]["Id"]){
$datahijo[$contadorCompra."_Compra"] = $rowLimpia[$i]['Fecha'];
}else{//Cuando es el siguiente Cliente...
//... se agrega el Cliente anterior al array $data
if(count($datahijo)>0){array_push($data,$datahijo);}
//... se reinician variables
$contadorCompra=1;
$datahijo = array();
$datahijo["Cliente"] = $rowLimpia[$i]['Cliente'];
$datahijo[$contadorCompra."_Compra"] = $rowLimpia[$i]['Fecha'];
}
$contadorCompra++;
}
//se agrega el ultimo Cliente encontrado al array $data.
if(count($datahijo)>0){array_push($data,$datahijo);}
//probando
var_dump($data);
Then there are records with Data: Client, 1st Purchase, 2nd Purchase, ...
[![introducir la descripción de la imagen aquí][1]][1]
Ordered in this way you only have to send them to the excel directly:
$filaInicial=1;
$columnas = ["","A","B","C","D","E","F","G","H"];
$columnaInicial=2;//equivalente a "B" segun $columnas;
for($i=0;$i<count($data);$i++){
//pintando Cliente
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$filaInicial, $data[$i]["Cliente"]);
for($j=1;$j<count($data[$i]);$j++){
//pintando Compras
$objPHPExcel->getActiveSheet()->SetCellValue($columnaInicial.$filaInicial, $data[$i][$j."_Cliente"]);
}
$filaInicial++;
}