Filling table

Asked

Viewed 77 times

0

I have a function, but I do not know how to make it recognize data with more than one registration linked to a code, for example I have this product code ABITAand it has 5 types and descriptions linked to it.

inserir a descrição da imagem aqui

But in my function I can only get the first or last record, which would be the ID 516 or ID 520. There is a possibility that I can fill this table below with correct data coming from the database in a way that each row receives a record ?

inserir a descrição da imagem aqui

index php.

<form action='salvar.php' method='POST'>   

    <div class='form-group col-lg-4'>
        <label>  <b>Descrição:</b> </label> <!-- Não é enviado para o banco só está sendo utilizado para preencher os campos a partir deste -->
        <input type="text" maxlength="20"  name="descri"><br><br>
    </div>
    <div class='form-group col-lg-4'>
        <label>  <b>Código do Produto:</b> </label>
        <input type="text" maxlength="15"  name="codigo_produto"><br><br>
    </div>

    <table border="2"><!-- Iniciando a Tabela -->

        <thead>
            <tr><!-- Início dos Títulos da Tabela / Cabeçalho -->
                <th>Tipo</th>
                <th>Descrição</th>                                                                                                              
            </tr><!-- Fim dos Títulos da Tabela / Cabeçalho -->
        </thead>

        <tbody>
        <?php for($i = 1; $i <= 5; $i++){ //coloquei este valor para testar ?>        
                <tr>
                    <?php              
                        $sql_tipo = "SELECT * FROM tipoprod ";
                        $resulta = $conn->query($sql_tipo);
                        $row = $resulta->fetch_assoc();
                            echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
                    ?> 
                    <td><input type="text" name="codigo_tipo[]"</td>
                    <td><input type="text" name="descricao[]" </td> 

                </tr>
           <?php } ?>
        </tbody>

    </table><br>

    <div class='form-group col-lg-3'><!-- Inicio Botão para efetuar registro no Banco de Dados -->
        <input type="submit" class="btn btn-success btn-lg btn-block" name="enviar_tipo" value="Salvar Informações">
    </div>

</form>

functionx.php

<?php
include_once("conn.php");

function retorna($descricao, $conn){
    // Utilizando JOIN para trazer dados de mais de uma tabela 
    $result = "SELECT A.descricao, B.id, B.codigo_produto, B.codigo_tipo, B.descricao FROM CADPRO A"
            . " LEFT OUTER JOIN TIPOPROD B ON (A.CODIGO_PRODUTO = B.CODIGO_PRODUTO) WHERE A.descricao = '$descricao' "; 

    $resultado = mysqli_query($conn, $result);

    // DECLARA A VARIAVEL
    $valores = array();

    // Realiza o preenchimento dos campos a partir da descricao do produto informado
    if($resultado){
        $row = mysqli_fetch_assoc($resultado);           
        $valores['codigo_produto'] = $row['codigo_produto'];
        $valores['id'] = $row['id'];
        $valores['codigo_tipo'] = $row['codigo_tipo'];
        $valores['descricao'] = $row['descricao'];


    } else {
        return json_encode(array( 'error' => mysqli_error($conn) ));        
    }

    return json_encode($valores);                
}

if(isset($_GET['descricao'])){
    echo retorna($_GET['descricao'], $conn);
}
?>

infoprod table

inserir a descrição da imagem aqui

  • Hello, Vitor! Could you provide us with the structure of your tables in the database? So it’s easier to see where the problem is, it’s probably in your select, but it’s easier if we have the structure of the tables.

  • I believe that if it was an error in select the function would not return the data, correct ? I’m thinking that inside Function I have to do a while or a loop for to go through the table data but I can’t do it. I will edit the question with the infoprod table structure

  • Cool.... I’ll take a look, I think I expressed myself badly about the error in select, kkk.... I meant in the logic of select and not select itself... rs you can put the structure of tipoprod as well?

1 answer

1

Good...

Actually I was a little mistaken with the error in the above comments, let’s go to the codes, rs.

<tbody>
    <?php for($i = 1; $i <= 5; $i++){ //coloquei este valor para testar ?>        
        <tr>
            <?php              
                $sql_tipo = "SELECT * FROM tipoprod ";
                $resulta = $conn->query($sql_tipo);
                $row = $resulta->fetch_assoc();
                    echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
            ?> 
            <td><input type="text" name="codigo_tipo[]"</td>
            <td><input type="text" name="descricao[]" </td> 

        </tr>
   <?php } ?>
</tbody>

In this excerpt you sent, note that you are always picking up only the first result. The correct thing would be for you to create a loop to go through each of the results, it would look something like this:

<?php
$sql_tipo = "SELECT * FROM tipoprod ";
$resulta = $conn->query($sql_tipo);
?>
<tbody>
    <?php while($row = $resulta->fetch_assoc()) : // Enquanto houver registro ele roda o while ?>        
        <tr>
            <td><input type="hidden" name="id" value="<?=$row['id'];?>"></td>';
            <td><input type="text" name="codigo_tipo[]" value="<?=$row['codigo_tipo'];?>"></td>
            <td><input type="text" name="descricao[]" value="<?=$row['descricao'];?>"> </td> 

        </tr>
   <?php endwhile ?>
</tbody>

Try to check these changes if they work for you.

  • Thanks, but I even know how to do this, what I wanted was that from the user write the description the other fields were completed automatically, and this way I will go through the table all searching for all records

  • I also noticed the lack of looping in the sql query, as answered by @Rodrigoteixieraandreotti, but now in your comment I was in doubt what you really need, could edit the question and explain this doubt, so other people can also help you.

  • Just like @Wagnerfernandomomesso I was also a bit in doubt now, because it seemed to me that you were having problems to popular the table with the data of your select, but now it seems to me you are wanting to popular the table from a user input, If that’s true, I think you’ll need some changes to your code.

  • so that’s basically this, in the description field the user will inform the product description, so I can’t bring all records linked to the product on the screen, only the first, wanted to know how to change the code to do this

  • The corrected code I posted also applies to this case, you will need to change your query to use a Where and refer to the description. The functionx.php file you posted, in a way is doing this, but it is not being used in the construction of your table, so you will need to make this change in the query you are using, and use the loop the same way I pointed out

  • But in this case I would have to include my ID, in the search for the Where clause, however I tried it and did not give, but anyway thank you, even not answering the question

Show 1 more comment

Browser other questions tagged

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