List table array horizontally and vertically

Asked

Viewed 141 times

1

I’m trying to list the company name horizontally, the quantities of the products just below each company name, and the product name in the first column vertically.

inserir a descrição da imagem aqui

My code is this, but not this listing as I need.

<br>
<table border="1" width="100%" >   
  <tr>
    <th>Nomes das Empresas  &#9658;<br>Nomes dos produtos &#9660;</th>

    <?php
    require 'config.php';
    $sql = "SELECT * FROM empresas inner join produtos group by nomeEmpresa ";
    $dados = $pdo->query($sql);

    if ($dados->rowCount() > 0) {
      foreach ($dados->fetchAll() as $om) {
        $idEmpresa = $om['idEmpresa'];
        $nomeEmpresa = $om['nomeEmpresa'];
        $nomeProduto = $om['nomeProduto'];

        echo "<th>";echo $om['nomeEmpresa']; echo "</th>";// Lista o  nome das empresas na horizonttal

      }

      echo "<tr>";
      echo "<th>";echo $om['nomeProduto']; echo "</th>"; // Lista o  nome de um dos produtos na vertical

      $sql = "SELECT count(idEmpresa) as soma FROM empresas  ";
      $sql = $pdo->query($sql);
      $contar = $sql->fetch();
      $quantidadeEmpresas =$contar['soma'];

      for ($x=1; $x <=$quantidadeEmpresas ; $x++) { 


        $sql = "SELECT quantidade_produto   FROM rel_empresa_produtos WHERE idEmpresa = '$x'   ";
        $dados = $pdo->query($sql);
        if ($dados->rowCount() > 0) {
          foreach ($dados->fetchAll() as $om) {

            $quantidade_produto = $om['quantidade_produto'];

            echo "<th>";echo $om['quantidade_produto']; echo "</th>";

          } 

        }
      }

}
echo "</table>";

Tables of the database:

inserir a descrição da imagem aquiinserir a descrição da imagem aquiinserir a descrição da imagem aqui

  • Put in question the diagrams of the tables empresas and produtos

  • Good morning friend, I put the DB prints, if you can help me thank you very much..

  • Give a analyzed in the edition I made and see if it is to your liking. If not I reverse.

  • You just edited the presentation let’s say so, the code remains the same right? Can you help me leave the way I need it?

1 answer

-1

Try to "encapsulate" all th of the company names into a single tr too.

Something like that:

<thead>
  <tr>
     <th>Empresa 1</th>
     <th>Empresa 2</th>
   </tr>
</thead>
  • The name of the companies is already horizontal, now I need to leave the name of the product in the first column on the right and the quantity side by side and below the name of each company, this is what I’m not getting right.

  • So, it wasn’t very clear, it would be better to show a print of how you want it. You want a simple table with the header, and the listing would be: NOME_PRODUTO, QTD_EMP1, QTD_EMP2, QTD_EMP3 ? If this is so, just set the first column to <td>$productName</td> and the rest you use the <td>$om['quantity_product']</td>, all this in a single <tr> . To basing me by high, I don’t know what the query returns.

  • I put the image, I need it to stay that way there.

Browser other questions tagged

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