Update HTML Table with PHP

4

I have a page that is giving select in php in a Firebird database and returning the result in table form.

My interest is to update the prices of a table to quote price, how do I click on the cell and update the price and send it to the bank or put it in the text field next to it and have it updated?

I can not get the data from the text field to put in the update.

Code:

<?phpinclude("_functions.php");
conecta();
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>ITENS COTACOES</title>
    <!-- <link rel="stylesheet" type="text/css" href="estilo.css" /> -->
    <!-- <script type="text/javascript" src="script.js"></script> -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><linkrel="stylesheet" href="css/estilos.css">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href='https://fonts.googleapis.com/css?family=Quicksand:400,700' rel='stylesheet' type='text/css'>
</head>
<body>
<form method="post" name="attcotacao" >
<table width="100%" border="1" cellspacing="0" cellpadding="3" id="playlistTable">
<tr>
    <tr>
    <td align="center" bgcolor="#CCCCCC"><strong>DISTRIBUIDOR</strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong>COD. BARRA</strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong>PRODUTO</strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong>LABORATORIO</strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong>QNTD.</strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong>PRECO ATUAL</strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong>PRECO COTACAO</strong></td>

</tr>
<?php 
//Recebe Dados do Form
    $tecnicos = ibase_query('SELECT * FROM ITENS_COTACOES');


    while ($linha = ibase_fetch_assoc($tecnicos)) {


    //Imprime Resultado na Tela
    echo '<tr>';
        echo '<td align="center">'.$linha['DISTRIBUIDOR'].'</td>';
        echo '<td align="center">'.$linha['CODIGO_BARRAS'].'</td>';
        echo '<td align="center">'.$linha['PRODUTO'].'</td>';
        echo '<td align="center">'.$linha['LABORATORIO'].'</td>';
        echo '<td align="center">'.$linha['QUANTIDADE'].'</td>';
        echo '<td align="center">'.$linha['PRECO'].'</td>';
        echo '<td align="center"><input type="text" name="status" id="status"></td>';

}
?>

<input type="submit" name="atualizar" id="atualizar" value="Atualizar Preços">
<?php 

if (isset($_POST['atualizar'])){
$status = $_POST['status'];
echo $status;
// $id_produto = 13840;

//      $query = ibase_query("UPDATE ITENS_COTACOES SET PRECO = '$preco' WHERE id_produto = '$id_produto'");

}
?>

</form>
<a class="logoff" href="logout.php">Sair do Sistema</a>
</table>

</body>
</html>
    
asked by anonymous 27.07.2016 / 21:12

2 answers

1

Hello, Joca.

I've done exactly what you've been doing for some time. Let's break it down:

For the input you should set the name attribute to array:

echo '<td align="center"><input type="text" name="status[]" id="status"></td>';

Remember that if you need to get the data from the table to insert into some database table, you must include it in the form inputs hidden:

<input type='hidden' name='codigo[]' value="<?php echo $dados[$key]['CODIGO']?>" id=''/>

And when processing and picking up the post you can get the values as follows:

$x = 0;
while($x < count($_POST['CODIGO_BARRAS'])) {
$preco[$x] = $_POST['preco'][$x];

//E para fazer o insert ou update no banco, insira o código aqui. Ou seja,
//para cadas registro na table ele executará a ação para o banco.

$x++;
}

I hope I can help you.

    
27.07.2016 / 22:39
1

Following the idea that my friend clarodrigues gave I would do so:

  

In Your HTML

You will need to have the ID column

<table class="table">
<tr>
    <tr>
    ...
    <td align="center" bgcolor="#CCCCCC"><strong>ID</strong></td>
    ...
    <td align="center" bgcolor="#CCCCCC"><strong>PRECO COTACAO</strong></td>
</tr>


while ($linha = ibase_fetch_assoc($tecnicos)) {
    //Imprime Resultado na Tela
    echo '<tr>';
        echo '<td align="center">'.$linha['ID'].'</td>';
       ...
        echo '<td align="center"><input type="text" name="status[]" id="status"></td>';
}

I would do this via jQuery: And you'll also need the jquery.tabletojson.min.js

  

In your javascript

$('#atualizar').on('click',function(){
   var table  = $('.table').tableToJSON();
   var tabela  = JSON.stringify(table);
   var status = document.getElementById('status').value;
   $.ajax({
     type : 'post',
     dataType : 'json',
     url : 'seuarquivo.php',
     data : {
         tabela : tabela,
         status : status,
         atualizar : 'A' 
     },
     success : function(data){
         window.location.reload();            
     }


   }}
});
  

In your php

if (isset($_POST['atualizar'])){
$tabela = $_POST['tabela'];
$cotacaoArray = json_decode($tabela);
$status = $_POST['status'];
int totalStatus= count($status);
$x = 0;
 foreach ($cotacaoArray as $item => $value) {
    $x++;
    $query = ibase_query("UPDATE ITENS_COTACOES SET PRECO = '".$status[$x]."' WHERE id_produto = '".$value->{'ID'}."'");
 }


echo json_encode(array("retorno", 1));

}

I hope it helps.

    
01.07.2017 / 23:05