Forms and Files

Asked

Viewed 96 times

3

I’m making a form where I basically access a file .txt in the CSV format containing certain articles and their prices (e.g., cheese,2.10).

The program presents the list in a table, and below appear two fields that ask for "Article" and "Quantity", now what is the best way to make the program look for the article in the file, and tell if it exists or not, if the product exists, show the total multiplication of the quantity with the value of the product, which is in the second column of the file .txt.

For example, Product: Cheese, Quantity: 10, present 10x the price of total cheese below.

PS: Forms with the "Article" and "Quantity" fields disappear after being filled in.

So far my code is like this:

<?php

$br="</br>";
$file=fopen("produtos.txt","r");

echo "<table border='1'><tr><th>Artigos</th><th>Preco</th>";
while(!feof($file)) {
$registo=fgetcsv($file);

    echo"<tr>";
    echo"<td>".$registo[0]."</td>";
    echo"<td>".$registo[1]."</td>";
    echo"</tr>";

}
echo "</table>";
fclose($file);
?>

<?php
if ($_POST) {
    echo "Produto: ".$_POST["produto"]."<br>";
    echo "Quantidade ".$_POST["quantidade"]."<br>";
}
else {
?>

<form action="eta15.php" method="post">
Produto <input type="text" name="produto"><br>
Quantidade <input type="text" name="quantidade"><br>
<input type="Submit" name="enviar" value="Enviar">
</form>

<?php 
}
?>

I leave two printscreens, I hope they help in my question of the result in which I am:

Search form:

inserir a descrição da imagem aqui

Result form:

inserir a descrição da imagem aqui

2 answers

1


For this just make a search condition, if the product is equal to the searched then calculates the total of the price multiplied by the quantity:

//...
$produto = isset( $_POST["produto"]    ) ? $_POST["produto"]    : "";
$qtd     = isset( $_POST["quantidade"] ) ? $_POST["quantidade"] : ""; 
$total   = 0;
$precoUN = 0;

while(!feof($file)) 
{
    $registo=fgetcsv($file);

    if( $produto == $registo[0] )
    {
        $total   = $registo[1] * $qtd;
        $precoUN = $registo[1];
    }

    echo"<tr>";
    echo"<td>".$registo[0]."</td>";
    echo"<td>".$registo[1]."</td>";
    echo"</tr>";

}
//...
if ($_POST) 
{
    echo "Produto:    ". $produto ."<br>";
    echo "Quantidade: ". $qtd     ."<br>";
    echo "Total       ". $total   ."<br>";
    echo "Preço/UN    ". $precoUN ."<br>";

    if ( $total > 0 ) 
    {
        echo "<br> O Produto selecionado existe";
    }
    else 
    { 
        echo "O Produto nao existe";
    }
}
//...
  • 1

    This is the right answer to the question that has been asked, it has a clean and professional code! Thanks for your time and help!

  • In this case, if I wanted the table to disappear after Submit, where I had to put it ?

  • @Mariocaeiro inside an if, so: if( ! $_POST ){ //tabela }

  • I didn’t notice, I have the code like this: http://pastebin.com/k32byUWD ?

  • @Mariocaeiro put the code that foils the table within a condition, if it was not done POST then print: if ( ! isset( $_POST ) ) { echo "<table border='1'> ........ echo "</table>"; }

  • I tried that, but when it presents values it ceases to calculate and accuses that there is no product, but the table disappears

  • @Mariocaeiro I forgot that you have there the research, it will be complicated thus...

Show 2 more comments

-2

vê se é isso?! I solved with If, anything question there!..

  • 4

    Avoid posting codes in image form, even more as a response. If possible, add the code directly into the reply.

  • Hello Cleber, when I leave work I will test your help, I need to present if there is or not the item, and present the result of the quantity (Example, Product - Cheese, Quantity - 10) and present in this case 10x the unitary price

  • Cleber, when I tried to use your code, simply the table with the items does not appear! The table appears but with nothing filled.

Browser other questions tagged

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