Show Total Records

5

Personal Talk, I am developing a system for videoconferences and this should include a Dashboard with some information, such as the total of videoconferences of the current day, the next day and the amount of VIP videoconferences.

To show the total VIP videoconferences I'm using following code:

    $dataDoDia = date('Y/m/d');
    $sqlVideo2 = "SELECT * FROM videoconferencias WHERE dia = '$dataDoDia' ORDER BY dia, horaInicio"; 
    $resultadoVideo2 = mysqli_query($connect, $sqlVideo2);

   if (mysqli_num_rows($resultadoVideo2) > 0) {

   while($dadosVideo2 = mysqli_fetch_array($resultadoVideo2)) {   

        $vip2 = $dadosVideo2['vip'];

        $contVip = 0;      
        while ($vip2 === 'SIM') {
            $contVip ++;
        }   

        echo $contVip;                      
    }
}

The data coming from the bank are shown correctly, however the problem is that in the wilhe it is entering infinite Loop. Does anyone know where it might be wrong? Thanks!

    
asked by anonymous 22.07.2018 / 16:58

2 answers

5

Problem is that you are calling a while() within another and that in the second you define the condition as $vip2=="SIM" , as the $vip2 that was set on this loop loop is in fact "YES" it will create an infinite loop, because the condition will never change, and will not be able to terminate to continue the first while() :

Try to do this, and echo has to be out of while() , if it will not print to each result, 1, 2, 3, etc.

Another question is the $countVip being within the while, every time it returns the loop $countVip becomes 0 , so I believe this is the way to get the total:

$contVip = 0;  
while($dadosVideo2 = mysqli_fetch_array($resultadoVideo2)) {   
    $vip2 = $dadosVideo2['vip'];     
    if($vip2 === 'SIM') {
        $contVip ++;
    }                   
}
echo $contVip;   
    
22.07.2018 / 17:07
2

One way is to change your query to bring the data ready by day, checking whether it is VIP or not:

SELECT dia,
       COUNT(1) AS total,
       COUNT(CASE vip WHEN 'SIM' THEN 1 ELSE NULL END) AS vip
  FROM videoconferencias vc
 WHERE dia BETWEEN GETDATE() AND DATEADD(DAY, 1, GETDATE())
 GROUP BY dia, horaInicio
    
23.07.2018 / 17:51