Add all values that are in the database and display on the screen

0

Could you give me a help, I have a table where I need to show the total values, in case you add all the values that are in the database and show in the table. My table calls ads and the value column calls (value).

<tr>
    <th style="text-align:center;">Valor Total:<?php
    $sql = $pdo->prepare("SELECT * FROM anuncios WHERE valor = :valor");
    ?></th>
</tr>
    
asked by anonymous 20.10.2017 / 19:19

4 answers

1

I do not know if you know how to return the values, but based on my comment up, follow the base code.

$sql = $pdo->prepare("SELECT SUM(valor) FROM anuncios");
$sql->execute();

$ln = $sql->fetchObject();

echo $ln->valor;
    
20.10.2017 / 19:36
0

The select is this:

SELECT SUM(valor) FROM anuncios

How will you treat and display, there is no way to know with that piece of code. If you want, post full.

    
20.10.2017 / 19:45
0
<?php require 'pages/header.php'; ?>
<?php
    if(empty($_SESSION['cLogin'])) {
        ?>
        <script type="text/javascript">window.location.href="login.php";</script>   
        <?php
        exit;
    }
?>
<div class="container"> 
    <h1 align="center">CONTROLES</h1>

    <a style="margin-left: 510px;" href="add-anuncio.php" class="btn btn-success">Adicionar Dados</a><br/><br/>

    <table class="table table-striper">
        <thead>
            <tr>
                <th>Foto</th>
                <th>Cliente</th>
                <th>Categoria</tb>
                <th>Valor</th>
                <th>Vencimento</tb>
                <th>Descrição</th>
                <th>Estado</th>
                <th>Ações</th>
            </tr>
        </thead>
        <?php
            require 'classes/anuncios.class.php';
            $a = new Anuncios();
            $anuncios = $a->getMeusAnuncios();

            foreach($anuncios as $anuncio):
        ?>
        <tr>
            <td>
                <?php if(!empty($anuncio['url'])): ?>
                <img src="assets/images/anuncios/<?php echo $anuncio['url']; ?>" width="100" height="40" border="0"/>
                <?php else: ?>
                <img src="assets/images/logo.png" width="100" height="40" border="0"/>
                <?php endif; ?>
            </td>
            <td><?php echo $anuncio['titulo']; ?>
            </td>
            <td>
                Cebola
            </td>
            <td>R$ <?php echo number_format($anuncio['valor'], 2); ?></td>
            <td>
                <?php echo $anuncio['data']; ?>
            </td>
            <td>
                <?php echo $anuncio['descricao']; ?>
            </td>
            <td><?php if($anuncio['estado']==1) {?><strong>PAGOU</strong><?php }else {?><strong>NÃO PAGOU</strong><?php };?></td>
            <td>
                <a href="editar-anuncio.php?id=<?php echo $anuncio['id']; ?>" class="btn btn-warning">Editar</a>
                <a href="excluir-anuncio.php?id=<?php echo $anuncio['id']; ?>" class="btn btn-danger">Excluir</a>
            </td>

        </tr>
        <?php endforeach;
        ?>
        <tr>
        </tr>
            <thead align="center">
                <tr>
                    <th style="text-align:center;">Valor Total:<?php 
                    $sql = $pdo->prepare("SELECT SUM(valor) FROM anuncios WHERE valor = ?");
                    $sql->execute(array($_POST['valor']));

                    $ln = $sql->fetchObject();

                    print_r($ln);

                    ?></th>

                </tr>
        </thead>
</table>

    
20.10.2017 / 19:46
0

Use AS mysql to create a nickname that is accessible in fetchObject / fetchAssoc :

<tr>
    <th style="text-align:center;">Valor Total:<?php

    $sql = $pdo->prepare("SELECT SUM(valor) AS total FROM anuncios");
    $sql->execute();

    $ln = $sql->fetchObject();

    echo $ln->total;

    ?></th>
</tr>

Use WHERE if you want to filter from a logged-in user, like this:

<th style="text-align:center;">Valor Total:<?php

$sql = $pdo->prepare("SELECT SUM(valor) AS total FROM anuncios WHERE id_usuario=?");

//Passa o ID do usuário para a query
$sql->bindValue(1, $_SESSION['id_do_usuario'], PDO::PARAM_INT);

$sql->execute();

$ln = $sql->fetchObject();

echo $ln->total;

?></th>
    
20.10.2017 / 21:41