Show total of records

Asked

Viewed 49 times

4

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

To show the total VIP video conferencing I am using the 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 database is shown correctly, but the problem is that in the Wilhe it is entering Infinite Loop. Does anyone know where they might be wrong? Thank you!

2 answers

4


Problem is you’re calling one while() within another and that in the second you define the condition as $vip2=="SIM", like the $vip2 which was set on that loop turn is actually "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 like this, and the echo has q be out of the while(), if you will not print every result, 1, 2, 3, etc.

Another issue is the $countVip be inside the while, every time he returns the loop $countVip becomes 0, so I believe that’s the way to get the total:

$contVip = 0;  
while($dadosVideo2 = mysqli_fetch_array($resultadoVideo2)) {   
    $vip2 = $dadosVideo2['vip'];     
    if($vip2 === 'SIM') {
        $contVip ++;
    }                   
}
echo $contVip;   
  • 1

    Brother, it worked out here! I had not listened to this question of the logic of one while inside the other. Thank you very much for the strength.

  • I who thank for being able to help, sometimes gnt gets lost amid the same HAUEHAUEAH codes

2

One way is to change your query to bring the data ready by day, making the check if 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
  • Brother, it worked out here too! Thanks for the feeling!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.