Expandable Table how to do Double select?

Asked

Viewed 688 times

0

In this script a have an expandable table showing by GROUP by nome , that when we click on the line, it will expand, showing a second table listing all the lines referring to the GROUP by nome.

Closed Table:

inserir a descrição da imagem aqui

Open Table: How it should show

Tabela Aberta

What I don’t know how to do is that this daughter table shows all the records referring to the mother table that show in GROUP by nome.

    <?php
    include"config.php";
    ?>
    <script  
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"  
    type="text/javascript"></script>
    <script type="text/javascript">  
    $(document).ready(function () {
    $("#report > tbody > tr").hide();
    $("#report > tbody > tr:even").addClass("odd").show();

    $("#report tr.odd").click(function () {
    $(this).next("tr").toggle();
    $(this).find(".arrow").toggleClass("up");
  });
  });
  </script>        
  </head>
  <body>
  <table id="report" border="1" style="width: 50%;">
    <thead>
        <tr>
            <th style="width: 20%;">Id</th>
            <th>Cliente</th>
            <th>Data</th>
        </tr>
    </thead>
    <tbody>
    <?php

    $sql = "SELECT * FROM a_finan GROUP by cliente ";

  $result = $PDO->query( $sql );
  $linha = $result->fetchAll();
  foreach ($linha as $row) {

  $id_empresa = $row['id_empresa'];
  $id_finan = $row['id_finan'];
  $start = $row['start'];
  $tipo = $row['tipo'];
  $valor = $row['valor'];
  $os = $row['os'];
  $cliente  = $row['cliente'];
  ?> 

  <tr>
  <td> <a href="#"><?php  echo $id_finan?> Mostrar Faturas</a></td>
       <td><?php  echo $nome_fan?></td>
       <td>30,00</td>
       </tr>
        <tr>
            <td colspan="5">
                <table>
                    <tr>
                        <td>
                        </td>
                        <td>OS</td>
                        <td>Cliente</td>
                        <td>Data</td>
                        <td>Centro Custo</td>
                        <td>Status Transacação</td>
                        <td>valor</td>
                        <td>Acao</td>
                    </tr>
                    <tr>
                        <td>
                        <input type="checkbox" />
                        </td>
                        <td><?php echo $id_finan?></td>
                        <td><?echo $cliente?></td>
                        <td>12/10/2015</td>
                        <td>xxx</td>
                        <td>xxx</td>
                        <td>30,00</td>
                        <td>Del</td>
                    </tr>
                </table>
            </td>
        </tr>
       <?}?>
    </tbody>
    </table> 
    </body>
  • I didn’t understand anything... what’s the question anyway?

  • 1

    In the table above there are two records with id 1037 and 1043 they are grouped by name. so far all right.... the problem and how to do when I click on the "show invoices" link when expanding the line show the disaggregated records here is the script working without the https://jsfiddle.net/gxr47dqr/1selects/

  • Fabio this fiddle is as it should be or as it currently is?

  • he’s just an example of what this should be like in practice

  • What do you mean, disaggregated ? I couldn’t understand exactly what the problem is.

  • In the parent table in the image above it closes a select using a Group by name, grouping all rows with the same name. What I want and that when clicking on the link while expanding the line show the lines that were grouped by Group by name.

  • 1

    Do you have the grouped list that would be customers, each customer has their correct ID? If correct, when you click on the link to "show invoices" click an onclick JS function on the link and do an AJAX by going to the search the client ID, with this ID you select the invoices and assemble the HTML by AJAX.

  • How are your tables structured? What are the fields and what values will be searched for? Knowing how they work makes it easier to help.

  • @Mayronceccon unfortunately know nothing of javascript and ajax.

  • Ola amigo thinks of a simple and unique table. where the fields are ( id,client,date,tel,status,value). In the parent row show (id, client, value) grouped by the client column. and in the expanded line show all ( id,customer,date,tel,status,value) referring to the grouped client.

  • @Mayronceccon converted your response into comment. If you want you can give a complete answer, with code examples for AP to understand the problem and the solution.

Show 6 more comments

2 answers

1


Try using the following query:

SELECT distinct cliente, data, group_concat(CONCAT_WS(',',id,coluna1,coluna2,valor) SEPARATOR ';') as resultados FROM tabela GROUP BY cliente ORDER BY cliente;

Where CONCAT_WS(',',id,coluna1,coluna2,valor) will group the columns id, coluna1, coluna2 e valor and group_concat([expr] SEPARATOR ';') will group lines and separate by ;

You will get the following result:

+----------------+------+----------------------------------------------+
| cliente        | data | resultados                                   |
+----------------+------+----------------------------------------------+
|    CVC Turismo |   30 | 1,xxx,xxx,30;4,xxx,xxx,30;6,xxx,xxx,30;      | 
| Sterna Viagens |   30 | 2,xxx,xxx,30;3,xxx,xxx,30;5,xxx,xxx,30;      |
+----------------+------+----------------------------------------------+

The treatment of the spine resultados, you can do in PHP itself. Ex.:

 $sql = "SELECT distinct cliente, data, group_concat(CONCAT_WS(',',id,coluna1,coluna2,valor) SEPARATOR ';') as resultados FROM tabela GROUP BY cliente ORDER BY cliente;";

 $result = $PDO->query( $sql );

 $linha = $result->fetchAll();
 foreach ($linha as $row) {
     $resultadoLinhas = explode(';',$row['resultados']);
     foreach ($resultadoLinhas as $resultadoLinha) {
         $resultadoColunas = explode(',', $resultadoLinha);
         $id      = $resultadoColunas[0];
         $coluna1 = $resultadoColunas[1];
         $coluna2 = $resultadoColunas[2];
         $valor   = $resultadoColunas[3];

         // No checkbox passe o id da linha no valor do input
         echo "<input type='checkbox' value='{$id}'>";
     }
 }

In this link have some interesting information about the result grouping in Mysql, worth checking: http://www.mysqltutorial.org/mysql-group_concat/

  • Ola Carlos in mine is giving an error Fatal error: Call to a Member Function query() on a non-object in / line 14 http://sandbox.onlinephpfunctions.com/code/f157db47d1c9b81a8716a92844317c1daf9d5903

  • Hello @Fabiohenrique on the link you passed you try to run the function query without first starting the PDO class. Try putting the previous line $PDO = connect();

  • Really good that I needed

0

Hello, do the following:

Do not use GROUP BY, take all lines, let’s work the grouping in PHP.

You will have an array similar to this:

[0] => id,cliente,data,tel,status,valor
[1] => id,cliente,data,tel,status,valor
[2] => id,cliente,data,tel,status,valor

Now use a foreach to go through all of them and group by id, client, value.

Sort of like this:

$result_novo = array();

foreach ($result as $key => $item) {
    $result_novo[$item['id']]['id']      = $item['id'];
    $result_novo[$item['id']]['cliente'] = $item['cliente'];
$result_novo[$item['id']]['detalhes'][$item['algumid']]['valor'] = $item['valor'];   

$result_novo[$item['id']]['detalhes'][$item['algumid']]['data'] = $item['data'];   


}

In algumid key use a validated column for array key.

And now to display in HTML, do two foreach, the first normal and the second inside the first, which in the example above is in the key details, so you mount two table, then use a little Jquery to open and close.

foreach $result_novo as $key => $item

echo $item['id'];
echo $item['cliente'];

if $item['detalhes'] == array {

foreach $item['detalhes'] as $key2 => $item2 {
   echo $item2['valor'];
   echo $item2['data'];
}

}

}

Note: Use only the logic of the codes.

Hug !!

Browser other questions tagged

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