Duplicate values in php

0

I have a table in the bank that is called a toy, inside it I have the columns name, total, amount available and quantity donated. But I want to show only the name, the total and the total amount donated.

When I use the code below that I know is not what I have to use, it brings me everything but I have a repeated toy name on this table, and I wanted it to appear once, and the total came with the highest value and the amount donated of that toy already added.

    if(isset($_REQUEST['rlanual']))
    {
        $ano1= $_REQUEST['rlanual'];

        $res = $mysqli->query("select * from brinquedo where ano=$ano1");
        $row1= $res->num_rows;  
         while($escrever = mysqli_fetch_array($res))
        {
            $nome1 = $escrever['nome'];
            $total = $escrever['total'];
            $qtd = $escrever['qtd_disponivel'];
            echo "$nome1";
            echo "$total";
            echo "$qtd";


        }


    }

To get the highest total value I have this:

$sql1=$mysqli->query('SELECT MAX(total) FROM brinquedo');
                             $result1 = $sql1;

In short, I do not want the same toy to appear more than once. I've even tried string comparison and nothing.

    
asked by anonymous 19.09.2015 / 20:41

2 answers

0

Try using the following query:

$res = $mysqli->query('SELECT * FROM brinquedo WHERE ano = ' . $ano1 . ' GROUP BY nome');

I recommend creating an index in the name column to improve query performance.

    
19.09.2015 / 22:55
0

Assuming the scenario

tabela - brinquedos

colunas
  id 
  nome
  quantidade_doada
  quantidade_disponivel

and that the internal data is

id    nome        quantidade_doada    quantidade_disponível
1     bola        10                  9
2     bola        6                   6
3     carrinho    8                   8
4     blocos      80                  76
5     bola        6                   6

We can generate the response for a totalization report as follows

SELECT nome, sum('quantidade_disponivel'),sum('quantidade_doada') from brinquedos group by nome

This will generate the following result

nome      sum('quantidade_disponivel')  sum('quantidade_doada')   
blocos    76                            80
bola      21                            22
carrinho  8                             8

With this, just use the php code you already have and print the correct variables.

    
20.09.2015 / 22:09