Sum variable within FOR

Asked

Viewed 32 times

0

Good people, I need to generate a report where I search for people who have been present a day, the person may have more than one presence on the same day, but I need to pull only once, so I used "GROUP BY" to group by user id,and then use mysqli_num_rows to count how many records I have in this search, the problem is that I have to present the total amount of record but I’m not getting it. My code is like this:

for($n=1;$n<= 26;$n++){
        $quant_enc = "SELECT * from presenca WHERE data_presenca = '$ano-$mes-$n' GROUP BY id_cad";
        $ex_enc = mysqli_query($conn,$quant_enc);
        $number_people = mysqli_num_rows($ex_enc);

        $html .= '<table class="table table-striped table-advance table-hover">';
        $html .= '<thead>';
        $html .= '<tr>';
        $html .= '<td>Nome</th>';
        $html .= '<td>Data Presença</th>';
        $html .= '<td>Horario</th>';
        $html .= '</tr>';
        $html .= '</thead>';


        while($rowsss = $ex_enc->fetch_array()){
            $html .= '<tr>';
            $html .= '<td>'.$rowsss['nomecad'].'</td>';
            $html .= '<td>'.date("d/m/Y", strtotime($rowsss['data_presenca'])) .'</td>';
            $html .= '<td>'.$rowsss['hora'].'</td>';
            $html .= '</tr>';


        }


      }     

How do I get the result I have in $number_people and add it up for every searched day ? I wanted something like this : Day 01 = 300 people, day 02 = 200 [.. ] = total number of people = 500 , or there are easier ways to achieve the result that I hope ?

1 answer

3


Just create a variable before and add to each iteration:

$total = 0;

for($n=1;$n<= 26;$n++){
    $quant_enc = "SELECT * from presenca WHERE data_presenca = '$ano-$mes-$n' GROUP BY id_cad";
    $ex_enc = mysqli_query($conn,$quant_enc);
    $number_people = mysqli_num_rows($ex_enc);

    $total += $number_people;

    // [...]
}

$html .= '<p>Total: $total</p>';

Browser other questions tagged

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