Difficulty to rescue information from AFFILIATE given a PRODUCT, many relationship to many

Asked

Viewed 95 times

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 subsidiary holding the product
  • Availability (which if it exists in the stock table will be green, otherwise red)

Below is the table and column data I need to use

db_product

CODE, CLASS, DESCRIPTION

db_stock

COD_FILIAL, CODIGO_PRODUTO

db_filial

COD_FILIAL, NOME_FILIAL

That is mine code COMPLETE

<?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>";
?>
  • That code has nothing to do with what I posted. It is difficult this way copy and paste my code is the error that is happening along with the creation of the file Conn.php

  • I edited my answer by comparing the code of the db_product table with the code of the db_stock table

  • Now it worked, could you tell me how it works? Some characters have this symbol , so I can signal with any character in the availability field, where I need to make the change?

  • Alexandra, you have an answer here regarding the strange characters... I believe the rest of your current question has been resolved with @Victor’s answer, correct? These characters are worthy of another question, not this one

  • Yeah, I’ll look it up. obgd

  • I found the question I had in mind: https://answall.com/q/43193/64969; I don’t know any other of this context here Sopt. If it doesn’t help... the new question button is right there #Hue

  • @Jeffersonquesado Thanks, I managed to solve

  • @Alexandra, did you manage to figure out which way your result was ?

  • @Alexandra, I tried to make the title more appropriate to the community proposal

  • @Victor Yes, it worked, I tested and the data is also correct with the BD!

  • I’m glad I could help, any doubt we’re there

Show 6 more comments

2 answers

1


Good you will need to utilize the JOIN I imagine it will clear your doubt, and it can also put everything inside the 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 but, you can change, I put only for you to analyze this code.

Follow the connection file and enter its name from 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);
    } 
?>

Also has change in 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>
  • Thanks @Victor, I put the connection code, but it shows the error Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given

  • I changed it again by placing the connection directly without include, and changed the variable name but printed the error between single quotes $result = mysqli_query($strcon,$result) or die('error');

  • Strange to have given this first error... Ever tried to run the query directly in Mysql? Without going through PHP?

  • Yes, I made the query in Mysql it works normally, I did it directly in Mysql

  • I will change and put a connection file, you change the name of the database and your password ok ?

  • I put the connection file, follow the steps, here it works perfectly,if it doesn’t work I think PHP should be outdated. Try not to change, after all the code is already adapted to suit your need

  • I made the changes, created other files, but appears Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given in , this can be PHP?

  • My php is in version 5.5

  • Check if the error is really in this query or in another above that comes before that. Otherwise, upgrade to php7 and paste this code that will be right, this php 5.5 only works with mysql, which in version 7.0 is discontinued and does not work so index mysqli

  • I updated, but still the same error, had some accentuation errors that was solved with the update, but Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given

  • The following occurs Parse error: syntax error, Unexpected '$result' (T_VARIABLE) and adding ; returns to the same error

  • Edit the question by placing the structure of your tables with the records, and the edited code together with the related file

Show 7 more comments

0

Product binds with stock that binds with branch. At least that’s what the columns name indicates, mnemonically.

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

Applying the same image format to 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 retrieving this information would 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 not zero, only 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, we will return in the query a new column (let’s call it generically cor_disponibilidade?), where color will be returned in RGB and then only apply in CSS (more about this here: It is possible to change the background color of an element for printing?).

In case, to play at the bank, we will check if the value of the affiliate 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
  • Thanks for providing the image, I understood the possibilities of Join, I replace, but it gave error, my complete code is this, how would be? but it accuses BD error this one, $result = mysqli_query($strcon,$sql) or die('bd error');

  • I used the same variable and only put the code here, $sql = "SELECT * FROM db_product";

  • @Alexandra will owe you a more complete code, I focused the answer on SQL which was where it appeared to be your greatest difficulty. Answering by mobile implies being difficult to test in the language itself. When I’m on the computer I put in PHP well

  • But was the error syntax? Or was it another error? Having the error I can try to improve the query or give a hint

  • Obigada, it was an error in mysqli_query, yes and my greatest difficulty, I will try again, and if possible I await your help

  • @Alexandra you intend to run your while with which fields to fill out the branch fields and availability ?

  • @Jeffersonquesado I’ll try to use the colors, thank you Jefferson!

  • @Alexandra, are you sure that my answer was the most appropriate to come up with the solution to your problem? I had the impression that it was Victor’s

Show 3 more comments

Browser other questions tagged

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