You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP循环中重复日期Recibos分组与悬停显示需求实现

解决PHP循环中Recibos数据同日期分组显示/隐藏问题

我明白你现在的困境——要在纯PHP里处理重复日期的Recibos数据,实现同日期只显示一个日期、鼠标悬停对应行时显示其他条目日期的效果,而且不能用MySQL分组。咱们一步步来重构你的代码,实现需求:

第一步:重构数据收集逻辑,按日期分组

首先,把你原来的多个独立数组($string_fecha、$recibo_monto等)换成一个按日期分组的二维数组,这样后续处理会更清晰。修改循环内的代码:

// 初始化分组数组
$recibosPorFecha = [];

// 你的循环逻辑(比如while循环读取MySQL数据)
while(/* 你的MySQL fetch逻辑 */) {
    $acumulado -= $monto;
    if(($fila >= $mostrar_de_aca_en_adelante && $detalles == 'minimo') || ($detalles == 'maximos')){
        // 将当前记录整理成数组
        $registro = [
            'id_cliente' => $id_cliente,
            'monto' => $monto,
            'saldo_total' => $saldo_total,
            'detalle_movimiento' => $datos_detalles['detalle_movimiento'],
            'recibo' => $recibo[0],
            'verificado' => $datos_detalles['verificado'],
            'acumulado' => $acumulado
        ];
        
        // 按日期分组:以$fecha_operacion为键,把记录追加到对应日期的数组里
        if(!isset($recibosPorFecha[$fecha_operacion])){
            $recibosPorFecha[$fecha_operacion] = [];
        }
        $recibosPorFecha[$fecha_operacion][] = $registro;
    }
}

第二步:生成带显示/隐藏逻辑的HTML表格

接下来,遍历分组后的数组,生成表格。对于每个日期组:

  • 第一条记录正常显示日期
  • 后续记录的日期单元格添加hidden-date类,默认隐藏
  • 用CSS实现鼠标悬停该行时,显示隐藏的日期单元格
// 先输出CSS样式(可以放到<head>里,这里为了方便直接写在表格前)
echo '<style>
    .hidden-date { display: none; }
    tr:hover .hidden-date { display: table-cell; }
    /* 可选:给hover的行加个背景色,提升体验 */
    tr:hover { background-color: #f5f5f5; }
</style>';

// 开始生成表格
echo '<table border="1" cellpadding="8" cellspacing="0">';
echo '<tr>
        <th>Fecha</th>
        <th>Cliente ID</th>
        <th>Monto</th>
        <th>Saldo Total</th>
        <th>Detalle</th>
        <th>Recibo</th>
        <th>Verificado</th>
        <th>Acumulado</th>
      </tr>';

// 遍历每个日期组
foreach($recibosPorFecha as $fecha => $registros) {
    $primerRegistro = true;
    // 遍历该日期下的所有记录
    foreach($registros as $reg) {
        echo '<tr>';
        // 处理日期单元格:第一条显示,其余隐藏
        if($primerRegistro) {
            echo "<td>{$fecha}</td>";
            $primerRegistro = false;
        } else {
            echo "<td class='hidden-date'>{$fecha}</td>";
        }
        // 输出其他字段
        echo "<td>{$reg['id_cliente']}</td>";
        echo "<td>{$reg['monto']}</td>";
        echo "<td>{$reg['saldo_total']}</td>";
        echo "<td>{$reg['detalle_movimiento']}</td>";
        echo "<td>{$reg['recibo']}</td>";
        echo "<td>{$reg['verificado']}</td>";
        echo "<td>{$reg['acumulado']}</td>";
        echo '</tr>';
    }
}

echo '</table>';

为什么这样改?

  1. 按日期分组的数组比多个独立数组更容易维护,能清晰关联每个日期对应的所有记录
  2. CSS控制显示/隐藏比纯PHP逻辑更高效,不需要额外的JS,浏览器原生支持hover事件
  3. 逻辑清晰:先把数据整理好,再按需渲染,避免了原来代码中对比$string_fecha[$i]和$string_fecha_tmp[$i+1]的混乱逻辑

如果你需要保留原来的累加金额逻辑(比如显示同日期的总金额),可以在遍历日期组的时候先计算总金额,然后在第一条记录的Monto单元格显示总金额,hover时显示每条的单独金额——只需要调整对应的单元格输出逻辑即可。

内容的提问来源于stack exchange,提问作者Juand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:57:35