Update HTML Table with PHP

Asked

Viewed 1,749 times

4

I have a page that you can select in php in a Firebird database and return the result in table form.

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

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

inserir a descrição da imagem aqui

Code:

<?php 
include ("_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>
    <link rel="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>

2 answers

1

Hello, Joca.

I did exactly what you were doing some time ago. Let’s go by parts:

For input you must set the name attribute as an array:

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

Remembering that if you need to take table data to insert into some database table, you must include in the Hidden input form:

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

And when processing and picking up the post you can take 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.

  • Thanks for the Help! This returning an error in the Array follows how the code went! http://pastebin.com/L1drxE7e I need to update the price of the current price column..

1

Following the idea that the friend skylights gave I would do so:

In Your HTML

You’ll need to have the spine ID

<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 that via jQuery: And you’ll also need the lib 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.

Browser other questions tagged

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