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

Asked

Viewed 687 times

0

They could give me a help, I have a table where I need to show the total of values, in case add all the values that this 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>
  • 1

    which database? there is a "sum" method for making sums in the database

  • You can use SUM. SELECT SUM(valor) FROM anuncios WHERE valor = :valor

  • This is returning something ? A lot is missing ! If so, it’s simple: SELECT SUM(valor) FROM anuncios

  • No, still not returning, do not know how to proceed, I am new in the area of PHP.

  • We’ve shown you how to get

  • @Marcosa.Massini have you tested in your bank the querys that Rafael or Raoni passed ? because they were supposed to have worked

  • @Viniciusshiguemori yes, if I give one print_r($in); he takes it normally, now if I give a echo $In->valor; he of error.

  • Only then he stays Total Value:stdClass Object ( [SUM(value)] => 12121 )

Show 3 more comments

4 answers

1

I don’t know if you know how to return the values, but based on my comment above, follow the base code.

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

$ln = $sql->fetchObject();

echo $ln->valor;
  • Notice: Undefined index: value in C: xampp htdocs control.php on line 70 stdClass Object ( [SUM('value')] => )

  • How do you send the parameter to be used in the WHERE? is POST really? because I put POST there example, you have to put as is your need

  • I’ll show you how my entire code is.

  • @Marcosa.Massini This, better, because then I can solve.

  • I don’t understand why you use the WHERE there I edited my answer, tests this way and tells me what you gave

  • It worked, there is no way to make it show only the value, instead of : Total Value:stdClass Object ( [SUM(value)] => 12121 )

  • @Marcosa.Massini I edited my code, just take the print and put echo as I did above

Show 2 more comments

0

The select is this:

SELECT SUM(valor) FROM anuncios

How you’re gonna handle it and display it, then there’s no telling with that piece of code. If you want, whole pole.

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>

  • $sql = $pdo->prepare("SELECT SUM(valor) FROM anuncios"); As I answered above

  • I got it with this code.

  • <?php &#xA; &#xA; $sql = $pdo->prepare("SELECT SUM(valor) FROM anuncios");&#xA;$sql->execute();&#xA;&#xA;$ln = $sql->fetchObject();&#xA;&#xA;print_r($ln);&#xA; &#xA;?>

  • There is no way to make it show only the value instead ??. Total Value:stdClass Object ( [SUM(value)] => 12121 )

  • There would be other ways to do it than by preparing it too.

  • use "echo $ln" instead of print_r, or if you have in array, "echo $ln[0]" or "echo $ln[value]"

  • in case it would be echo $ln->valor because it is an object, I edited my code above

  • Total Value: Notice: Undefined Property: stdClass::$value in C: xampp htdocs control.php on line 75 It is still returning this error, I applied your code above rafael.

Show 3 more comments

0

Use the 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 the WHERE if you want to filter from a logged in user, so:

<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>
  • And thank you very much, and William an example, if I want to add only what the user is logged in, and not everything that is in the database I do what?

  • @Marcosa.Massini I have no way of knowing friend, this is not in the question, as far as I see only has the table of advertisements, but in general would be via WHERE, for example SELECT SUM(valor) AS total FROM anuncios WHERE usuarioid=[aqui vai o id do usuario logado], this past id must be in a session.

  • So I understood, only that there complicates too much, why users are saving everything together in the bank.

  • What is the structure of the advertisement table? @Marcosa.Massini

  • id : as primary key INT and auto increment id_usuario: INT id_category: INT title: VARCHAR 100 Description: TEXT value: FLOAT status: INT date: DATE

  • @Marcosa.Massini so each ad is a "user" too?

  • Guilherme I have a "state" system that in the database is 0 and 1, and put to when show 0 this PAID, and when it is 1 printar NOT PAID, I tried to make a check to show the total value only of those that are written NOT PAID, which in the case is the value 1,

  • @Marcosa.Massini then try it like this $pdo->prepare("SELECT SUM(valor) AS total FROM anuncios WHERE id_usuario=" . $idDoUsuarioQueQuerFiltrar);

  • So, in case everyone has their id, because they are registered accounts, I put what? , sorry to bother, is that I’m learning now rsrs.

  • Users are in different tables, in this case they are in the user table.

  • @Marcosa.Massini help I want, but this hard to understand, you want to cross-reference data, like JOIN?

  • Don’t look like I’m gonna explain it to you. I want the total value shown to be only of that user who is logged in, and not of the others, in case each user has their id. Then I just want him to get the full amount of his ads. When the user adds ads there is a selection form that if he selects PAGO will with id 1, and if he selects NOT PAID will with id 0, then I wanted the values shown to be only from the user and in case show only those that are as NOT PAID. Understands?

  • Thus making all that are edited as PAID, go giving "Low", in this case taking the total amount and leaving only those that are as NOT PAID.

  • @Marcosa.Massini But this is exactly what the code I posted in the previous comment does ... Marcos $_SESSION['cLogin'] is the ID or login? What session variable contains the user ID? See the edit I made in my reply with an example of session pass.

Show 9 more comments

Browser other questions tagged

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