Pick up bank record and if duplicated, show only one

Asked

Viewed 33 times

0

I have a table in the bank called orders, where the control number can be duplicated, because the customer in the same order can buy 2 items, IE, each row will insert a product purchased but with the same control number, and I wanted that when it comes to displaying the purchased products, show only one, and then when I click I will show the rest, but in the display I need it to bring me only 1, may be the last record of this control, and not duplicated as it is coming. I don’t know if I made myself clear.

<table class="table">
                                                <thead>
                                                  <tr>
                                                    <th scope="col">Número pedido</th>
                                                    <th scope="col">Data</th>
                                                    <th scope="col">Valor</th>
                                                    <th scope="col">Situação</th>
                                                  </tr>
                                                </thead>
                                        <?
                                            $result = ("SELECT * FROM pedidos WHERE id_cliente = '".$usr_id."' ORDER BY id DESC");
                                            $execute = mysqli_query($conn, $result);

                                            if(mysqli_num_rows ($execute) > 0 )
                                            {
                                                while ($dados_cliente = mysqli_fetch_assoc($execute)){
                                                $data = $dados_cliente['data'];
                                                $controle = $dados_cliente['identificacao_pedido'];
                                                $valor_pedido = $dados_cliente['valor_pedido'];
                                                $valor_frete = $dados_cliente['valor_frete'];
                                                $situacao = $dados_cliente['situacao'];

                                              $total = $valor_pedido+$valor_frete;
                                              if ($situacao=='ag') {
                                                $exibir_situacao = 'Aguardando Pagamento';
                                              }
                                              elseif ($situacao=='ap') {
                                                $exibir_situacao = 'Pagamento Aprovado';
                                              }
                                              elseif ($situacao=='cn') {
                                                $exibir_situacao = 'Pagamento Cancelado';
                                              }
                                              elseif ($situacao=='sp') {
                                                $exibir_situacao = 'Em separação';
                                              }
                                              elseif ($situacao=='tr') {
                                                $exibir_situacao = 'Em transporte';
                                              }
                                              elseif ($situacao=='en') {
                                                $exibir_situacao = 'Pedido Entregue';
                                              }
                                            ?>

                                                <tbody>
                                                  <tr>
                                                    <th scope="row">#<?=$controle;?></th>
                                                    <td><?=$data;?></td>
                                                    <td>R$ <?=number_format($total,2,',','.');?></td>
                                                    <td><span><strong><?=$exibir_situacao;?></strong></span></td>
                                                  </tr>

                                                </tbody>


                                    <?}}else{?>
                                    <table class="table table-striped table-dark">
                                      <center>Você não efetuou nenhum pedido ainda.</center>
                                    </table>
                                    <?}?>
                                    </table>
  • I could not make a query using DISTINCT to list only the values, without the repetitions, and then go to another element/page that shows all items independent of repetitions?

1 answer

0

Opa!

Do a distinct in your query like the example below, so you will only display one at a time, and place a detail link in each row to display all lines.

SELECT DISTINCT(CampoControle) FROM pedidos WHERE id_cliente = '".$usr_id."' ORDER BY id DESC

I hope it helps.

  • so it worked, but now it’s just displaying field control and values and everything else is not showing

  • Place the rest of the fields at the beginning of the query after select SELECT DISTINCT(CampoControle), ColunaB, ColunaC FROM pedidos...

Browser other questions tagged

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