Ver Mensaje Individual
  #28 (permalink)  
Antiguo 29/01/2014, 17:18
Avatar de NSD
NSD
Colaborador
 
Fecha de Ingreso: mayo-2012
Ubicación: Somewhere
Mensajes: 1.332
Antigüedad: 12 años, 9 meses
Puntos: 320
Respuesta: Como guardar el dato anterior de un while + php ?

Código PHP:
Ver original
  1. <?php
  2. // Funcion que muestra los datos.
  3.     function showFila(&$response, $anterior, $counterTickets, $counterRisk, $counterWinLoss, $todayFigure)
  4.    {
  5.                 $response .= "<tr>
  6.                            <td>$anterior[Time]</td>
  7.                            <td>$anterior[NumGame]</td>
  8.                            <td>$anterior[Teams]</td>
  9.                            <td>$anterior[Score]</td>
  10.                            <td><a href=\"#\" onClick=\"openInfoWager(this);\">$counterTickets</a></td>
  11.                            <td>$counterRisk</td>
  12.                            <td>$counterWinLoss</td>
  13.                            <td>$todayFigure</td>
  14.                        </tr>";
  15.     }
  16. // Variable
  17.     $estado = (bool) ($dataGames = $strDataGames->fetch(PDO::FETCH_ASSOC));
  18. // El while
  19.     while($estado)
  20.     {
  21.         if ( isset($anterior) )
  22.         {
  23.             // Si hay un aterior trabajamos con el. Esto se hace ciempre.
  24.            $counterTickets      += $anterior['Tickets'];
  25.            $counterRisk         += $anterior['Risk'];
  26.            $counterWinLoss  += $anterior['WinLoss'];
  27.            $IdGames             .= $anterior['IdGame']."+";    
  28.        
  29.             // Verificamos la similitud con el anterior. Si coinciden hago lo que quiero.
  30.             if
  31.             (       ($dataGames['NumGame']  == $anterior['NumGame'])
  32.                 &&  ($dataGames['Teams']    == $anterior['Teams'])
  33.             )
  34.             {
  35.                if(strtotime($anterior['Time']) < strtotime($dataGames['Time']))
  36.                   $anterior['Time'] = $dataGames['Time'];
  37.      
  38.                $anterior['Score']   =  $dataGames['Score'];
  39.             }
  40.             else
  41.             {  
  42.                 $todayFigure += $counterWinLoss; // Resto cuando el usuario perdio.
  43.                // ahora se usa la funcion
  44.                 showFila($response, $anterior, $counterTickets, $counterRisk, $counterWinLoss, $todayFigure);
  45.                        
  46.                 // Borrar el anterior.
  47.                 unset($anterior);
  48.             }      
  49.         }
  50.        
  51.         if ( !isset($anterior) )
  52.         {
  53.             // Preparar las variables para la proxima ronda.
  54.             $counterTickets     = 0;
  55.             $counterRisk        = 0;
  56.             $counterWinLoss     = 0;
  57.             $IdGames            = "";
  58.             $anterior           = $dataGames;
  59.         }
  60.        
  61.         // Si es el ultimo.
  62.         if(!($estado = (bool) ($dataGames = $strDataGames->fetch(PDO::FETCH_ASSOC))))
  63.                 showFila($response, $anterior, $counterTickets, $counterRisk, $counterWinLoss, $todayFigure);
  64.     }
__________________
Maratón de desafíos PHP Junio - Agosto 2015 en FDW | Reglamento - Desafios