How to pull all rows of the database in php but only showing the value of a specific query column once?

Asked

Viewed 150 times

0

I’m having a problem, I have a PHP screen that shows everything from a database query.

So far everything ok, but I need to show all information without replicating the requested column.

Follow the prints.

The first is that I bring today:

inserir a descrição da imagem aqui

The second is what I need to do:

inserir a descrição da imagem aqui

I’m wearing a while in PHP pulling from a Mysql database.

Follows the code:

$sql = mysqli_query($con,"SELECT 
                            a.num_pedido,
                            nota_fiscal,
                            lote_serial,
                            produto,
                            qtd_disp-pedido qtd_disp,
                            lote,
                            unid_medida,
                            pedido,
                            a.status,
                            cnpj,
                            a.nome_cli,
                            a.endereco,
                            a.numero,
                            a.bairro,
                            a.cep_cli,
                            a.cidade,
                            cod_id,
                            data 
                            from
                            sistemas_ag.clientes_ag a left join
                        sistemas_ag.agendamento_ag b on a.num_pedido = b.num_pedido
                        where cnpj = '".$cnpj."' 
                        and (a.num_pedido like '%$busca%'
                        or nota_fiscal like '%$busca%'
                        or produto like '%$busca%'
                        or lote_serial like '%$busca%')
                        order by length(a.num_pedido), a.num_pedido asc")or die("erro no select verifica pedido");
  • 1

    boy I don’t understand what you need

  • I need to merge the information from the first column showing just once more without hiding to others. I need something to check if the value is repeated if it is shown once

  • the problem of hiding that in case, are different id’s, how would you treat it ? if possible put the query you used to generate this grid

  • Just use the command DISTINCT mysql, which will only return a single request.

  • It is not in the query that you will change it. You will have to change it is in the display programming of this data on the screen. Where is the code?

  • vese a GROUP BY a.num_pedido solves, at least the part of duplicated records I think will solve. But it may end up "missing" a few lines.

Show 1 more comment

2 answers

2

What you need is to use the "rowspan" attribute in the html td tag (https://www.w3schools.com/tags/att_td_rowspan.asp).

I made an example of using the loop of repetition foreach. I even recommend that you use foreach instead of while for data exposure. Of course there are exceptions.

<?php
$pk = "id";
$products = [
    [$pk => 1,"name" => "mouse"],
    [$pk => 1,"name" => "mouse"],
    [$pk => 2,"name" => "keyboard"],
    [$pk => 2,"name" => "keyboard"],
    [$pk => 2,"name" => "keyboard"],
    [$pk => 3,"name" => "notebook"],
    [$pk => 3,"name" => "notebook"],
    [$pk => 4,"name" => "desktop"],
];
array_push($products, [$pk => ""]);
?>

<table border="1">
    <thead>
        <tr>
            <th>Código</th>
            <th>Nome</th>
        </tr>
    </thead>
    <tbody>
    <?php 
    $repeats = [];
    foreach($products as $i => $product):
        if($i === 0):
            array_push($repeats, $product);
        elseif($product[$pk] !== $products[$i-1][$pk]):
            foreach($repeats as $iRepeat => $repeat):
    ?>
        <tr>
        <?php if($iRepeat === 0): ?>
            <td rowspan="<?= count($repeats) ?>"><?= $repeat[$pk] ?></td>
        <?php endif ?>
            <td><?= $repeat["name"] ?></td>
        </tr>
    <?php
            endforeach;
            $repeats = [];
            array_push($repeats, $product);
        else:
            array_push($repeats, $product);
        endif;
    endforeach;
    ?>
    </tbody>
</table>

0

Using "DISTINCT" in your select would already solve the problem of column data coming repeated. With "DISTINCT", the data from that column (with the same value) is only returned once in that query.

$sql = mysqli_query($con,"SELECT DISTINCT 
                            a.num_pedido,
                            nota_fiscal,
                            lote_serial,
                            produto,
                            qtd_disp-pedido qtd_disp,
                            lote,
                            unid_medida,
                            pedido,
                            a.status,
                            cnpj,
                            a.nome_cli,
                            a.endereco,
                            a.numero,
                            a.bairro,
                            a.cep_cli,
                            a.cidade,
                            cod_id,
                            data 
                            from
                            sistemas_ag.clientes_ag a left join
                        sistemas_ag.agendamento_ag b on a.num_pedido = b.num_pedido
                        where cnpj = '".$cnpj."' 
                        and (a.num_pedido like '%$busca%'
                        or nota_fiscal like '%$busca%'
                        or produto like '%$busca%'
                        or lote_serial like '%$busca%')
                        order by length(a.num_pedido), a.num_pedido asc")or die("erro no select verifica pedido");

Browser other questions tagged

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