我在php中有两段时间:
<?php
while($row1 = mysqli_fetch_array($result1))
{
?>
<tr>
<td><?php echo $row1["codigo"]; ?></td>
<td><?php echo $row1["nome"]; ?></td>
<td><?php echo $row1["QTD 1º e 2º Hora"]; ?></td>
<td><?php echo $row1["Valor 1º e 2º Hora"]; ?></td>
<td><?php echo $row1["QTD 3º Hora"]; ?></td>
<td><?php echo $row1["Valor 3º Hora"]; ?></td>
<td><?php echo $row1["QTD 4º ou mais Horas"]; ?></td>
<td><?php echo $row1["Valor 4º ou mais Horas"]; ?></td>
<td><?php echo $row1["Valor Total"]; ?></td>
</tr>
<?php
}
?>
此处返回以下三行:
然后我有第二个时间:
<?php
while($row3 = mysqli_fetch_array($result3))
{
?>
<tr>
<td><?php echo $row3["codigo"]; ?></td>
<td><?php echo $row3["nome"]; ?></td>
<td><?php echo $row3["QTD 1º e 2º Hora"]; ?></td>
<td><?php echo $row3["Valor 1º e 2º Hora"]; ?></td>
<td><?php echo $row3["QTD 3º Hora"]; ?></td>
<td><?php echo $row3["Valor 3º Hora"]; ?></td>
<td><?php echo $row3["QTD 4º ou mais Horas"]; ?></td>
<td><?php echo $row3["Valor 4º ou mais Horas"]; ?></td>
<td><?php echo $row3["Valor Total"]; ?></td>
</tr>
<?php
}
?>
此处返回此行:
同一时间中的两个while会返回4行,如下所示:
如果您注意到这4行,则您有两条具有相同用户编号的行。而你不能。我的意思是,根据相同的用户编号,第二行中的行绘制了第一行中的值和小时。
我该怎么做?基本上,这个想法是在注册错误的情况下创建一个贷方通知单系统。
显示代码:
$query2 = "SELECT E.DataDia,
E.codigo,
E.nome,
E.Colaborador,
E.`Local`,
E.Datasaida,
E.DataChegada,
E.`Horas Consumidas`,
TIME_FORMAT(SEC_TO_TIME(SUM(E.teste1)),'%i:%s') AS `QTD 1º e 2º Hora`,
CONVERT(CONCAT(SUM(E.Valor), '€'), char(8)) AS `Valor 1º e 2º Hora`,
TIME_FORMAT(SEC_TO_TIME(SUM(E.teste)),'%i:%s') AS `QTD 3º Hora`,
CONVERT(CONCAT(SUM(E.Valor1), '€'), char(8)) AS `Valor 3º Hora`,
TIME_FORMAT(SEC_TO_TIME(SUM(E.teste2)),'%i:%s') AS `QTD 4º ou mais Horas`,
CONVERT(CONCAT(SUM(E.Valor2), '€'), char(8)) AS `Valor 4º ou mais Horas`,
CONVERT(CONCAT(SUM(E.Total), '€'), char(8)) AS `Valor Total`
FROM
(SELECT C.Id,
C.DataDia,
C.codigo,
C.nome,
C.Colaborador,
C.`Local`,
C.Datasaida,
C.DataChegada,
C.`Horas Consumidas`,
C.Minutos,
C.teste1,
C.Valor,
C.teste,
C.Valor1,
C.teste2,
C.Valor2,
C.Valor+C.Valor1+C.Valor2 AS Total
FROM
(SELECT A.Id,
A.DataDia,
A.codigo,
A.nome,
A.Colaborador,
A.`Local`,
A.Datasaida,
A.DataChegada,
A.`Horas Consumidas`,
A.Minutos,
CAST(CASE WHEN A.teste1 IS NULL THEN 0 ELSE A.teste1 END AS DECIMAL (15,2)) AS teste1,
CAST(CASE WHEN D.teste IS NULL THEN 0 ELSE D.teste END AS DECIMAL (15,2)) AS teste,
CAST(CASE WHEN A.teste2 IS NULL THEN 0 ELSE A.teste2 END AS DECIMAL (15,2)) AS teste2,
CAST(CASE WHEN A.Valor IS NULL THEN 0 ELSE A.Valor END AS DECIMAL (15,2)) AS Valor,
CAST(CASE WHEN D.Valor1 IS NULL THEN 0 ELSE D.Valor1 END AS DECIMAL (15,2)) AS Valor1,
CAST(CASE WHEN A.Valor2 IS NULL THEN 0 ELSE A.Valor2 END AS DECIMAL (15,2)) AS Valor2
FROM
(SELECT B.Id,
B.DataDia,
B.codigo,
B.nome,
B.Colaborador,
B.`Local`,
B.Datasaida,
B.DataChegada,
B.`Horas Consumidas`,
B.Minutos,
(CASE WHEN B.Minutos <= 120 THEN B.Minutos ELSE 120 END) AS teste1,
(CASE WHEN B.Minutos <= 120 THEN B.Minutos*20/120 ELSE 20 END) AS Valor,
(CASE WHEN B.Minutos >= 180 THEN B.Minutos-180 ELSE 0 END) AS teste2,
(CASE WHEN B.Minutos >= 180 THEN (B.Minutos-180)*15/60 ELSE 0 END) AS Valor2
FROM
(SELECT A.Id,
A.DataDia,
A.codigo,
A.nome,
A.Colaborador,
A.`Local`,
A.Datasaida,
A.DataChegada,
A.`Horas Consumidas`,
CAST(TIME_TO_SEC(A.`Horas Consumidas`)/60 AS DECIMAL(15,0)) AS Minutos
FROM
(SELECT raddb.NotaCreditoAcompa.Id,
raddb.NotaCreditoAcompa.DataDia,
codigo,
raddb.Utente.nome,
raddb.usuarios.nome AS Colaborador,
`Local`,
DataChegada,
Datasaida,
SUBTIME(DATE_FORMAT(`DataChegada`,'%H:%i'), DATE_FORMAT(`Datasaida`,'%H:%i')) AS `Horas Consumidas`
FROM raddb.NotaCreditoAcompa LEFT OUTER JOIN raddb.Utente
ON raddb.Utente.codigo = raddb.NotaCreditoAcompa.NomeUtente LEFT OUTER JOIN raddb.usuarios
ON raddb.usuarios.id = raddb.NotaCreditoAcompa.Enfermeiro) AS A) AS B
) AS A
LEFT OUTER JOIN
(SELECT B.Id,
B.DataDia,
B.nome,
B.Colaborador,
B.`Local`,
B.Datasaida,
B.DataChegada,
B.`Horas Consumidas`,
B.Minutos,
(CASE WHEN B.Minutos <= 180 THEN B.Minutos-120 ELSE 60 END) AS teste,
(CASE WHEN B.Minutos <= 180 THEN (B.Minutos-120)*12/60 ELSE 12 END) AS Valor1
FROM
(SELECT A.Id,
A.DataDia,
A.nome,
A.Colaborador,
A.`Local`,
A.DataChegada,
A.Datasaida,
A.`Horas Consumidas`,
CAST(TIME_TO_SEC(A.`Horas Consumidas`)/60 AS DECIMAL(15,0)) AS Minutos
FROM
(SELECT raddb.NotaCreditoAcompa.Id,
DataDia,
raddb.Utente.nome,
raddb.usuarios.nome AS Colaborador,
`Local`,
Datasaida,
DataChegada,
SUBTIME(DATE_FORMAT(`DataChegada`,'%H:%i'), DATE_FORMAT(`Datasaida`,'%H:%i')) AS `Horas Consumidas`
FROM raddb.NotaCreditoAcompa LEFT OUTER JOIN raddb.Utente
ON raddb.Utente.codigo = raddb.NotaCreditoAcompa.NomeUtente LEFT OUTER JOIN raddb.usuarios
ON raddb.usuarios.id = raddb.NotaCreditoAcompa.Enfermeiro) AS A) AS B
WHERE B.Minutos > 120 ) AS D
ON A.Id = D.Id) AS C
WHERE date(C.`Datasaida`) BETWEEN '$inicio' AND '$fim') AS E
GROUP BY E.codigo, E.nome, E.DataDia, E.Colaborador, E.`Local`, E.Datasaida, E.DataChegada
ORDER BY E.DataDia ASC";
$query3 = "SELECT E.codigo,
E.nome,
TIME_FORMAT(SEC_TO_TIME(SUM(E.teste1)),'%i:%s') AS `QTD 1º e 2º Hora`,
CONVERT(CONCAT(SUM(E.Valor), '€'), char(8)) AS `Valor 1º e 2º Hora`,
TIME_FORMAT(SEC_TO_TIME(SUM(E.teste)),'%i:%s') AS `QTD 3º Hora`,
CONVERT(CONCAT(SUM(E.Valor1), '€'), char(8)) AS `Valor 3º Hora`,
TIME_FORMAT(SEC_TO_TIME(SUM(E.teste2)),'%i:%s') AS `QTD 4º ou mais Horas`,
CONVERT(CONCAT(SUM(E.Valor2), '€'), char(8)) AS `Valor 4º ou mais Horas`,
CONVERT(CONCAT(SUM(E.Total), '€'), char(8)) AS `Valor Total`
FROM
(SELECT C.Id,
C.codigo,
C.nome,
C.Datasaida,
C.DataChegada,
C.`Horas Consumidas`,
C.Minutos,
C.teste1,
C.Valor,
C.teste,
C.Valor1,
C.teste2,
C.Valor2,
C.Valor+C.Valor1+C.Valor2 AS Total
FROM
(SELECT A.Id,
A.codigo,
A.nome,
A.Datasaida,
A.DataChegada,
A.`Horas Consumidas`,
A.Minutos,
CAST(CASE WHEN A.teste1 IS NULL THEN 0 ELSE A.teste1 END AS DECIMAL (15,2)) AS teste1,
CAST(CASE WHEN D.teste IS NULL THEN 0 ELSE D.teste END AS DECIMAL (15,2)) AS teste,
CAST(CASE WHEN A.teste2 IS NULL THEN 0 ELSE A.teste2 END AS DECIMAL (15,2)) AS teste2,
CAST(CASE WHEN A.Valor IS NULL THEN 0 ELSE A.Valor END AS DECIMAL (15,2)) AS Valor,
CAST(CASE WHEN D.Valor1 IS NULL THEN 0 ELSE D.Valor1 END AS DECIMAL (15,2)) AS Valor1,
CAST(CASE WHEN A.Valor2 IS NULL THEN 0 ELSE A.Valor2 END AS DECIMAL (15,2)) AS Valor2
FROM
(SELECT B.Id,
B.codigo,
B.nome,
B.Datasaida,
B.DataChegada,
B.`Horas Consumidas`,
B.Minutos,
(CASE WHEN B.Minutos <= 120 THEN B.Minutos ELSE 120 END) AS teste1,
(CASE WHEN B.Minutos <= 120 THEN B.Minutos*20/120 ELSE 20 END) AS Valor,
(CASE WHEN B.Minutos >= 180 THEN B.Minutos-180 ELSE 0 END) AS teste2,
(CASE WHEN B.Minutos >= 180 THEN (B.Minutos-180)*15/60 ELSE 0 END) AS Valor2
FROM
(SELECT A.Id,
A.codigo,
A.nome,
A.Datasaida,
A.DataChegada,
A.`Horas Consumidas`,
CAST(TIME_TO_SEC(A.`Horas Consumidas`)/60 AS DECIMAL(15,0)) AS Minutos
FROM
(SELECT raddb.NotaCreditoAcompa.Id,
codigo,
nome,
DataChegada,
Datasaida,
TIMEDIFF(DATE_FORMAT(`DataChegada`,'%H:%i'), DATE_FORMAT(`Datasaida`,'%H:%i')) AS `Horas Consumidas`
FROM raddb.NotaCreditoAcompa LEFT OUTER JOIN raddb.Utente
ON raddb.Utente.codigo = raddb.NotaCreditoAcompa.NomeUtente) AS A) AS B
) AS A
LEFT OUTER JOIN
(SELECT B.Id,
B.nome,
B.Datasaida,
B.DataChegada,
B.`Horas Consumidas`,
B.Minutos,
(CASE WHEN B.Minutos <= 180 THEN B.Minutos-120 ELSE 60 END) AS teste,
(CASE WHEN B.Minutos <= 180 THEN (B.Minutos-120)*12/60 ELSE 12 END) AS Valor1
FROM
(SELECT A.Id,
A.nome,
A.DataChegada,
A.Datasaida,
A.`Horas Consumidas`,
CAST(TIME_TO_SEC(A.`Horas Consumidas`)/60 AS DECIMAL(15,0)) AS Minutos
FROM
(SELECT raddb.NotaCreditoAcompa.Id,
nome,
Datasaida,
DataChegada,
TIMEDIFF(DATE_FORMAT(`DataChegada`,'%H:%i'), DATE_FORMAT(`Datasaida`,'%H:%i')) AS `Horas Consumidas`
FROM raddb.NotaCreditoAcompa LEFT OUTER JOIN raddb.Utente
ON raddb.Utente.codigo = raddb.NotaCreditoAcompa.NomeUtente) AS A) AS B
WHERE B.Minutos > 120 ) AS D
ON A.Id = D.Id) AS C
WHERE date(C.`Datasaida`) BETWEEN '$inicio' AND '$fim') AS E
GROUP BY E.codigo, E.nome
ORDER BY E.nome";
我尝试过的事情
<?php
while($row1 = mysqli_fetch_array($result1))
{
while($row3 = mysqli_fetch_array($result3))
{
?>
<tr>
<td><?php echo $row1["codigo"]; ?></td>
<td><?php echo $row1["nome"]; ?></td>
<td><?php echo $row1["QTD 1º e 2º Hora"]; ?> - <?php echo $row3["QTD 1º e 2º Hora"]; ?></td>
<td><?php echo $row1["Valor 1º e 2º Hora"]; ?> - <?php echo $row3["Valor 1º e 2º Hora"]; ?></td>
<td><?php echo $row1["QTD 3º Hora"]; ?> - <?php echo $row3["QTD 3º Hora"]; ?></td>
<td><?php echo $row1["Valor 3º Hora"]; ?> - <?php echo $row3["Valor 3º Hora"]; ?></td>
<td><?php echo $row1["QTD 4º ou mais Horas"]; ?> - <?php echo $row3["QTD 4º ou mais Horas"]; ?></td>
<td><?php echo $row1["Valor 4º ou mais Horas"]; ?> - <?php echo $row3["Valor 4º ou mais Horas"]; ?></td>
<td><?php echo $row1["Valor Total"]; ?> - <?php echo $row3["Valor Total"]; ?></td>
</tr>
<?php
} }
?>
答案 0 :(得分:1)
您可以尝试这样的事情:
<?php
// You create an empty array
$result = [];
while($row1 = mysqli_fetch_array($result1))
{
// I assume $row1["codigo"] is your id. If not, replace by your id.
// The idea is to fill the array with all the data you will display after
$result[$row1["codigo"]] = array(
"nome" => $row1["nome"],
"qtd1_2hora" => $row1["QTD 1º e 2º Hora"],
...
"total" => $row1["Valor Total"]
);
}
// Now the second while
while($row3 = mysqli_fetch_array($result3))
{
// If you don't have a row with the same id : create it
if (empty($result[$row3["codigo"]])) {
$result[$row3["codigo"]] = array(
"nome" => $row3["nome"],
"qtd1_2hora" => $row3["QTD 1º e 2º Hora"],
...
"total" => $row3["Valor Total"]
);
}
// Now if you already have a row with this id : update your data
else {
// Eg: if you want to add the total of first while with total of second while
$result[$row3["codigo"]]["total"] += $row3["Valor Total"];
/* You have to do what you want here, can't help you more ! */
}
}
// Now you can display you data :
foreach ($result as $id => $data) {
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $data["nome"]; ?></td>
<td><?php echo $data["qtd1_2hora"]; ?></td>
...
<td><?php echo $data["total"]; ?></td>
</tr>
<?php
}
?>
是您要找的东西吗?