Show variable in specific location with a div

Asked

Viewed 27 times

1

I have this while where I created a variable before the while to then get the total of a column:

$Total = 0;

    while($rows_cursos = mysqli_fetch_array($resultado_cursos)) {

        $teste = $rows_cursos['Horas Consumidas'];
        $Total = $Total + $teste;

$tabela3 .= '<tr>';

$tabela3 .= '<td>'.$rows_cursos['DataRegisto'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['codigoutente'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['nome'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['descricaovalencia'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['Data'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['Inicio'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['Fim'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['Colaborador'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['Horas Consumidas'].'</td>';

$tabela3 .= '</tr>'; 

}

$tabela3 .= '</tr>';

$tabela3 .='</tbody>'; 

$tabela3 .= '</table>';

$tabela3 .= '</div>';

echo $tabela3;

echo '<strong>Total de Horas Consumidas:</strong> '. $Total;

But this result that appears surrounded in red, wanted to appear under the last column of the table as pointed out by the arrow, since it is the sum of the hours of that column.

1 answer

1


See that after the while you are closing the </tr> unnecessarily:

$tabela3 .= '</tr>'; 

}

$tabela3 .= '</tr>'; <-- fechando novamente a TR

The lines are already closed inside the while.

Instead of this line $tabela3 .= '</tr>'; you can insert a new line with <td colspan="9">, where 9 represents the number of columns, creating a row with only one <td> covering the 9 table columns. Also put align="right" to align the text to the right. The code would look like this:

while($rows_cursos = mysqli_fetch_array($resultado_cursos)) {

        $teste = $rows_cursos['Horas Consumidas'];
        $Total = $Total + $teste;

$tabela3 .= '<tr>';

$tabela3 .= '<td>'.$rows_cursos['DataRegisto'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['codigoutente'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['nome'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['descricaovalencia'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['Data'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['Inicio'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['Fim'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['Colaborador'].'</td>';

$tabela3 .= '<td>'.$rows_cursos['Horas Consumidas'].'</td>';

$tabela3 .= '</tr>'; 

}

$tabela3 .= '<tr>';
$tabela3 .= '<td colspan="9" align="right">';
$tabela3 .= '<strong>Total de Horas Consumidas:</strong> '. $Total;
$tabela3 .= '</td>';
$tabela3 .= '</tr>';

$tabela3 .='</tbody>'; 

$tabela3 .= '</table>';

$tabela3 .= '</div>';

echo $tabela3;

Browser other questions tagged

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