Difficulty in recovering BRANCH information given a PRODUCT, many-to-many relationship

1

I recently started my studies in the area and I have a difficulty in the code.

I need to display the data as follows:

  • Product code
  • Product class
  • Product Description
  • Name of the branch where you have the product
  • Availability (if it exists in the stock table it will turn green, otherwise red)

Below is the table and column data that I need to use

db_product

  

CODE, CLASS, DESCRIPTION

db_estoque

  

COD_FILIAL, CODE_PRODUCT

db_filial

  

COD_FILIAL, FULL_NAME

This is my code FULL

<?php

 //tabela

 echo "<table border=1>";
 echo "<tr>";
 echo "<th>Codigo</th>";
 echo "<th>Descrição</th>";
 echo "<th>Classe</th>";
 echo "<th>Disponibilidade</th>";
 echo "</tr>";

//conexão

 $strcon =  mysqli_connect('localhost','s.o','b.7','s.a') or die ('Erro');
$sql = "SELECT * FROM db_produto";

$resultado = mysqli_query($strcon,$sql) or die('erro bd');

//resultados loop

while ($registro = mysqli_fetch_array($resultado))
{
$codigo = $registro['CODIGO'];
$descricao = $registro['DESCRICAO'];
$classe = $registro['CLASSE'];
echo "<tr>";
echo "<td>".$codigo;"</td>";
echo "<td>".$descricao;"</td>";
echo "<td>".$classe;"</td>";
echo "</tr>";

}

// fim while 

mysqli_close($strcon);
echo "</table>";
?>
    
asked by anonymous 22.11.2017 / 00:24

2 answers

1

Well you will need to use JOIN , I imagine it will cure your doubt, and you can also put everything inside while thus filling the table with the records that will be displayed from the database, n column Disponibilidade I put the field nome_filial to fill in but you can change, I just put you to analyze code.

Follow the connection file and name it conn.php

conn.php

 <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "nomedoseubanco";

    // Criando a conexão com o banco de dados
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Checando a conexão com o banco de dados
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
?>

You also have to change the code below

<table class="display example" cellspacing="0" width="100%">
    <thead>    
        <tr>
            <th>Código do Produto</th>
            <th>Classe</th>
            <th>Descrição</th>
            <th>Filial</th>
            <th>Disponibilidade</th>
        </tr>
    </thead>
    <tbody>
        <?php
              include ("conn.php");

                $result = "SELECT A.codigo, A.classe, A.descricao, B.cod_filial, C.nome_filial FROM DB_PRODUTO A"
                        . " LEFT OUTER JOIN DB_ESTOQUE B ON (A.codigo = B.codigo_produto)"
                        . " LEFT OUTER JOIN DB_FILIAL C ON (B.cod_filial = C.cod_filial) ORDER BY A.codigo";
                $resultado = mysqli_query($conn, $result);

                $row = array();

                while ($row = mysqli_fetch_assoc($resultado)){

                    echo "<tr class='btn-default'>";
                        echo "<td>". $row['codigo'] ."</td>";
                        echo "<td>". $row['classe'] ."</td>";
                        echo "<td>". $row['descricao'] ."</td>";
                        echo "<td>". $row['cod_filial'] ."</td>";
                        echo "<td>". $row['nome_filial'] ."</td>";
                    echo "</tr>";

                }
        ?>
    </tbody>
</table>
    
22.11.2017 / 02:40
0

Product binds with stock that binds with branch. At least that's what the column names indicate, mnemonicamente.

To accept products that are not in inventory, always remember the sql join chart, see this answer . In this case, we want what the author of the image identified as two LEFT OUTTER JOINS .

Applying the same image format for your case:

SELECT p.*, f.nome_filial
FROM
  db_produto p
    LEFT JOIN db_estoque e ON p.codigo = e.codigo_produto
    LEFT JOIN db_filial f ON e.cod_filial = f.cod_filial

After rescuing this information would be treat for display. In this case, null branch names should not be displayed in the table, and should also be red in the availability column. If it were not null, just display the value in the appropriate column and green in the availability column.

Above, the treatment of which color to display would be in PHP. Can you play in the bank? Yes, it is possible. In this case, let's return in the query a new column (let's call it generically from cor_disponibilidade ?), Where it will return color in RGB then only apply it in CSS (more about it here: Is it possible to change the background color of an element for printing? ).

In case, to play in the bank, we will check if the value of the branch code is null and return #ff0000 for absence, or #00ff00 for availability. The structure is basically the same:

SELECT p.*, isnull(f.nome_filial, '') as nome_filial,
    CASE
      WHEN e.cod_filial IS NULL THEN '#ff0000'
      ELSE '#00ff00'
    END as cor_disponibilidade
FROM
  db_produto p
    LEFT JOIN db_estoque e ON p.codigo = e.codigo_produto
    LEFT JOIN db_filial f ON e.cod_filial = f.cod_filial
    
22.11.2017 / 01:10